Skip to content

Commit d37e18f

Browse files
committed
Add config option to strip (most) colors from console logs
1 parent 7dc923a commit d37e18f

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

conf/defaults.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ level = Info
224224
# For "console" mode only
225225
[log.console]
226226
level =
227+
# Set formatting to "false" to disable color formatting of console logs
228+
formatting = true
227229

228230
# For "file" mode only
229231
[log.file]

pkg/log/console.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ var (
4545

4646
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
4747
type ConsoleWriter struct {
48-
lg *log.Logger
49-
Level int `json:"level"`
48+
lg *log.Logger
49+
Level int `json:"level"`
50+
Formatting bool `json:"formatting"`
5051
}
5152

5253
// create ConsoleWriter returning as LoggerInterface.
5354
func NewConsole() LoggerInterface {
5455
return &ConsoleWriter{
55-
lg: log.New(os.Stderr, "", log.Ldate|log.Ltime),
56-
Level: TRACE,
56+
lg: log.New(os.Stderr, "", log.Ldate|log.Ltime),
57+
Level: TRACE,
58+
Formatting: true,
5759
}
5860
}
5961

@@ -65,7 +67,7 @@ func (cw *ConsoleWriter) WriteMsg(msg string, skip, level int) error {
6567
if cw.Level > level {
6668
return nil
6769
}
68-
if runtime.GOOS == "windows" {
70+
if runtime.GOOS == "windows" || !cw.Formatting {
6971
cw.lg.Println(msg)
7072
} else {
7173
cw.lg.Println(colors[level](msg))

pkg/setting/setting.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type CommandLineArgs struct {
140140

141141
func init() {
142142
IsWindows = runtime.GOOS == "windows"
143-
log.NewLogger(0, "console", `{"level": 0}`)
143+
log.NewLogger(0, "console", `{"level": 0, "formatting":true}`)
144144
}
145145

146146
func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
@@ -527,7 +527,11 @@ func initLogging(args *CommandLineArgs) {
527527
// Generate log configuration.
528528
switch mode {
529529
case "console":
530-
LogConfigs[i] = util.DynMap{"level": level}
530+
formatting := sec.Key("formatting").MustBool(true)
531+
LogConfigs[i] = util.DynMap{
532+
"level": level,
533+
"formatting": formatting,
534+
}
531535
case "file":
532536
logPath := sec.Key("file_name").MustString(filepath.Join(LogsPath, "grafana.log"))
533537
os.MkdirAll(filepath.Dir(logPath), os.ModePerm)

0 commit comments

Comments
 (0)