- Unstructured
- Leveled
- With caller (file path / name and line number)
- Customizable output format
- Rotating by size, date or hour
- Cross platform, tested on Linux, macOS and Windows
- No 3rd party dependance
- Fast
go get -u github.com/keakon/golog
package main
import (
    "github.com/keakon/golog"
    "github.com/keakon/golog/log"
)
func main() {
    l := golog.NewStdoutLogger()
    defer l.Close()
    l.Infof("hello %d", 1)
    log.SetDefaultLogger(l)
    test()
}
func test() {
    log.Infof("hello %d", 2)
}func main() {
    w, _ := golog.NewBufferedFileWriter("test.log")
    l := golog.NewLoggerWithWriter(w)
    defer l.Close()
    l.Infof("hello world")
}func main() {
    w, _ := golog.NewTimedRotatingFileWriter("test", golog.RotateByDate, 30)
    l := golog.NewLoggerWithWriter(w)
    defer l.Close()
    l.Infof("hello world")
}func main() {
    w := golog.NewStdoutWriter()
    f := golog.ParseFormat("[%l %D %T %S] %m")
    h := golog.NewHandler(golog.InfoLevel, f)
    h.AddWriter(w)
    l := golog.NewLogger(golog.InfoLevel)
    l.AddHandler(h)
    defer l.Close()
    l.Infof("hello world")
}Check document for more format directives.
func main() {
    golog.StartFastTimer()
    defer golog.StopFastTimer()
    l := golog.NewStdoutLogger()
    defer l.Close()
    l.Infof("hello world")
}The fast timer is about 30% faster than calling time.Time() for each logging record. But it's not thread-safe which may cause some problems (I think those are neglectable in most cases):
- The timer updates every 1 second, so the logging time can be at most 1 second behind the real time.
- Each thread will notice the changes of timer in a few milliseconds, so the concurrent logging messages may get different logging time (less than 2% probability). eg:
[I 2021-09-13 14:31:25 log_test:206] test
[I 2021-09-13 14:31:24 log_test:206] test
[I 2021-09-13 14:31:25 log_test:206] test
- When the day changing, the logging date and time might from different day. eg:
[I 2021-09-12 23:59:59 log_test:206] test
[I 2021-09-13 23:59:59 log_test:206] test
[I 2021-09-12 00:00:00 log_test:206] test
go1.17 darwin/amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkDiscardLogger-12                 13788436       77.83 ns/op       0 B/op      0 allocs/op
BenchmarkDiscardLoggerWithoutTimer-12     10472464       112.1 ns/op       0 B/op      0 allocs/op
BenchmarkNopLog-12                      1000000000      0.1918 ns/op       0 B/op      0 allocs/op
BenchmarkMultiLevels-12                    3922504       304.8 ns/op       0 B/op      0 allocs/op
BenchmarkBufferedFileLogger-12             4937521       251.7 ns/op       0 B/op      0 allocs/op
BenchmarkDiscardZerolog-12                 4074019       289.8 ns/op     280 B/op      3 allocs/op
BenchmarkDiscardZap-12                     2908678       409.8 ns/op     321 B/op      7 allocs/op
Example output of the benchmarks:
[I 2018-11-20 17:05:37 log_test:118] test