@ -55,7 +55,7 @@ func NewDefaultLogger(format, level string) (Logger, error) {
// make the writer thread-safe
// make the writer thread-safe
logWriter = newSyncWriter ( logWriter )
logWriter = newSyncWriter ( logWriter )
return defaultLogger {
return & defaultLogger {
Logger : zerolog . New ( logWriter ) . Level ( logLevel ) . With ( ) . Timestamp ( ) . Logger ( ) ,
Logger : zerolog . New ( logWriter ) . Level ( logLevel ) . With ( ) . Timestamp ( ) . Logger ( ) ,
} , nil
} , nil
}
}
@ -76,9 +76,7 @@ func (l defaultLogger) Info(msg string, keyVals ...interface{}) {
}
}
func ( l defaultLogger ) Error ( msg string , keyVals ... interface { } ) {
func ( l defaultLogger ) Error ( msg string , keyVals ... interface { } ) {
e := l . Logger . Error ( )
e . Fields ( getLogFields ( keyVals ... ) ) . Msg ( msg )
l . Logger . Error ( ) . Fields ( getLogFields ( keyVals ... ) ) . Msg ( msg )
}
}
func ( l defaultLogger ) Debug ( msg string , keyVals ... interface { } ) {
func ( l defaultLogger ) Debug ( msg string , keyVals ... interface { } ) {
@ -86,11 +84,33 @@ func (l defaultLogger) Debug(msg string, keyVals ...interface{}) {
}
}
func ( l defaultLogger ) With ( keyVals ... interface { } ) Logger {
func ( l defaultLogger ) With ( keyVals ... interface { } ) Logger {
return defaultLogger {
return & defaultLogger {
Logger : l . Logger . With ( ) . Fields ( getLogFields ( keyVals ... ) ) . Logger ( ) ,
Logger : l . Logger . With ( ) . Fields ( getLogFields ( keyVals ... ) ) . Logger ( ) ,
}
}
}
}
// OverrideWithNewLogger replaces an existing logger's internal with
// a new logger, and makes it possible to reconfigure an existing
// logger that has already been propagated to callers.
func OverrideWithNewLogger ( logger Logger , format , level string ) error {
ol , ok := logger . ( * defaultLogger )
if ! ok {
return fmt . Errorf ( "logger %T cannot be overridden" , logger )
}
newLogger , err := NewDefaultLogger ( format , level )
if err != nil {
return err
}
nl , ok := newLogger . ( * defaultLogger )
if ! ok {
return fmt . Errorf ( "logger %T cannot be overridden by %T" , logger , newLogger )
}
ol . Logger = nl . Logger
return nil
}
func getLogFields ( keyVals ... interface { } ) map [ string ] interface { } {
func getLogFields ( keyVals ... interface { } ) map [ string ] interface { } {
if len ( keyVals ) % 2 != 0 {
if len ( keyVals ) % 2 != 0 {
return nil
return nil