diff --git a/util/log/README.md b/util/log/README.md new file mode 100644 index 00000000..2ae61058 --- /dev/null +++ b/util/log/README.md @@ -0,0 +1,17 @@ +# Log + +This is the global logger for all micro based libraries which makes use of [github.com/go-log/log](https://github.com/go-log/log). + +It defaults the logger to the stdlib log implementation. + +## Set Logger + +Set the logger for micro libraries + +```go +// import micro/go-log +import "github.com/micro/go-micro/util/log" + +// SetLogger expects github.com/go-log/log.Logger interface +log.SetLogger(mylogger) +``` diff --git a/util/log/log.go b/util/log/log.go new file mode 100644 index 00000000..86208c06 --- /dev/null +++ b/util/log/log.go @@ -0,0 +1,41 @@ +// Package log is a global internal logger +package log + +import ( + "os" + + "github.com/go-log/log" + golog "github.com/go-log/log/log" +) + +var ( + // the local logger + logger log.Logger = golog.New() +) + +// Log makes use of github.com/go-log/log.Log +func Log(v ...interface{}) { + logger.Log(v...) +} + +// Logf makes use of github.com/go-log/log.Logf +func Logf(format string, v ...interface{}) { + logger.Logf(format, v...) +} + +// Fatal logs with Log and then exits with os.Exit(1) +func Fatal(v ...interface{}) { + Log(v...) + os.Exit(1) +} + +// Fatalf logs with Logf and then exits with os.Exit(1) +func Fatalf(format string, v ...interface{}) { + Logf(format, v...) + os.Exit(1) +} + +// SetLogger sets the local logger +func SetLogger(l log.Logger) { + logger = l +}