-
Notifications
You must be signed in to change notification settings - Fork 618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom contextual loggers #2319
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ package logger | |
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
@@ -53,24 +54,32 @@ var Config *logConfig | |
|
||
func logfmtFormatter(params string) seelog.FormatterFunc { | ||
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} { | ||
return fmt.Sprintf(`level=%s time=%s msg=%q module=%s | ||
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, context.FileName()) | ||
cc, ok := context.CustomContext().(map[string]string) | ||
var customContext string | ||
if ok && len(cc) > 0 { | ||
var sortedContext []string | ||
for k, v := range cc { | ||
sortedContext = append(sortedContext, k+"="+v) | ||
} | ||
sort.Strings(sortedContext) | ||
customContext = " " + strings.Join(sortedContext, " ") | ||
} | ||
return fmt.Sprintf(`level=%s time=%s msg=%q module=%s%s | ||
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, context.FileName(), customContext) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this and lines 74-82 share enough in common that I'd like to see them as a separate function. (You might have done this in one of the later commits... If so, please disregard. I'm going through in chronological order.) |
||
} | ||
} | ||
|
||
func jsonFormatter(params string) seelog.FormatterFunc { | ||
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} { | ||
return fmt.Sprintf(`{"level": %q, "time": %q, "msg": %q, "module": %q} | ||
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, context.FileName()) | ||
} | ||
} | ||
|
||
func reloadConfig() { | ||
logger, err := seelog.LoggerFromConfigAsString(seelogConfig()) | ||
if err == nil { | ||
seelog.ReplaceLogger(logger) | ||
} else { | ||
seelog.Error(err) | ||
cc, ok := context.CustomContext().(map[string]string) | ||
var customContext string | ||
if ok && len(cc) > 0 { | ||
for k, v := range cc { | ||
customContext += fmt.Sprintf(", %q: %q", k, v) | ||
} | ||
} | ||
return fmt.Sprintf(`{"level": %q, "time": %q, "msg": %q, "module": %q%s} | ||
`, level.String(), context.CallTime().UTC().Format(time.RFC3339), message, context.FileName(), customContext) | ||
} | ||
} | ||
|
||
|
@@ -117,7 +126,7 @@ func SetLevel(logLevel string) { | |
Config.lock.Lock() | ||
defer Config.lock.Unlock() | ||
Config.level = parsedLevel | ||
reloadConfig() | ||
reloadMainConfig() | ||
} | ||
} | ||
|
||
|
@@ -129,6 +138,24 @@ func GetLevel() string { | |
return Config.level | ||
} | ||
|
||
func InitLogger() seelog.LoggerInterface { | ||
logger, err := seelog.LoggerFromConfigAsString(seelogConfig()) | ||
if err != nil { | ||
seelog.Errorf("Error creating seelog logger: %s", err) | ||
return seelog.Default | ||
} | ||
return logger | ||
} | ||
|
||
func reloadMainConfig() { | ||
logger, err := seelog.LoggerFromConfigAsString(seelogConfig()) | ||
if err == nil { | ||
seelog.ReplaceLogger(logger) | ||
} else { | ||
seelog.Error(err) | ||
} | ||
} | ||
|
||
func init() { | ||
Config = &logConfig{ | ||
logfile: os.Getenv(LOGFILE_ENV_VAR), | ||
|
@@ -139,7 +166,9 @@ func init() { | |
MaxRollCount: DEFAULT_MAX_ROLL_COUNT, | ||
} | ||
|
||
SetLevel(os.Getenv(LOGLEVEL_ENV_VAR)) | ||
if level := os.Getenv(LOGLEVEL_ENV_VAR); level != "" { | ||
SetLevel(level) | ||
} | ||
if RolloverType := os.Getenv(LOG_ROLLOVER_TYPE_ENV_VAR); RolloverType != "" { | ||
Config.RolloverType = RolloverType | ||
} | ||
|
@@ -169,7 +198,6 @@ func init() { | |
if err := seelog.RegisterCustomFormatter("EcsAgentJson", jsonFormatter); err != nil { | ||
seelog.Error(err) | ||
} | ||
|
||
registerPlatformLogger() | ||
reloadConfig() | ||
seelog.ReplaceLogger(InitLogger()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we know full implication of this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it's logging a non-nil error I thought it more appropriate to be logging at the ERROR level