Commit 1d8fdc09 by Torkel Ödegaard

feat(logging): added log format option, #4590

parent 7d6cda49
......@@ -251,18 +251,23 @@ templates_pattern = emails/*.html
# Use space to separate multiple modes, e.g. "console file"
mode = console, file
# Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Info"
level = Info
# Either "trace", "debug", "info", "warn", "error", "critical", default is "info"
level = info
# For "console" mode only
[log.console]
level =
# Set formatting to "false" to disable color formatting of console logs
formatting = false
# log line format, valid options are text, console and json
format = console
# For "file" mode only
[log.file]
level =
# log line format, valid options are text, console and json
format = text
# This enables automated log rotate(switch of following options), default is true
log_rotate = true
......@@ -280,6 +285,10 @@ max_days = 7
[log.syslog]
level =
# log line format, valid options are text, console and json
format = text
# Syslog network type and address. This can be udp, tcp, or unix. If left blank, the default unix endpoints will be used.
network =
address =
......@@ -290,6 +299,7 @@ facility =
# Syslog tag. By default, the process' argv[0] is used.
tag =
#################################### AMQP Event Publisher ##########################
[event_publisher]
enabled = false
......
......@@ -230,19 +230,26 @@ check_for_updates = true
#################################### Logging ##########################
[log]
# Either "console", "file", "syslog". Default is console and file
# Use comma to separate multiple modes, e.g. "console, file"
# Use space to separate multiple modes, e.g. "console file"
;mode = console, file
# Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Info"
;level = Info
# Either "trace", "debug", "info", "warn", "error", "critical", default is "info"
;level = info
# For "console" mode only
[log.console]
;level =
# log line format, valid options are text, console and json
;format = console
# For "file" mode only
[log.file]
;level =
# log line format, valid options are text, console and json
;format = text
# This enables automated log rotate(switch of following options), default is true
;log_rotate = true
......@@ -258,6 +265,23 @@ check_for_updates = true
# Expired days of log file(delete after max days), default is 7
;max_days = 7
[log.syslog]
;level =
# log line format, valid options are text, console and json
;format = text
# Syslog network type and address. This can be udp, tcp, or unix. If left blank, the default unix endpoints will be used.
;network =
;address =
# Syslog facility. user, daemon and local0 through local7 are valid.
;facility =
# Syslog tag. By default, the process' argv[0] is used.
;tag =
#################################### AMQP Event Publisher ##########################
[event_publisher]
;enabled = false
......
......@@ -13,6 +13,7 @@ import (
"gopkg.in/ini.v1"
"github.com/inconshreveable/log15"
"github.com/inconshreveable/log15/term"
)
var Root log15.Logger
......@@ -82,16 +83,17 @@ func Close() {
}
var logLevels = map[string]log15.Lvl{
"Trace": log15.LvlDebug,
"Debug": log15.LvlDebug,
"Info": log15.LvlInfo,
"Warn": log15.LvlWarn,
"Error": log15.LvlError,
"Critical": log15.LvlCrit,
"trace": log15.LvlDebug,
"debug": log15.LvlDebug,
"info": log15.LvlInfo,
"warn": log15.LvlWarn,
"error": log15.LvlError,
"critical": log15.LvlCrit,
}
func getLogLevelFromConfig(key string, defaultName string, cfg *ini.File) (string, log15.Lvl) {
levelName := cfg.Section(key).Key("level").In(defaultName, []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"})
levelName := cfg.Section(key).Key("level").MustString("info")
levelName = strings.ToLower(levelName)
level := getLogLevelFromString(levelName)
return levelName, level
}
......@@ -118,10 +120,26 @@ func getFilters(filterStrArray []string) map[string]log15.Lvl {
return filterMap
}
func getLogFormat(format string) log15.Format {
switch format {
case "console":
if term.IsTty(os.Stdout.Fd()) {
return log15.TerminalFormat()
}
return log15.LogfmtFormat()
case "text":
return log15.LogfmtFormat()
case "json":
return log15.JsonFormat()
default:
return log15.LogfmtFormat()
}
}
func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
Close()
defaultLevelName, _ := getLogLevelFromConfig("log", "Info", cfg)
defaultLevelName, _ := getLogLevelFromConfig("log", "info", cfg)
defaultFilters := getFilters(cfg.Section("log").Key("filters").Strings(" "))
handlers := make([]log15.Handler, 0)
......@@ -136,18 +154,20 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
// Log level.
_, level := getLogLevelFromConfig("log."+mode, defaultLevelName, cfg)
modeFilters := getFilters(sec.Key("filters").Strings(" "))
format := getLogFormat(sec.Key("format").MustString(""))
var handler log15.Handler
// Generate log configuration.
switch mode {
case "console":
handler = log15.StdoutHandler
handler = log15.StreamHandler(os.Stdout, format)
case "file":
fileName := sec.Key("file_name").MustString(filepath.Join(logsPath, "grafana.log"))
os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
fileHandler := NewFileWriter()
fileHandler.Filename = fileName
fileHandler.Format = format
fileHandler.Rotate = sec.Key("log_rotate").MustBool(true)
fileHandler.Maxlines = sec.Key("max_lines").MustInt(1000000)
fileHandler.Maxsize = 1 << uint(sec.Key("max_size_shift").MustInt(28))
......@@ -159,6 +179,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
handler = fileHandler
case "syslog":
sysLogHandler := NewSyslog()
sysLogHandler.Format = format
sysLogHandler.Network = sec.Key("network").MustString("")
sysLogHandler.Address = sec.Key("address").MustString("")
sysLogHandler.Facility = sec.Key("facility").MustString("local7")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment