Add config option to strip (most) colors from console logs

This commit is contained in:
ctdk
2015-10-01 13:16:23 -07:00
parent 7dc923a292
commit d37e18fdcf
3 changed files with 15 additions and 7 deletions

View File

@@ -45,15 +45,17 @@ var (
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
type ConsoleWriter struct {
lg *log.Logger
Level int `json:"level"`
lg *log.Logger
Level int `json:"level"`
Formatting bool `json:"formatting"`
}
// create ConsoleWriter returning as LoggerInterface.
func NewConsole() LoggerInterface {
return &ConsoleWriter{
lg: log.New(os.Stderr, "", log.Ldate|log.Ltime),
Level: TRACE,
lg: log.New(os.Stderr, "", log.Ldate|log.Ltime),
Level: TRACE,
Formatting: true,
}
}
@@ -65,7 +67,7 @@ func (cw *ConsoleWriter) WriteMsg(msg string, skip, level int) error {
if cw.Level > level {
return nil
}
if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" || !cw.Formatting {
cw.lg.Println(msg)
} else {
cw.lg.Println(colors[level](msg))

View File

@@ -140,7 +140,7 @@ type CommandLineArgs struct {
func init() {
IsWindows = runtime.GOOS == "windows"
log.NewLogger(0, "console", `{"level": 0}`)
log.NewLogger(0, "console", `{"level": 0, "formatting":true}`)
}
func parseAppUrlAndSubUrl(section *ini.Section) (string, string) {
@@ -527,7 +527,11 @@ func initLogging(args *CommandLineArgs) {
// Generate log configuration.
switch mode {
case "console":
LogConfigs[i] = util.DynMap{"level": level}
formatting := sec.Key("formatting").MustBool(true)
LogConfigs[i] = util.DynMap{
"level": level,
"formatting": formatting,
}
case "file":
logPath := sec.Key("file_name").MustString(filepath.Join(LogsPath, "grafana.log"))
os.MkdirAll(filepath.Dir(logPath), os.ModePerm)