Best Gauge code snippet using logger.addLogger
base_test.go
Source:base_test.go
1package gomol2import (3 "errors"4 "github.com/aphistic/sweet"5 "github.com/efritz/glock"6 . "github.com/onsi/gomega"7)8type BaseSuite struct{}9var testBase *Base10type testExiter struct {11 exited bool12 code int13}14func (exiter *testExiter) Exit(code int) {15 exiter.code = code16 exiter.exited = true17}18var curTestExiter *testExiter19func (s *BaseSuite) SetUpTest(t sweet.T) {20 setFakeCallerInfo("", 0)21 gomolFiles = map[string]fileRecord{}22 curTestExiter = &testExiter{}23 setExiter(curTestExiter)24 clock := glock.NewMockClock()25 testBase = NewBase(26 withClock(clock),27 )28 testBase.AddLogger(newDefaultMemLogger())29 testBase.InitLoggers()30 curDefault = NewBase(31 withClock(clock),32 )33 curDefault.AddLogger(newDefaultMemLogger())34 curDefault.InitLoggers()35}36func (s *BaseSuite) TearDownTest(t sweet.T) {37 curDefault.ShutdownLoggers()38 testBase.ShutdownLoggers()39}40func (s *BaseSuite) TestShouldLog(t sweet.T) {41 b := NewBase()42 b.SetLogLevel(LevelInfo)43 Expect(b.shouldLog(LevelDebug)).To(Equal(false))44 Expect(b.shouldLog(LevelInfo)).To(Equal(true))45 Expect(b.shouldLog(LevelWarning)).To(Equal(true))46 Expect(b.shouldLog(LevelError)).To(Equal(true))47 Expect(b.shouldLog(LevelFatal)).To(Equal(true))48 b.SetLogLevel(LevelFatal)49 Expect(b.shouldLog(LevelDebug)).To(Equal(false))50 Expect(b.shouldLog(LevelInfo)).To(Equal(false))51 Expect(b.shouldLog(LevelWarning)).To(Equal(false))52 Expect(b.shouldLog(LevelError)).To(Equal(false))53 Expect(b.shouldLog(LevelFatal)).To(Equal(true))54 b.SetLogLevel(LevelNone)55 Expect(b.shouldLog(LevelDebug)).To(Equal(false))56 Expect(b.shouldLog(LevelInfo)).To(Equal(false))57 Expect(b.shouldLog(LevelWarning)).To(Equal(false))58 Expect(b.shouldLog(LevelError)).To(Equal(false))59 Expect(b.shouldLog(LevelFatal)).To(Equal(false))60}61func (s *BaseSuite) TestNewBase(t sweet.T) {62 b := NewBase()63 Expect(b.isInitialized).To(Equal(false))64 Expect(b.config).ToNot(BeNil())65 Expect(b.config.FilenameAttr).To(Equal(""))66 Expect(b.config.LineNumberAttr).To(Equal(""))67 Expect(b.logLevel).To(Equal(LevelDebug))68 Expect(b.loggers).To(HaveLen(0))69 Expect(b.BaseAttrs.Attrs()).To(HaveLen(0))70}71func (s *BaseSuite) TestSetConfig(t sweet.T) {72 b := NewBase()73 Expect(b.config).ToNot(BeNil())74 Expect(b.config.FilenameAttr).To(Equal(""))75 Expect(b.config.LineNumberAttr).To(Equal(""))76 cfg := NewConfig()77 cfg.FilenameAttr = "filename"78 cfg.LineNumberAttr = "line_number"79 b.SetConfig(cfg)80 Expect(b.config).ToNot(BeNil())81 Expect(b.config.FilenameAttr).To(Equal("filename"))82 Expect(b.config.LineNumberAttr).To(Equal("line_number"))83}84func (s *BaseSuite) TestErrorChannel(t sweet.T) {85 ch := make(chan error)86 received := make(chan error, 3)87 b := NewBase()88 b.SetErrorChan(ch)89 go func() {90 defer close(received)91 for val := range ch {92 received <- val93 }94 }()95 b.report(errors.New("error1"))96 b.report(errors.New("error2"))97 b.report(errors.New("error3"))98 Eventually(received).Should(Receive(MatchError("error1")))99 Eventually(received).Should(Receive(MatchError("error2")))100 Eventually(received).Should(Receive(MatchError("error3")))101 b.ShutdownLoggers()102 Eventually(ch).Should(BeClosed())103 Eventually(received).Should(BeClosed())104}105func (s *BaseSuite) TestSetLogLevel(t sweet.T) {106 b := NewBase()107 b.InitLoggers()108 ml := newDefaultMemLogger()109 b.AddLogger(ml)110 b.SetLogLevel(LevelWarning)111 b.Dbg("test")112 b.Info("test")113 b.Warn("test")114 b.Err("test")115 b.Fatal("test")116 b.ShutdownLoggers()117 Expect(ml.Messages()).To(HaveLen(3))118}119func (s *BaseSuite) TestAddLogger(t sweet.T) {120 b := NewBase()121 b.InitLoggers()122 Expect(b.loggers).To(HaveLen(0))123 ml := newDefaultMemLogger()124 Expect(ml.IsInitialized()).To(Equal(false))125 Expect(ml.base).To(BeNil())126 b.AddLogger(ml)127 Expect(b.IsInitialized()).To(Equal(true))128 Expect(b.loggers).To(HaveLen(1))129 Expect(b.loggers[0].IsInitialized()).To(Equal(true))130 Expect(ml.base).To(Equal(b))131}132func (s *BaseSuite) TestAddLoggerAfterInit(t sweet.T) {133 b := NewBase()134 b.InitLoggers()135 ml := newDefaultMemLogger()136 Expect(ml.IsInitialized()).To(Equal(false))137 ret := b.AddLogger(ml)138 Expect(ret).To(BeNil())139 Expect(ml.IsInitialized()).To(Equal(true))140}141func (s *BaseSuite) TestAddLoggerAfterShutdown(t sweet.T) {142 b := NewBase()143 ml := newDefaultMemLogger()144 Expect(ml.IsInitialized()).To(Equal(false))145 ml.InitLogger()146 Expect(ml.IsInitialized()).To(Equal(true))147 ret := b.AddLogger(ml)148 Expect(ret).To(BeNil())149 Expect(ml.IsInitialized()).To(Equal(false))150}151func (s *BaseSuite) TestAddLoggerAfterInitFail(t sweet.T) {152 b := NewBase()153 b.InitLoggers()154 mlCfg := newMemLoggerConfig()155 mlCfg.FailInit = true156 ml, err := newMemLogger(mlCfg)157 Expect(err).To(BeNil())158 Expect(ml.IsInitialized()).To(Equal(false))159 ret := b.AddLogger(ml)160 Expect(ret).ToNot(BeNil())161 Expect(ret.Error()).To(Equal("Init failed"))162 Expect(ml.IsInitialized()).To(Equal(false))163 Expect(b.loggers).To(HaveLen(0))164}165func (s *BaseSuite) TestAddLoggerAfterShutdownFail(t sweet.T) {166 b := NewBase()167 mlCfg := newMemLoggerConfig()168 mlCfg.FailShutdown = true169 ml, err := newMemLogger(mlCfg)170 Expect(err).To(BeNil())171 Expect(ml.IsInitialized()).To(Equal(false))172 ml.InitLogger()173 Expect(ml.IsInitialized()).To(Equal(true))174 ret := b.AddLogger(ml)175 Expect(ret).ToNot(BeNil())176 Expect(ret.Error()).To(Equal("Shutdown failed"))177 Expect(ml.IsInitialized()).To(Equal(true))178 Expect(b.loggers).To(HaveLen(0))179}180func (s *BaseSuite) TestBaseRemoveLogger(t sweet.T) {181 b := NewBase()182 ml1 := newDefaultMemLogger()183 ml2 := newDefaultMemLogger()184 ml3 := newDefaultMemLogger()185 b.AddLogger(ml1)186 b.AddLogger(ml2)187 b.AddLogger(ml3)188 b.InitLoggers()189 Expect(ml1.IsInitialized()).To(Equal(true))190 Expect(ml2.IsInitialized()).To(Equal(true))191 Expect(ml3.IsInitialized()).To(Equal(true))192 Expect(b.loggers).To(HaveLen(3))193 err := b.RemoveLogger(ml2)194 Expect(err).To(BeNil())195 Expect(ml1.IsInitialized()).To(Equal(true))196 Expect(ml2.IsInitialized()).To(Equal(false))197 Expect(ml3.IsInitialized()).To(Equal(true))198 Expect(b.loggers).To(HaveLen(2))199}200func (s *BaseSuite) TestBaseRemoveLoggerNonExistent(t sweet.T) {201 b := NewBase()202 ml1 := newDefaultMemLogger()203 ml2 := newDefaultMemLogger()204 b.AddLogger(ml1)205 b.InitLoggers()206 Expect(ml1.IsInitialized()).To(Equal(true))207 Expect(b.loggers).To(HaveLen(1))208 err := b.RemoveLogger(ml2)209 Expect(err).To(BeNil())210}211func (s *BaseSuite) TestBaseClearLoggers(t sweet.T) {212 b := NewBase()213 ml1 := newDefaultMemLogger()214 ml2 := newDefaultMemLogger()215 ml3 := newDefaultMemLogger()216 b.AddLogger(ml1)217 b.AddLogger(ml2)218 b.AddLogger(ml3)219 b.InitLoggers()220 Expect(ml1.IsInitialized()).To(Equal(true))221 Expect(ml2.IsInitialized()).To(Equal(true))222 Expect(ml3.IsInitialized()).To(Equal(true))223 Expect(b.loggers).To(HaveLen(3))224 err := b.ClearLoggers()225 Expect(err).To(BeNil())226 Expect(ml1.IsInitialized()).To(Equal(false))227 Expect(ml2.IsInitialized()).To(Equal(false))228 Expect(ml3.IsInitialized()).To(Equal(false))229 Expect(b.loggers).To(HaveLen(0))230}231func (s *BaseSuite) TestInitLoggers(t sweet.T) {232 b := NewBase()233 Expect(b.IsInitialized()).To(Equal(false))234 Expect(b.queue).To(BeNil())235 ml1 := newDefaultMemLogger()236 ml2 := newDefaultMemLogger()237 b.AddLogger(ml1)238 b.AddLogger(ml2)239 b.InitLoggers()240 Expect(b.queue).ToNot(BeNil())241 Expect(b.IsInitialized()).To(Equal(true))242 Expect(ml1.IsInitialized()).To(Equal(true))243 Expect(ml2.IsInitialized()).To(Equal(true))244}245func (s *BaseSuite) TestInitLoggersTwice(t sweet.T) {246 b := NewBase()247 Expect(b.IsInitialized()).To(Equal(false))248 ml1 := newDefaultMemLogger()249 ml2 := newDefaultMemLogger()250 b.AddLogger(ml1)251 b.AddLogger(ml2)252 b.InitLoggers()253 b.InitLoggers()254 Expect(b.IsInitialized()).To(Equal(true))255 Expect(ml1.IsInitialized()).To(Equal(true))256 Expect(ml2.IsInitialized()).To(Equal(true))257}258func (s *BaseSuite) TestInitLoggersFail(t sweet.T) {259 b := NewBase()260 mlCfg := newMemLoggerConfig()261 mlCfg.FailInit = true262 ml1, err := newMemLogger(mlCfg)263 Expect(err).To(BeNil())264 ml2, err := newMemLogger(mlCfg)265 Expect(err).To(BeNil())266 b.AddLogger(ml1)267 b.AddLogger(ml2)268 err = b.InitLoggers()269 Expect(err).ToNot(BeNil())270 Expect(err.Error()).To(Equal("Init failed"))271 Expect(b.IsInitialized()).To(Equal(false))272 Expect(ml1.IsInitialized()).To(Equal(false))273 Expect(ml2.IsInitialized()).To(Equal(false))274}275func (s *BaseSuite) TestShutdownLoggers(t sweet.T) {276 b := NewBase()277 ml1 := newDefaultMemLogger()278 ml2 := newDefaultMemLogger()279 b.AddLogger(ml1)280 b.AddLogger(ml2)281 b.InitLoggers()282 b.ShutdownLoggers()283 Expect(ml1.isShutdown).To(Equal(true))284 Expect(ml2.isShutdown).To(Equal(true))285}286func (s *BaseSuite) TestShutdownLoggersFail(t sweet.T) {287 b := NewBase()288 mlCfg := newMemLoggerConfig()289 mlCfg.FailShutdown = true290 ml1, err := newMemLogger(mlCfg)291 Expect(err).To(BeNil())292 ml2, err := newMemLogger(mlCfg)293 Expect(err).To(BeNil())294 b.AddLogger(ml1)295 b.AddLogger(ml2)296 b.InitLoggers()297 err = b.ShutdownLoggers()298 Expect(err).ToNot(BeNil())299 Expect(err.Error()).To(Equal("Shutdown failed"))300 Expect(ml1.isShutdown).To(Equal(false))301 Expect(ml2.isShutdown).To(Equal(false))302}303func (s *BaseSuite) TestShutdownLoggersTwice(t sweet.T) {304 b := NewBase()305 ml1 := newDefaultMemLogger()306 ml2 := newDefaultMemLogger()307 b.AddLogger(ml1)308 b.AddLogger(ml2)309 b.InitLoggers()310 b.ShutdownLoggers()311 b.ShutdownLoggers()312 Expect(ml1.isShutdown).To(Equal(true))313 Expect(ml2.isShutdown).To(Equal(true))314}315func (s *BaseSuite) TestLogWhileNotInitialized(t sweet.T) {316 b := NewBase()317 err := b.Log(LevelDebug, nil, "message")318 Expect(err).To(Equal(ErrNotInitialized))319}320func (s *BaseSuite) TestSetAttr(t sweet.T) {321 b := NewBase()322 b.SetAttr("attr1", 1)323 Expect(b.BaseAttrs.Attrs()).To(HaveLen(1))324 Expect(b.BaseAttrs.GetAttr("attr1")).To(Equal(1))325 b.SetAttr("attr2", "val2")326 Expect(b.BaseAttrs.Attrs()).To(HaveLen(2))327 Expect(b.BaseAttrs.GetAttr("attr2")).To(Equal("val2"))328}329func (s *BaseSuite) TestGetAttr(t sweet.T) {330 b := NewBase()331 b.SetAttr("attr1", 1)332 b.SetAttr("attr2", "val2")333 Expect(b.GetAttr("attr2")).To(Equal("val2"))334 Expect(b.GetAttr("notakey")).To(BeNil())335}336func (s *BaseSuite) TestRemoveAttr(t sweet.T) {337 b := NewBase()338 b.SetAttr("attr1", 1)339 Expect(b.BaseAttrs.Attrs()).To(HaveLen(1))340 Expect(b.BaseAttrs.GetAttr("attr1")).To(Equal(1))341 b.RemoveAttr("attr1")342 Expect(b.BaseAttrs.Attrs()).To(HaveLen(0))343}344func (s *BaseSuite) TestClearAttrs(t sweet.T) {345 b := NewBase()346 b.SetAttr("attr1", 1)347 b.SetAttr("attr2", "val2")348 Expect(b.BaseAttrs.Attrs()).To(HaveLen(2))349 b.ClearAttrs()350 Expect(b.BaseAttrs.Attrs()).To(HaveLen(0))351}352func (s *BaseSuite) TestSequenceDisabled(t sweet.T) {353 b := NewBase()354 b.InitLoggers()355 Expect(b.sequence).To(Equal(uint64(0)))356 b.Dbg("test")357 Expect(b.sequence).To(Equal(uint64(0)))358 b.Dbg("test")359 Expect(b.sequence).To(Equal(uint64(0)))360 b.ShutdownLoggers()361}362func (s *BaseSuite) TestSequence(t sweet.T) {363 b := NewBase()364 b.config.SequenceAttr = "seq"365 l := newDefaultMemLogger()366 b.AddLogger(l)367 b.InitLoggers()368 Expect(b.sequence).To(Equal(uint64(0)))369 b.Dbg("test")370 Expect(b.sequence).To(Equal(uint64(1)))371 b.Dbg("test")372 Expect(b.sequence).To(Equal(uint64(2)))373 b.ShutdownLoggers()374 Expect(l.Messages()).To(HaveLen(2))375 msg := l.Messages()[0]376 Expect(msg.Message).To(Equal("test"))377 Expect(msg.Attrs).To(HaveLen(1))378 Expect(msg.Attrs["seq"]).To(Equal(uint64(1)))379 Expect(msg.Level).To(Equal(LevelDebug))380 msg = l.Messages()[1]381 Expect(msg.Message).To(Equal("test"))382 Expect(msg.Attrs).To(HaveLen(1))383 Expect(msg.Attrs["seq"]).To(Equal(uint64(2)))384 Expect(msg.Level).To(Equal(LevelDebug))385}386// Base func tests387func (s *BaseSuite) TestBaseDbgfWithFormattingParams(t sweet.T) {388 b := NewBase()389 l1 := newDefaultMemLogger()390 b.AddLogger(l1)391 b.InitLoggers()392 b.Dbgf("LOG %s", "%2b")393 b.ShutdownLoggers()394 Expect(l1.Messages()).To(HaveLen(1))395 msg := l1.Messages()[0]396 Expect(msg.Message).To(Equal("LOG %2b"))397 Expect(msg.Attrs).To(HaveLen(0))398 Expect(msg.Level).To(Equal(LevelDebug))399}400func (s *BaseSuite) TestBaseDbg(t sweet.T) {401 b := NewBase()402 l1 := newDefaultMemLogger()403 l2 := newDefaultMemLogger()404 b.AddLogger(l1)405 b.AddLogger(l2)406 b.InitLoggers()407 b.Dbg("test")408 b.ShutdownLoggers()409 Expect(l1.Messages()).To(HaveLen(1))410 msg := l1.Messages()[0]411 Expect(msg.Message).To(Equal("test"))412 Expect(msg.Attrs).To(HaveLen(0))413 Expect(msg.Level).To(Equal(LevelDebug))414 Expect(l2.Messages()).To(HaveLen(1))415 msg = l2.Messages()[0]416 Expect(msg.Message).To(Equal("test"))417 Expect(msg.Attrs).To(HaveLen(0))418 Expect(msg.Level).To(Equal(LevelDebug))419}420func (s *BaseSuite) TestBaseDbgf(t sweet.T) {421 b := NewBase()422 l1 := newDefaultMemLogger()423 l2 := newDefaultMemLogger()424 b.AddLogger(l1)425 b.AddLogger(l2)426 b.InitLoggers()427 b.Dbgf("test %v", 1234)428 b.ShutdownLoggers()429 Expect(l1.Messages()).To(HaveLen(1))430 msg := l1.Messages()[0]431 Expect(msg.Message).To(Equal("test 1234"))432 Expect(msg.Attrs).To(HaveLen(0))433 Expect(msg.Level).To(Equal(LevelDebug))434 Expect(l2.Messages()).To(HaveLen(1))435 msg = l2.Messages()[0]436 Expect(msg.Message).To(Equal("test 1234"))437 Expect(msg.Attrs).To(HaveLen(0))438 Expect(msg.Level).To(Equal(LevelDebug))439}440func (s *BaseSuite) TestBaseDbgm(t sweet.T) {441 b := NewBase()442 b.SetAttr("attr1", 1234)443 l1 := newDefaultMemLogger()444 l2 := newDefaultMemLogger()445 b.AddLogger(l1)446 b.AddLogger(l2)447 b.InitLoggers()448 b.Dbgm(449 NewAttrs().450 SetAttr("attr2", 4321).451 SetAttr("attr3", "val3"),452 "test %v",453 1234)454 b.ShutdownLoggers()455 Expect(l1.Messages()).To(HaveLen(1))456 msg := l1.Messages()[0]457 Expect(msg.Message).To(Equal("test 1234"))458 Expect(msg.Attrs).To(HaveLen(3))459 Expect(msg.Attrs["attr1"]).To(Equal(1234))460 Expect(msg.Attrs["attr2"]).To(Equal(4321))461 Expect(msg.Attrs["attr3"]).To(Equal("val3"))462 Expect(msg.Level).To(Equal(LevelDebug))463 Expect(l2.Messages()).To(HaveLen(1))464 msg = l2.Messages()[0]465 Expect(msg.Message).To(Equal("test 1234"))466 Expect(msg.Attrs).To(HaveLen(3))467 Expect(msg.Attrs["attr1"]).To(Equal(1234))468 Expect(msg.Attrs["attr2"]).To(Equal(4321))469 Expect(msg.Attrs["attr3"]).To(Equal("val3"))470 Expect(msg.Level).To(Equal(LevelDebug))471}472func (s *BaseSuite) TestBaseInfo(t sweet.T) {473 b := NewBase()474 l1 := newDefaultMemLogger()475 l2 := newDefaultMemLogger()476 b.AddLogger(l1)477 b.AddLogger(l2)478 b.InitLoggers()479 b.Info("test")480 b.ShutdownLoggers()481 Expect(l1.Messages()).To(HaveLen(1))482 msg := l1.Messages()[0]483 Expect(msg.Message).To(Equal("test"))484 Expect(msg.Attrs).To(HaveLen(0))485 Expect(msg.Level).To(Equal(LevelInfo))486 Expect(l2.Messages()).To(HaveLen(1))487 msg = l2.Messages()[0]488 Expect(msg.Message).To(Equal("test"))489 Expect(msg.Attrs).To(HaveLen(0))490 Expect(msg.Level).To(Equal(LevelInfo))491}492func (s *BaseSuite) TestBaseInfof(t sweet.T) {493 b := NewBase()494 l1 := newDefaultMemLogger()495 l2 := newDefaultMemLogger()496 b.AddLogger(l1)497 b.AddLogger(l2)498 b.InitLoggers()499 b.Infof("test %v", 1234)500 b.ShutdownLoggers()501 Expect(l1.Messages()).To(HaveLen(1))502 msg := l1.Messages()[0]503 Expect(msg.Message).To(Equal("test 1234"))504 Expect(msg.Attrs).To(HaveLen(0))505 Expect(msg.Level).To(Equal(LevelInfo))506 Expect(l2.Messages()).To(HaveLen(1))507 msg = l2.Messages()[0]508 Expect(msg.Message).To(Equal("test 1234"))509 Expect(msg.Attrs).To(HaveLen(0))510 Expect(msg.Level).To(Equal(LevelInfo))511}512func (s *BaseSuite) TestBaseInfom(t sweet.T) {513 b := NewBase()514 b.SetAttr("attr1", 1234)515 l1 := newDefaultMemLogger()516 l2 := newDefaultMemLogger()517 b.AddLogger(l1)518 b.AddLogger(l2)519 b.InitLoggers()520 b.Infom(521 NewAttrs().522 SetAttr("attr2", 4321).523 SetAttr("attr3", "val3"),524 "test %v",525 1234)526 b.ShutdownLoggers()527 Expect(l1.Messages()).To(HaveLen(1))528 msg := l1.Messages()[0]529 Expect(msg.Message).To(Equal("test 1234"))530 Expect(msg.Attrs).To(HaveLen(3))531 Expect(msg.Attrs["attr1"]).To(Equal(1234))532 Expect(msg.Attrs["attr2"]).To(Equal(4321))533 Expect(msg.Attrs["attr3"]).To(Equal("val3"))534 Expect(msg.Level).To(Equal(LevelInfo))535 Expect(l2.Messages()).To(HaveLen(1))536 msg = l2.Messages()[0]537 Expect(msg.Message).To(Equal("test 1234"))538 Expect(msg.Attrs).To(HaveLen(3))539 Expect(msg.Attrs["attr1"]).To(Equal(1234))540 Expect(msg.Attrs["attr2"]).To(Equal(4321))541 Expect(msg.Attrs["attr3"]).To(Equal("val3"))542 Expect(msg.Level).To(Equal(LevelInfo))543}544func (s *BaseSuite) TestBaseWarn(t sweet.T) {545 b := NewBase()546 l1 := newDefaultMemLogger()547 l2 := newDefaultMemLogger()548 b.AddLogger(l1)549 b.AddLogger(l2)550 b.InitLoggers()551 b.Warn("test")552 b.ShutdownLoggers()553 Expect(l1.Messages()).To(HaveLen(1))554 msg := l1.Messages()[0]555 Expect(msg.Message).To(Equal("test"))556 Expect(msg.Attrs).To(HaveLen(0))557 Expect(msg.Level).To(Equal(LevelWarning))558 Expect(l2.Messages()).To(HaveLen(1))559 msg = l2.Messages()[0]560 Expect(msg.Message).To(Equal("test"))561 Expect(msg.Attrs).To(HaveLen(0))562 Expect(msg.Level).To(Equal(LevelWarning))563}564func (s *BaseSuite) TestBaseWarnf(t sweet.T) {565 b := NewBase()566 l1 := newDefaultMemLogger()567 l2 := newDefaultMemLogger()568 b.AddLogger(l1)569 b.AddLogger(l2)570 b.InitLoggers()571 b.Warnf("test %v", 1234)572 b.ShutdownLoggers()573 Expect(l1.Messages()).To(HaveLen(1))574 msg := l1.Messages()[0]575 Expect(msg.Message).To(Equal("test 1234"))576 Expect(msg.Attrs).To(HaveLen(0))577 Expect(msg.Level).To(Equal(LevelWarning))578 Expect(l2.Messages()).To(HaveLen(1))579 msg = l2.Messages()[0]580 Expect(msg.Message).To(Equal("test 1234"))581 Expect(msg.Attrs).To(HaveLen(0))582 Expect(msg.Level).To(Equal(LevelWarning))583}584func (s *BaseSuite) TestBaseWarnm(t sweet.T) {585 b := NewBase()586 b.SetAttr("attr1", 1234)587 l1 := newDefaultMemLogger()588 l2 := newDefaultMemLogger()589 b.AddLogger(l1)590 b.AddLogger(l2)591 b.InitLoggers()592 b.Warnm(593 NewAttrs().594 SetAttr("attr2", 4321).595 SetAttr("attr3", "val3"),596 "test %v",597 1234)598 b.ShutdownLoggers()599 Expect(l1.Messages()).To(HaveLen(1))600 msg := l1.Messages()[0]601 Expect(msg.Message).To(Equal("test 1234"))602 Expect(msg.Attrs).To(HaveLen(3))603 Expect(msg.Attrs["attr1"]).To(Equal(1234))604 Expect(msg.Attrs["attr2"]).To(Equal(4321))605 Expect(msg.Attrs["attr3"]).To(Equal("val3"))606 Expect(msg.Level).To(Equal(LevelWarning))607 Expect(l2.Messages()).To(HaveLen(1))608 msg = l2.Messages()[0]609 Expect(msg.Message).To(Equal("test 1234"))610 Expect(msg.Attrs).To(HaveLen(3))611 Expect(msg.Attrs["attr1"]).To(Equal(1234))612 Expect(msg.Attrs["attr2"]).To(Equal(4321))613 Expect(msg.Attrs["attr3"]).To(Equal("val3"))614 Expect(msg.Level).To(Equal(LevelWarning))615}616func (s *BaseSuite) TestBaseErr(t sweet.T) {617 b := NewBase()618 l1 := newDefaultMemLogger()619 l2 := newDefaultMemLogger()620 b.AddLogger(l1)621 b.AddLogger(l2)622 b.InitLoggers()623 b.Err("test")624 b.ShutdownLoggers()625 Expect(l1.Messages()).To(HaveLen(1))626 msg := l1.Messages()[0]627 Expect(msg.Message).To(Equal("test"))628 Expect(msg.Attrs).To(HaveLen(0))629 Expect(msg.Level).To(Equal(LevelError))630 Expect(l2.Messages()).To(HaveLen(1))631 msg = l2.Messages()[0]632 Expect(msg.Message).To(Equal("test"))633 Expect(msg.Attrs).To(HaveLen(0))634 Expect(msg.Level).To(Equal(LevelError))635}636func (s *BaseSuite) TestBaseErrf(t sweet.T) {637 b := NewBase()638 l1 := newDefaultMemLogger()639 l2 := newDefaultMemLogger()640 b.AddLogger(l1)641 b.AddLogger(l2)642 b.InitLoggers()643 b.Errf("test %v", 1234)644 b.ShutdownLoggers()645 Expect(l1.Messages()).To(HaveLen(1))646 msg := l1.Messages()[0]647 Expect(msg.Message).To(Equal("test 1234"))648 Expect(msg.Attrs).To(HaveLen(0))649 Expect(msg.Level).To(Equal(LevelError))650 Expect(l2.Messages()).To(HaveLen(1))651 msg = l2.Messages()[0]652 Expect(msg.Message).To(Equal("test 1234"))653 Expect(msg.Attrs).To(HaveLen(0))654 Expect(msg.Level).To(Equal(LevelError))655}656func (s *BaseSuite) TestBaseErrm(t sweet.T) {657 b := NewBase()658 b.SetAttr("attr1", 1234)659 l1 := newDefaultMemLogger()660 l2 := newDefaultMemLogger()661 b.AddLogger(l1)662 b.AddLogger(l2)663 b.InitLoggers()664 b.Errm(665 NewAttrs().666 SetAttr("attr2", 4321).667 SetAttr("attr3", "val3"),668 "test %v",669 1234)670 b.ShutdownLoggers()671 Expect(l1.Messages()).To(HaveLen(1))672 msg := l1.Messages()[0]673 Expect(msg.Message).To(Equal("test 1234"))674 Expect(msg.Attrs).To(HaveLen(3))675 Expect(msg.Attrs["attr1"]).To(Equal(1234))676 Expect(msg.Attrs["attr2"]).To(Equal(4321))677 Expect(msg.Attrs["attr3"]).To(Equal("val3"))678 Expect(msg.Level).To(Equal(LevelError))679 Expect(l2.Messages()).To(HaveLen(1))680 msg = l2.Messages()[0]681 Expect(msg.Message).To(Equal("test 1234"))682 Expect(msg.Attrs).To(HaveLen(3))683 Expect(msg.Attrs["attr1"]).To(Equal(1234))684 Expect(msg.Attrs["attr2"]).To(Equal(4321))685 Expect(msg.Attrs["attr3"]).To(Equal("val3"))686 Expect(msg.Level).To(Equal(LevelError))687}688func (s *BaseSuite) TestBaseFatal(t sweet.T) {689 b := NewBase()690 l1 := newDefaultMemLogger()691 l2 := newDefaultMemLogger()692 b.AddLogger(l1)693 b.AddLogger(l2)694 b.InitLoggers()695 b.Fatal("test")696 b.ShutdownLoggers()697 Expect(l1.Messages()).To(HaveLen(1))698 msg := l1.Messages()[0]699 Expect(msg.Message).To(Equal("test"))700 Expect(msg.Attrs).To(HaveLen(0))701 Expect(msg.Level).To(Equal(LevelFatal))702 Expect(l2.Messages()).To(HaveLen(1))703 msg = l2.Messages()[0]704 Expect(msg.Message).To(Equal("test"))705 Expect(msg.Attrs).To(HaveLen(0))706 Expect(msg.Level).To(Equal(LevelFatal))707}708func (s *BaseSuite) TestBaseFatalf(t sweet.T) {709 b := NewBase()710 l1 := newDefaultMemLogger()711 l2 := newDefaultMemLogger()712 b.AddLogger(l1)713 b.AddLogger(l2)714 b.InitLoggers()715 b.Fatalf("test %v", 1234)716 b.ShutdownLoggers()717 Expect(l1.Messages()).To(HaveLen(1))718 msg := l1.Messages()[0]719 Expect(msg.Message).To(Equal("test 1234"))720 Expect(msg.Attrs).To(HaveLen(0))721 Expect(msg.Level).To(Equal(LevelFatal))722 Expect(l2.Messages()).To(HaveLen(1))723 msg = l2.Messages()[0]724 Expect(msg.Message).To(Equal("test 1234"))725 Expect(msg.Attrs).To(HaveLen(0))726 Expect(msg.Level).To(Equal(LevelFatal))727}728func (s *BaseSuite) TestBaseFatalm(t sweet.T) {729 b := NewBase()730 b.SetAttr("attr1", 1234)731 l1 := newDefaultMemLogger()732 l2 := newDefaultMemLogger()733 b.AddLogger(l1)734 b.AddLogger(l2)735 b.InitLoggers()736 b.Fatalm(737 NewAttrs().738 SetAttr("attr2", 4321).739 SetAttr("attr3", "val3"),740 "test %v",741 1234)742 b.ShutdownLoggers()743 Expect(l1.Messages()).To(HaveLen(1))744 msg := l1.Messages()[0]745 Expect(msg.Message).To(Equal("test 1234"))746 Expect(msg.Attrs).To(HaveLen(3))747 Expect(msg.Attrs["attr1"]).To(Equal(1234))748 Expect(msg.Attrs["attr2"]).To(Equal(4321))749 Expect(msg.Attrs["attr3"]).To(Equal("val3"))750 Expect(msg.Level).To(Equal(LevelFatal))751 Expect(l2.Messages()).To(HaveLen(1))752 msg = l2.Messages()[0]753 Expect(msg.Message).To(Equal("test 1234"))754 Expect(msg.Attrs).To(HaveLen(3))755 Expect(msg.Attrs["attr1"]).To(Equal(1234))756 Expect(msg.Attrs["attr2"]).To(Equal(4321))757 Expect(msg.Attrs["attr3"]).To(Equal("val3"))758 Expect(msg.Level).To(Equal(LevelFatal))759}760func (s *BaseSuite) TestBaseDie(t sweet.T) {761 b := NewBase()762 l1 := newDefaultMemLogger()763 l2 := newDefaultMemLogger()764 b.AddLogger(l1)765 b.AddLogger(l2)766 b.InitLoggers()767 b.Die(1234, "test")768 Expect(l1.Messages()).To(HaveLen(1))769 msg := l1.Messages()[0]770 Expect(msg.Message).To(Equal("test"))771 Expect(msg.Attrs).To(HaveLen(0))772 Expect(msg.Level).To(Equal(LevelFatal))773 Expect(l2.Messages()).To(HaveLen(1))774 msg = l2.Messages()[0]775 Expect(msg.Message).To(Equal("test"))776 Expect(msg.Attrs).To(HaveLen(0))777 Expect(msg.Level).To(Equal(LevelFatal))778 Expect(b.isInitialized).To(Equal(false))779 Expect(curTestExiter.exited).To(Equal(true))780 Expect(curTestExiter.code).To(Equal(1234))781}782func (s *BaseSuite) TestBaseDief(t sweet.T) {783 b := NewBase()784 l1 := newDefaultMemLogger()785 l2 := newDefaultMemLogger()786 b.AddLogger(l1)787 b.AddLogger(l2)788 b.InitLoggers()789 b.Dief(1234, "test %v", 1234)790 Expect(l1.Messages()).To(HaveLen(1))791 msg := l1.Messages()[0]792 Expect(msg.Message).To(Equal("test 1234"))793 Expect(msg.Attrs).To(HaveLen(0))794 Expect(msg.Level).To(Equal(LevelFatal))795 Expect(l2.Messages()).To(HaveLen(1))796 msg = l2.Messages()[0]797 Expect(msg.Message).To(Equal("test 1234"))798 Expect(msg.Attrs).To(HaveLen(0))799 Expect(msg.Level).To(Equal(LevelFatal))800 Expect(b.isInitialized).To(Equal(false))801 Expect(curTestExiter.exited).To(Equal(true))802 Expect(curTestExiter.code).To(Equal(1234))803}804func (s *BaseSuite) TestBaseDiem(t sweet.T) {805 b := NewBase()806 b.SetAttr("attr1", 1234)807 l1 := newDefaultMemLogger()808 l2 := newDefaultMemLogger()809 b.AddLogger(l1)810 b.AddLogger(l2)811 b.InitLoggers()812 b.Diem(813 1234,814 NewAttrs().815 SetAttr("attr2", 4321).816 SetAttr("attr3", "val3"),817 "test %v",818 1234)819 b.ShutdownLoggers()820 Expect(l1.Messages()).To(HaveLen(1))821 msg := l1.Messages()[0]822 Expect(msg.Message).To(Equal("test 1234"))823 Expect(msg.Attrs).To(HaveLen(3))824 Expect(msg.Attrs["attr1"]).To(Equal(1234))825 Expect(msg.Attrs["attr2"]).To(Equal(4321))826 Expect(msg.Attrs["attr3"]).To(Equal("val3"))827 Expect(msg.Level).To(Equal(LevelFatal))828 Expect(l2.Messages()).To(HaveLen(1))829 msg = l2.Messages()[0]830 Expect(msg.Message).To(Equal("test 1234"))831 Expect(msg.Attrs).To(HaveLen(3))832 Expect(msg.Attrs["attr1"]).To(Equal(1234))833 Expect(msg.Attrs["attr2"]).To(Equal(4321))834 Expect(msg.Attrs["attr3"]).To(Equal("val3"))835 Expect(msg.Level).To(Equal(LevelFatal))836 Expect(b.isInitialized).To(Equal(false))837 Expect(curTestExiter.exited).To(Equal(true))838 Expect(curTestExiter.code).To(Equal(1234))839}840func (s *BaseSuite) TestBaseOrdering(t sweet.T) {841 b := NewBase()842 b.SetAttr("attr1", 1234)843 l1 := newDefaultMemLogger()844 l2 := newDefaultMemLogger()845 b.AddLogger(l1)846 b.AddLogger(l2)847 b.InitLoggers()848 b.Fatalm(849 NewAttrs().850 SetAttr("attr2", 4321).851 SetAttr("attr3", "val3"),852 "test %v",853 1234)854 b.Fatalm(855 NewAttrs().856 SetAttr("attr4", 4321).857 SetAttr("attr5", "val3"),858 "test %v",859 4321)860 b.ShutdownLoggers()861 Expect(l1.Messages()).To(HaveLen(2))862 msg := l1.Messages()[0]863 Expect(msg.Message).To(Equal("test 1234"))864 Expect(msg.Attrs).To(HaveLen(3))865 Expect(msg.Attrs["attr1"]).To(Equal(1234))866 Expect(msg.Attrs["attr2"]).To(Equal(4321))867 Expect(msg.Attrs["attr3"]).To(Equal("val3"))868 Expect(msg.Level).To(Equal(LevelFatal))869 msg = l1.Messages()[1]870 Expect(msg.Message).To(Equal("test 4321"))871 Expect(msg.Attrs).To(HaveLen(3))872 Expect(msg.Attrs["attr1"]).To(Equal(1234))873 Expect(msg.Attrs["attr4"]).To(Equal(4321))874 Expect(msg.Attrs["attr5"]).To(Equal("val3"))875 Expect(msg.Level).To(Equal(LevelFatal))876 Expect(l2.Messages()).To(HaveLen(2))877 msg = l2.Messages()[0]878 Expect(msg.Message).To(Equal("test 1234"))879 Expect(msg.Attrs).To(HaveLen(3))880 Expect(msg.Attrs["attr1"]).To(Equal(1234))881 Expect(msg.Attrs["attr2"]).To(Equal(4321))882 Expect(msg.Attrs["attr3"]).To(Equal("val3"))883 Expect(msg.Level).To(Equal(LevelFatal))884 msg = l2.Messages()[1]885 Expect(msg.Message).To(Equal("test 4321"))886 Expect(msg.Attrs).To(HaveLen(3))887 Expect(msg.Attrs["attr1"]).To(Equal(1234))888 Expect(msg.Attrs["attr4"]).To(Equal(4321))889 Expect(msg.Attrs["attr5"]).To(Equal("val3"))890 Expect(msg.Level).To(Equal(LevelFatal))891}...
minilog_test.go
Source:minilog_test.go
1// Copyright (2012) Sandia Corporation.2// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,3// the U.S. Government retains certain rights in this software.4package minilog5import (6 "bytes"7 "io"8 "os"9 "strings"10 "sync"11 "testing"12 "time"13)14func TestFilter(t *testing.T) {15 sink1 := new(bytes.Buffer)16 AddLogger("sink1Level", sink1, DEBUG, false)17 testString := "test 123"18 testString2 := "test 456"19 Debugln(testString)20 s1 := sink1.String()21 if !strings.Contains(s1, testString) {22 t.Fatal("sink1 got:", s1)23 }24 AddFilter("sink1Level", "minilog_test")25 Debugln(testString2)26 s1 = sink1.String()27 if strings.Contains(s1, testString2) {28 t.Fatal("sink1 got:", s1)29 }30 DelFilter("sink1Level", "minilog_test")31 Debugln(testString2)32 s1 = sink1.String()33 if !strings.Contains(s1, testString2) {34 t.Fatal("sink1 got:", s1)35 }36}37func TestMultilog(t *testing.T) {38 sink1 := new(bytes.Buffer)39 sink2 := new(bytes.Buffer)40 AddLogger("sink1", sink1, DEBUG, false)41 AddLogger("sink2", sink2, DEBUG, false)42 testString := "test 123"43 Debugln(testString)44 s1 := sink1.String()45 s2 := sink2.String()46 if !strings.Contains(s1, testString) {47 t.Fatal("sink1 got:", s1)48 }49 if !strings.Contains(s2, testString) {50 t.Fatal("sink2 got:", s2)51 }52}53func TestLogLevels(t *testing.T) {54 sink1 := new(bytes.Buffer)55 sink2 := new(bytes.Buffer)56 AddLogger("sink1Level", sink1, DEBUG, false)57 AddLogger("sink2Level", sink2, INFO, false)58 testString := "test 123"59 Debugln(testString)60 s1 := sink1.String()61 s2 := sink2.String()62 if !strings.Contains(s1, testString) {63 t.Fatal("sink1 got:", s1)64 }65 if len(s2) != 0 {66 t.Fatal("sink2 got:", s2)67 }68}69func TestDelLogger(t *testing.T) {70 sink := new(bytes.Buffer)71 AddLogger("sinkDel", sink, DEBUG, false)72 testString := "test 123"73 testString2 := "test 456"74 Debug(testString)75 s, err := sink.ReadString('\n')76 if err != nil {77 t.Fatal(err)78 }79 if !strings.Contains(s, testString) {80 t.Fatal("sink got:", s)81 }82 DelLogger("sinkDel")83 Debug(testString2)84 s, err = sink.ReadString('\n')85 if err != nil && err != io.EOF {86 t.Fatal(err)87 }88 if len(s) != 0 {89 t.Fatal("sink got:", s)90 }91}92func TestLogAll(t *testing.T) {93 sink := new(bytes.Buffer)94 source := bytes.NewBufferString("line_1\nline_2\nline_3")95 AddLogger("sinkAll", sink, DEBUG, false)96 LogAll(source, DEBUG, "test")97 time.Sleep(1 * time.Second) // allow the LogAll goroutine to finish98 // we should see only three lines on the logger output99 l1, err := sink.ReadString('\n')100 if err != nil {101 t.Fatal(err)102 }103 if !strings.Contains(l1, "line_1") {104 t.Fatal("sink got:", l1)105 }106 l2, err := sink.ReadString('\n')107 if err != nil {108 t.Fatal(err)109 }110 if !strings.Contains(l2, "line_2") {111 t.Fatal("sink got:", l2)112 }113 l3, err := sink.ReadString('\n')114 if err != nil {115 t.Fatal(err, l3)116 }117 if !strings.Contains(l3, "line_3") {118 t.Fatal("sink got:", l3)119 }120 oops, err := sink.ReadString('\n')121 if err != io.EOF {122 t.Fatal(err, oops)123 }124}125func BenchmarkLogging(b *testing.B) {126 null, err := os.Create(os.DevNull)127 if err != nil {128 b.Fatal(err)129 }130 AddLogger("null", null, DEBUG, false)131 defer DelLogger("null")132 // waitgroup for logging completed133 var wg sync.WaitGroup134 // Create a bunch of goroutines, firing off b.N messages each135 for i := 0; i < 10; i++ {136 wg.Add(1)137 go func(i int) {138 defer wg.Done()139 for j := 0; j < b.N; j++ {140 log(DEBUG, "", "message from %v: %v/%v", i, j, b.N)141 }142 }(i)143 }144 wg.Wait()145}...
log.go
Source:log.go
1/*2 * Copyright (C) 2019 Intel Corporation3 * SPDX-License-Identifier: BSD-3-Clause4 */5package log6import (7 "runtime"8 "strings"9 "intel/isecl/lib/common/v4/log/setup"10 log "github.com/sirupsen/logrus"11)12var ErrLoggerExists = setup.ErrLoggerExists13const (14 unknownLoggerName = "unknown"15 DefaultLoggerName = "default"16 SecurityLoggerName = "security"17)18var defaultLogger *log.Entry19var securityLogger *log.Entry20func init() {21 setup.AddLogger(DefaultLoggerName, "name", log.StandardLogger())22 setup.AddLogger(SecurityLoggerName, "name", log.New())23 setup.AddLogger(unknownLoggerName, "package", log.StandardLogger())24}25func AddLogger(name string, l *log.Logger) error {26 return setup.AddLogger(name, "name", l)27}28func AddLoggerByPackageName() (*log.Entry, string) {29 pc := make([]uintptr, 2)30 runtime.Callers(2, pc)31 f := runtime.FuncForPC(pc[0])32 pkgName := strings.Split(f.Name(), ".")[0]33 setup.AddLogger(pkgName, "package", log.StandardLogger())34 return setup.GetLogger(pkgName), pkgName35}36func GetLogger(name string) *log.Entry {37 if name == "" {38 return setup.GetLogger(unknownLoggerName)39 }40 return setup.GetLogger(name)41}42func GetDefaultLogger() *log.Entry {43 if defaultLogger == nil {44 defaultLogger = setup.GetLogger(DefaultLoggerName)45 }46 return defaultLogger47}48func GetSecurityLogger() *log.Entry {49 if securityLogger == nil {50 securityLogger = setup.GetLogger(SecurityLoggerName)51 }52 return securityLogger53}54// GetFuncName returns the name of the calling function or code block55func GetFuncName() string {56 pc := make([]uintptr, 15)57 n := runtime.Callers(2, pc)58 frames := runtime.CallersFrames(pc[:n])59 frame, _ := frames.Next()60 return frame.Function61}...
addLogger
Using AI Code Generation
1import (2func main() {3 f, err := os.Create("log.txt")4 if err != nil {5 fmt.Println(err)6 }7 defer f.Close()8 log.SetOutput(f)9 log.Println("This is a normal message.")10 log.Fatalln("This is a fatal message.")11 log.Panicln("This is a panic message.")12}
addLogger
Using AI Code Generation
1import (2func main() {3 file, err := os.Create("log.txt")4 if err != nil {5 fmt.Println(err)6 }7 log.SetOutput(file)8 log.Println("This is a log entry")9}10import (11func main() {12 file, err := os.Create("log.txt")13 if err != nil {14 fmt.Println(err)15 }16 log.SetOutput(file)17 log.Println("This is a log entry")18 log.Println("This is a log entry")19 log.Println("This is a log entry")20}21import (22func main() {23 file, err := os.Create("log.txt")24 if err != nil {25 fmt.Println(err)26 }27 log.SetOutput(file
addLogger
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 log.Println("Hello World")5}6import (7func main() {8 fmt.Println("Hello World")9 log.Println("Hello World")10}11import (12func main() {13 fmt.Println("Hello World")14 log.Println("Hello World")15}16import (17func main() {18 fmt.Println("Hello World")19 log.Println("Hello World")20}21import (22func main() {23 fmt.Println("Hello World")24 log.Println("Hello World")25}26import (27func main() {28 fmt.Println("Hello World")29 log.Println("Hello World")30}31import (32func main() {33 fmt.Println("Hello World")34 log.Println("Hello World")35}36import (37func main() {38 fmt.Println("Hello World")39 log.Println("Hello World")40}41import (42func main() {43 fmt.Println("Hello World")44 log.Println("Hello World")45}46import (47func main() {48 fmt.Println("Hello World")49 log.Println("Hello World")50}
addLogger
Using AI Code Generation
1import "logger"2func main() {3 logger.AddLogger("log.txt")4 logger.Log("Hello World")5}6import "logger"7func main() {8 logger.AddLogger("log2.txt")9 logger.Log("Hello World")10}11import "logger"12func main() {13 logger.AddLogger("log3.txt")14 logger.Log("Hello World")15}16import "logger"17func main() {18 logger.AddLogger("log4.txt")19 logger.Log("Hello World")20}21import "logger"22func main() {23 logger.AddLogger("log5.txt")24 logger.Log("Hello World")25}26import "logger"27func main() {28 logger.AddLogger("log6.txt")29 logger.Log("Hello World")30}31import "logger"32func main() {33 logger.AddLogger("log7.txt")34 logger.Log("Hello World")35}36import "logger"37func main() {38 logger.AddLogger("log8.txt")39 logger.Log("Hello World")40}41import "logger"42func main() {43 logger.AddLogger("log9.txt")44 logger.Log("Hello World")45}46import "logger"47func main() {48 logger.AddLogger("log10.txt")49 logger.Log("Hello World")50}51import "logger"52func main() {53 logger.AddLogger("log11.txt")54 logger.Log("Hello World")55}
addLogger
Using AI Code Generation
1import "log"2import "fmt"3func main() {4 fmt.Println("Hello, playground")5 log.Println("Hello, playground")6}7import "log"8import "fmt"9func main() {10 fmt.Println("Hello, playground")11 log.Println("Hello, playground")12}13import "log"14import "fmt"15func main() {16 fmt.Println("Hello, playground")17 log.Println("Hello, playground")18}19import "log"20import "fmt"21func main() {22 fmt.Println("Hello, playground")23 log.Println("Hello, playground")24}25import "log"26import "fmt"27func main() {28 fmt.Println("Hello, playground")29 log.Println("Hello, playground")30}31import "log"32import "fmt"33func main() {34 fmt.Println("Hello, playground")35 log.Println("Hello, playground")36}37import "log"38import "fmt"39func main() {40 fmt.Println("Hello, playground")41 log.Println("Hello, playground")42}43import "log"44import "fmt"45func main() {46 fmt.Println("Hello, playground")47 log.Println("Hello, playground")48}49import "log"50import "fmt"51func main() {52 fmt.Println("Hello, playground")53 log.Println("Hello, playground")54}55import "log"56import "fmt"57func main() {58 fmt.Println("Hello, playground")59 log.Println("Hello, playground")60}
addLogger
Using AI Code Generation
1import "logger"2func main() {3 logger.addLogger()4}5func addLogger() {6}7import "testing"8func TestAddLogger(t *testing.T) {9}10--- PASS: TestAddLogger (0.00s)11--- PASS: TestAddLogger (0.00s)
addLogger
Using AI Code Generation
1import (2func main() {3 file, err = os.OpenFile("test.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)4 if err != nil {5 log.Fatal(err)6 }7 log.SetOutput(file)8 log.Println("This is a test log entry")9 fmt.Println("Check the contents of the file test.txt in the directory")10}11import (12func main() {13 file, err = os.OpenFile("test.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)14 if err != nil {15 log.Fatal(err)16 }17 log.SetOutput(file)18 log.Println("This is a test log entry")19 fmt.Println("Check the contents of the file test.txt in the directory")20}
addLogger
Using AI Code Generation
1import "logger"2func main() {3 logger := logger.NewLogger()4 logger.AddLogger("file")5 logger.AddLogger("console")6 logger.Log("Hello")7}8import "logger"9func main() {10 logger := logger.NewLogger()11 logger.AddLogger("file")12 logger.AddLogger("console")13 logger.Log("Hello")14}15import "logger"16func main() {17 logger := logger.NewLogger()18 logger.AddLogger("file")19 logger.AddLogger("console")20 logger.Log("Hello")21}22import "logger"23func main() {24 logger := logger.NewLogger()25 logger.AddLogger("file")26 logger.AddLogger("console")27 logger.Log("Hello")28}29import "logger"30func main() {31 logger := logger.NewLogger()32 logger.AddLogger("file")33 logger.AddLogger("console")34 logger.Log("Hello")35}36import "logger"37func main() {38 logger := logger.NewLogger()39 logger.AddLogger("file")40 logger.AddLogger("console")41 logger.Log("Hello")42}43import "logger"44func main() {45 logger := logger.NewLogger()46 logger.AddLogger("file")47 logger.AddLogger("console")48 logger.Log("Hello")49}50import "logger"51func main() {52 logger := logger.NewLogger()53 logger.AddLogger("file")54 logger.AddLogger("console")55 logger.Log("Hello")56}57import "logger"58func main() {59 logger := logger.NewLogger()60 logger.AddLogger("file")61 logger.AddLogger("console")62 logger.Log("Hello")63}
addLogger
Using AI Code Generation
1import (2func main() {3 obj := logger.NewLogger()4 obj.AddLogger("file", "log.txt")5 obj.Log("file", "hello world")6}7import (8type Logger struct {9}10func NewLogger() *Logger {11 return &Logger{12 loggers: make(map[string]*os.File),13 }14}15func (l *Logger) AddLogger(name, filePath string) {16 file, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)17 if err != nil {18 panic(err)19 }20}21func (l *Logger) Log(name, message string) {22 if !ok {23 panic("logger not found")24 }25 _, err := fmt.Fprintln(file, message)26 if err != nil {27 panic(err)28 }29}30func (l *Logger) Close() {31 for _, file := range l.loggers {32 err := file.Close()33 if err != nil {34 panic(err)35 }36 }37}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!