func (s *writerSuite) TestWritingLimitTrace(c *gc.C) { writer := &loggo.TestWriter{} err := loggo.RegisterWriter("test", writer, loggo.TRACE) c.Assert(err, gc.IsNil) start := time.Now() s.logger.Criticalf("Something critical.") s.logger.Errorf("An error.") s.logger.Warningf("A warning message") s.logger.Infof("Info message") s.logger.Tracef("Trace the function") end := time.Now() c.Assert(writer.Log, gc.HasLen, 5) c.Assert(writer.Log[0].Level, gc.Equals, loggo.CRITICAL) c.Assert(writer.Log[0].Message, gc.Equals, "Something critical.") c.Assert(writer.Log[0].Timestamp, Between(start, end)) c.Assert(writer.Log[1].Level, gc.Equals, loggo.ERROR) c.Assert(writer.Log[1].Message, gc.Equals, "An error.") c.Assert(writer.Log[1].Timestamp, Between(start, end)) c.Assert(writer.Log[2].Level, gc.Equals, loggo.WARNING) c.Assert(writer.Log[2].Message, gc.Equals, "A warning message") c.Assert(writer.Log[2].Timestamp, Between(start, end)) c.Assert(writer.Log[3].Level, gc.Equals, loggo.INFO) c.Assert(writer.Log[3].Message, gc.Equals, "Info message") c.Assert(writer.Log[3].Timestamp, Between(start, end)) c.Assert(writer.Log[4].Level, gc.Equals, loggo.TRACE) c.Assert(writer.Log[4].Message, gc.Equals, "Trace the function") c.Assert(writer.Log[4].Timestamp, Between(start, end)) }
func (s *logwriterSuite) SetUpTest(c *gc.C) { loggo.ResetLoggers() loggo.RemoveWriter("default") s.writer = &loggo.TestWriter{} err := loggo.RegisterWriter("test", s.writer, loggo.TRACE) c.Assert(err, gc.IsNil) s.logger = loggo.GetLogger("test.writer") // Make it so the logger itself writes all messages. s.logger.SetLogLevel(loggo.TRACE) }
func setupTempFileWriter(c *gc.C) (logFile *os.File, cleanup func()) { loggo.RemoveWriter("test") logFile, err := ioutil.TempFile("", "loggo-test") c.Assert(err, gc.IsNil) cleanup = func() { logFile.Close() os.Remove(logFile.Name()) } writer := loggo.NewSimpleWriter(logFile, &loggo.DefaultFormatter{}) err = loggo.RegisterWriter("testfile", writer, loggo.TRACE) c.Assert(err, gc.IsNil) return }
func (s *writerSuite) TestMultipleWriters(c *gc.C) { errorWriter := &loggo.TestWriter{} err := loggo.RegisterWriter("error", errorWriter, loggo.ERROR) c.Assert(err, gc.IsNil) warningWriter := &loggo.TestWriter{} err = loggo.RegisterWriter("warning", warningWriter, loggo.WARNING) c.Assert(err, gc.IsNil) infoWriter := &loggo.TestWriter{} err = loggo.RegisterWriter("info", infoWriter, loggo.INFO) c.Assert(err, gc.IsNil) traceWriter := &loggo.TestWriter{} err = loggo.RegisterWriter("trace", traceWriter, loggo.TRACE) c.Assert(err, gc.IsNil) s.logger.Errorf("An error.") s.logger.Warningf("A warning message") s.logger.Infof("Info message") s.logger.Tracef("Trace the function") c.Assert(errorWriter.Log, gc.HasLen, 1) c.Assert(warningWriter.Log, gc.HasLen, 2) c.Assert(infoWriter.Log, gc.HasLen, 3) c.Assert(traceWriter.Log, gc.HasLen, 4) }
func (s *writerSuite) TestWritingCapturesFileAndLineAndModule(c *gc.C) { writer := &loggo.TestWriter{} err := loggo.RegisterWriter("test", writer, loggo.INFO) c.Assert(err, gc.IsNil) s.logger.Infof("Info message") // WARNING: test checks the line number of the above logger lines, this // will mean that if the above line moves, the test will fail unless // updated. c.Assert(writer.Log, gc.HasLen, 1) c.Assert(writer.Log[0].Filename, gc.Equals, "writer_test.go") c.Assert(writer.Log[0].Line, gc.Equals, 113) c.Assert(writer.Log[0].Module, gc.Equals, "test.writer") }
func (s *writerBasicsSuite) TestWillWrite(c *gc.C) { // By default, the root logger watches TRACE messages c.Assert(loggo.WillWrite(loggo.TRACE), gc.Equals, true) // Note: ReplaceDefaultWriter doesn't let us change the default log // level :( writer, _, err := loggo.RemoveWriter("default") c.Assert(err, gc.IsNil) c.Assert(writer, gc.NotNil) err = loggo.RegisterWriter("default", writer, loggo.CRITICAL) c.Assert(err, gc.IsNil) c.Assert(loggo.WillWrite(loggo.TRACE), gc.Equals, false) c.Assert(loggo.WillWrite(loggo.DEBUG), gc.Equals, false) c.Assert(loggo.WillWrite(loggo.INFO), gc.Equals, false) c.Assert(loggo.WillWrite(loggo.WARNING), gc.Equals, false) c.Assert(loggo.WillWrite(loggo.CRITICAL), gc.Equals, true) }
func (s *logwriterSuite) BenchmarkLoggingDiskWriterNoMessages(c *gc.C) { logFile, cleanup := setupTempFileWriter(c) defer cleanup() // Change the log level writer, _, err := loggo.RemoveWriter("testfile") c.Assert(err, gc.IsNil) loggo.RegisterWriter("testfile", writer, loggo.WARNING) msg := "just a simple warning for %d" for i := 0; i < c.N; i++ { s.logger.Debugf(msg, i) } offset, err := logFile.Seek(0, os.SEEK_CUR) c.Assert(err, gc.IsNil) c.Assert(offset, gc.Equals, int64(0), gc.Commentf("Data was written to the log file.")) }
func (*writerBasicsSuite) TestRegisterWriterTypedNil(c *gc.C) { // If the interface is a typed nil, we have to trust the user. var writer *loggo.TestWriter err := loggo.RegisterWriter("nil", writer, loggo.INFO) c.Assert(err, gc.IsNil) }
func (*writerBasicsSuite) TestRegisterNilWriter(c *gc.C) { err := loggo.RegisterWriter("nil", nil, loggo.INFO) c.Assert(err, gc.ErrorMatches, `Writer cannot be nil`) }
func (*writerBasicsSuite) TestRegisterWriterExistingName(c *gc.C) { err := loggo.RegisterWriter("default", &loggo.TestWriter{}, loggo.INFO) c.Assert(err, gc.ErrorMatches, `there is already a Writer registered with the name "default"`) }