-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update logger to facilitate the use of global logger
- Loading branch information
Showing
9 changed files
with
173 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# logger package | ||
|
||
The logger package is intended to make unified log management in the whole app | ||
|
||
The log may be used in 2 ways: | ||
|
||
- Global: easy use of the global logger without the need to init or configure. | ||
``` | ||
package main | ||
import ( | ||
"github.com/zondax/golem/pkg/logger" | ||
) | ||
func main() { | ||
// Importing logger global logger is configured in info level and may be used | ||
logger.Info("Log info message") | ||
// Reconfigure global logger with config | ||
logger.SetGlobalConfig(logger.Config{Level: "debug"}) | ||
logger.Info("Log debug message") | ||
logger.Sync() | ||
} | ||
``` | ||
|
||
- Local: use distinct logger for the package | ||
``` | ||
package main | ||
import ( | ||
"github.com/zondax/golem/pkg/logger" | ||
) | ||
func main() { | ||
// Generate new logger with options and use it | ||
log := logger.NewLogger(opts ...interface{}) | ||
log.Info("Log info message") | ||
log.Sync() | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package logger | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
) | ||
|
||
func ReplaceGlobals(logger *zap.Logger) func() { | ||
return zap.ReplaceGlobals(logger) | ||
} | ||
|
||
func SetGlobalConfig(config Config) func() { | ||
logger := configureAndBuildLogger(config) | ||
return ReplaceGlobals(logger) | ||
} | ||
|
||
func L() *zap.Logger { | ||
return zap.L() | ||
} | ||
|
||
func S() *zap.SugaredLogger { | ||
return zap.S() | ||
} | ||
|
||
func Info(msg string) { | ||
L().Info(msg) | ||
} | ||
|
||
func Debug(msg string) { | ||
L().Debug(msg) | ||
} | ||
|
||
func Warn(msg string) { | ||
L().Warn(msg) | ||
} | ||
|
||
func Error(msg string) { | ||
L().Error(msg) | ||
} | ||
|
||
func DPanic(msg string) { | ||
L().DPanic(msg) | ||
} | ||
|
||
func Panic(msg string) { | ||
L().Panic(msg) | ||
} | ||
|
||
func Fatal(msg string) { | ||
L().Fatal(msg) | ||
} | ||
|
||
func Infof(template string, args ...interface{}) { | ||
S().Infof(template, args...) | ||
} | ||
|
||
func Debugf(template string, args ...interface{}) { | ||
S().Debugf(template, args...) | ||
} | ||
|
||
func Warnf(template string, args ...interface{}) { | ||
S().Warnf(template, args...) | ||
} | ||
|
||
func Errorf(template string, args ...interface{}) { | ||
S().Errorf(template, args...) | ||
} | ||
|
||
func DPanicf(template string, args ...interface{}) { | ||
S().DPanicf(template, args...) | ||
} | ||
|
||
func Panicf(template string, args ...interface{}) { | ||
S().Panicf(template, args...) | ||
} | ||
|
||
func Fatalf(template string, args ...interface{}) { | ||
S().Fatalf(template, args...) | ||
} | ||
|
||
func Sync() error { | ||
// Sync global logger | ||
err := L().Sync() | ||
if err != nil { | ||
return err | ||
} | ||
// Sync global sugar logger | ||
return S().Sync() | ||
} |
Oops, something went wrong.