1
0
mirror of https://github.com/khorevaa/logos.git synced 2025-01-22 05:10:53 +02:00

fix: error in examples

This commit is contained in:
khorevaa 2021-02-01 19:19:18 +03:00
parent 0d0e002c95
commit 2fe64ed299
3 changed files with 66 additions and 3 deletions

View File

@ -3,13 +3,46 @@ package logos_test
import (
"errors"
"github.com/khorevaa/logos"
log2 "log"
)
func ExampleNew_simple() {
rawConfig := `
appenders:
console:
- name: CONSOLE
target: stdout
no_color: true
encoder:
console:
disable_timestamp: true
color_scheme:
info_level: blue+b
debug_level: green+b
loggerConfigs:
root:
level: debug
appender_refs:
- CONSOLE
`
err := logos.InitWithConfigContent(rawConfig)
if err != nil {
panic(err)
}
//logos.CancelRedirectStdLog()
log2.Println("1")
log := logos.New("<your-package-name>") // like github.com/khorevaa/logos
log.Info("This is me first log. Hello world logging systems")
//cancel()
//log2.Println("2")
// Output:
// INFO stdlog 1
// INFO <your-package-name> This is me first log. Hello world logging systems
}
func ExampleNew_with_config_json() {
@ -34,6 +67,8 @@ loggerConfigs:
log := logos.New("<your-package-name>") // like github.com/khorevaa/logos
log.Info("This is me first log. Hello world logging systems")
// Output:
// .+"logger":"<your-package-name>","msg":"This is me first log. Hello world logging systems"}
}
func ExampleNew_with_color_scheme() {
@ -42,8 +77,10 @@ appenders:
console:
- name: CONSOLE
target: stdout
no_color: true
encoder:
console:
disable_timestamp: true
color_scheme:
info_level: blue+b
debug_level: green+b
@ -63,4 +100,7 @@ loggerConfigs:
err := errors.New("log system error")
log.Debug("This is me first error", logos.Any("err", err))
// Output:
// INFO <your-package-name> This is me first log. Hello world logging systems
// DEBUG <your-package-name> This is me first error err=log system error
}

View File

@ -149,6 +149,7 @@ func InitWithConfigContent(content string) error {
}
func New(name string) Logger {
manager.Sync()
return manager.NewLogger(name)
}
func SetLevel(name string, level zapcore.Level) {
@ -162,3 +163,11 @@ func UpdateLogger(name string, logger *zap.Logger) {
func Sync() {
_ = manager.Sync()
}
func RedirectStdLog() func() {
return manager.RedirectStdLog()
}
func CancelRedirectStdLog() {
manager.CancelRedirectStdLog()
}

View File

@ -25,6 +25,8 @@ type logManager struct {
rootLevel zapcore.Level
rootLogger *warpLogger
rootLoggerConfig *loggerConfig
cancelRedirectStdLog func()
}
func newLogManager(rawConfig *common.Config) (*logManager, error) {
@ -250,10 +252,20 @@ func (m *logManager) newCoreLoggerConfig(name string) *loggerConfig {
return loggerConfig
}
func (m *logManager) RedirectStdLog() {
func (m *logManager) RedirectStdLog() func() {
stdlog := m.getLogger("stdlog", false)
zap.RedirectStdLog(stdlog.defLogger)
m.cancelRedirectStdLog = zap.RedirectStdLog(stdlog.defLogger)
return m.cancelRedirectStdLog
}
func (m *logManager) CancelRedirectStdLog() {
if m.cancelRedirectStdLog == nil {
return
}
m.cancelRedirectStdLog()
}
func (m *logManager) Update(rawConfig *common.Config) error {
@ -300,7 +312,9 @@ func (m *logManager) Update(rawConfig *common.Config) error {
return true
})
m.RedirectStdLog()
if m.cancelRedirectStdLog != nil {
m.cancelRedirectStdLog = m.RedirectStdLog()
}
return nil
}