Categorygithub.com/ajpikul-com/ilog
repositorypackage
0.0.0-20230802005115-88d039e2c7e6
Repository: https://github.com/ajpikul-com/ilog.git
Documentation: pkg.go.dev

# README

ilog

Notes: Some changes were made. We now have Warn and Debug level interfaces that can be turned on and off. In simple logger, it's a Level(DEBUG|INFO|WARN|ERROR|NONE) function, in zap, it's a structure member Level I realized in zap we can't use most of the sugar logger interface with this structure, and I'm leaning towards just using zap entirely. Also, tests don't implement any of this yet.

I generally like it, but I want better stack trace processing. Also, need to set levels in consistent way.

ilog provides an interface (LoggerInterface) that specifies three functions:

  • Init() error
  • Info(string)
  • Error(string)
  • Debug(string)
  • Warn(string)

You must choose (or write) a backend:

  • EmptyLogger- Logging is off in this case
defaultLogger = &ilog.EmptyLogger{}
defaultLogger.Init()
  • SimpleLogger- Unbuffered writes to stderr or another file descriptor
defaultLogger = &ilog.SimpleLogger{} 
defaultLogger.Init()
defaultLogger.Level(ilog.DEBUG|INFO|WARN|ERROR)
  • ZapWrap- A backend of uber-go/zap
defaultLogger = &ilog.ZapWrap{}
type ilog.ZapWrap struct { 
 Sugar bool // If true support printf and key-value pairs
 Paths []string // Out, default is stderr
 Level int // One of ilog.DEBUG|INFO|WARN|ERROR
 ZapLogger *zap.Logger // Access after init
 SugarLogger *zap.SugaredLogger // Access after init
}
  • TestLogger- A backend that calls t.Log or b.Log (has seperate constructor which accepts as an argument the test being run)

Use the ilog_test.go file as an example of how to set up loggers. Or TODO: write readme for each one

Use

In Application Example

newLogger := new(ilog.ZapWrap)
err := newLogger.Init()
if err != nil {
	panic(err)
}
myModule.SetDefaultLogger(newLogger)

In-Module Example

// Set a global logger for the library
var defaultLogger ilog.LoggerInterface

// Establish a default logger
func init() {
	if defaulLogger == nil {
		defaultLogger = new(ilog.EmptyLogger)
	}
}

// Allow calling program to change default logger
func SetDefaultLogger(newLogger ilog.LoggerInterface) {
	defaultLogger = newLogger
	defaultLogger.Info("Default Logger Set")
}

Benchmarks

NameIterationsSpeedMemoryAllocs
BenchmarkLogger/Benchmark_empty_logger20000000000.64 ns/op0 B/op0 allocs/op
BenchmarkLogger/Benchmark_simple_logger10000001166 ns/op0 B/op0 allocs/op
BenchmarkLogger/Benchmark_zap_production_logger5000000308 ns/op2 B/op0 allocs/op
BenchmarkLogger/Benchmark_zap_sugared_logger2000000611 ns/op50 B/op2 allocs/op