MCPcopy Create free account
hub / github.com/DoNewsCode/core / NewLogger

Function NewLogger

logging/log.go:41–77  ·  view source on GitHub ↗

NewLogger constructs a log.Logger based on the given format. The support formats are "json" and "logfmt".

(format string)

Source from the content-addressed store, hash-verified

39// NewLogger constructs a log.Logger based on the given format. The support
40// formats are "json" and "logfmt".
41func NewLogger(format string) (logger log.Logger) {
42 switch strings.ToLower(format) {
43 case "json":
44 logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
45 return logger
46 default:
47 // Color by level value
48 colorFn := func(keyvals ...interface{}) term.FgBgColor {
49 for i := 0; i < len(keyvals)-1; i += 2 {
50 if keyvals[i] != "level" {
51 continue
52 }
53 if value, ok := keyvals[i+1].(level.Value); ok {
54 switch value.String() {
55 case "debug":
56 return term.FgBgColor{Fg: term.DarkGray}
57 case "info":
58 return term.FgBgColor{Fg: term.Gray}
59 case "warn":
60 return term.FgBgColor{Fg: term.Yellow}
61 case "error":
62 return term.FgBgColor{Fg: term.Red}
63 case "crit":
64 return term.FgBgColor{Fg: term.Gray, Bg: term.DarkRed}
65 default:
66 return term.FgBgColor{}
67 }
68 }
69 }
70 return term.FgBgColor{}
71 }
72 logger = term.NewLogger(os.Stdout, log.NewLogfmtLogger, colorFn)
73 logger = log.With(log.NewSyncLogger(logger), "ts", log.DefaultTimestamp)
74
75 return logger
76 }
77}
78
79// LevelFilter filters the log output based on its level.
80// Allowed levels are "debug", "info", "warn", "error", or "none"

Callers 7

ProvideLoggerFunction · 0.92
Example_minimalFunction · 0.92
Example_levelFunction · 0.92
ExampleWithLevelFunction · 0.92
Example_sprintfFunction · 0.92
ExampleWithContextFunction · 0.92
TestNewLoggerFunction · 0.85

Calls 2

StringMethod · 0.65
WithMethod · 0.45

Tested by 6

Example_minimalFunction · 0.74
Example_levelFunction · 0.74
ExampleWithLevelFunction · 0.74
Example_sprintfFunction · 0.74
ExampleWithContextFunction · 0.74
TestNewLoggerFunction · 0.68