Commit a02cf5be by Torkel Ödegaard

feat(logging): added loging filters

parent 3dc7706c
......@@ -248,12 +248,9 @@ templates_pattern = emails/*.html
#################################### 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
# Buffer length of channel, keep it as it is if you don't know what it is.
buffer_len = 10000
# Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Info"
level = Info
......
......@@ -233,9 +233,6 @@ check_for_updates = true
# Use comma to separate multiple modes, e.g. "console, file"
;mode = console, file
# Buffer length of channel, keep it as it is if you don't know what it is.
;buffer_len = 10000
# Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Info"
;level = Info
......
......@@ -90,21 +90,40 @@ var logLevels = map[string]log15.Lvl{
"Critical": log15.LvlCrit,
}
func getLogLevel(key string, defaultName string, cfg *ini.File) (string, log15.Lvl) {
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"})
level := getLogLevelFromString(levelName)
return levelName, level
}
func getLogLevelFromString(levelName string) log15.Lvl {
level, ok := logLevels[levelName]
if !ok {
Root.Error("Unknown log level", "level", levelName)
return log15.LvlError
}
return levelName, level
return level
}
func getFilters(filterStrArray []string) map[string]log15.Lvl {
filterMap := make(map[string]log15.Lvl)
for _, filterStr := range filterStrArray {
parts := strings.Split(filterStr, ":")
filterMap[parts[0]] = getLogLevelFromString(parts[1])
}
return filterMap
}
func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
Close()
defaultLevelName, _ := getLogLevel("log", "Info", cfg)
defaultLevelName, _ := getLogLevelFromConfig("log", "Info", cfg)
defaultFilters := getFilters(cfg.Section("log").Key("filters").Strings(" "))
handlers := make([]log15.Handler, 0)
for _, mode := range modes {
......@@ -115,12 +134,15 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
}
// Log level.
_, level := getLogLevel("log."+mode, defaultLevelName, cfg)
_, level := getLogLevelFromConfig("log."+mode, defaultLevelName, cfg)
modeFilters := getFilters(sec.Key("filters").Strings(" "))
var handler log15.Handler
// Generate log configuration.
switch mode {
case "console":
handlers = append(handlers, log15.LvlFilterHandler(level, log15.StdoutHandler))
handler = log15.StdoutHandler
case "file":
fileName := sec.Key("file_name").MustString(filepath.Join(logsPath, "grafana.log"))
os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
......@@ -134,7 +156,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
fileHandler.Init()
loggersToClose = append(loggersToClose, fileHandler)
handlers = append(handlers, log15.LvlFilterHandler(level, fileHandler))
handler = fileHandler
// case "conn":
// LogConfigs[i] = util.DynMap{
......@@ -168,7 +190,41 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
// "tag": sec.Key("tag").MustString(""),
// }
}
for key, value := range defaultFilters {
if _, exist := modeFilters[key]; !exist {
modeFilters[key] = value
}
}
for key, value := range modeFilters {
fmt.Printf("key: %v, value: %v \n", key, value)
}
handler = LogFilterHandler(level, modeFilters, handler)
handlers = append(handlers, handler)
}
Root.SetHandler(log15.MultiHandler(handlers...))
}
func LogFilterHandler(maxLevel log15.Lvl, filters map[string]log15.Lvl, h log15.Handler) log15.Handler {
return log15.FilterHandler(func(r *log15.Record) (pass bool) {
if len(filters) > 0 {
for i := 0; i < len(r.Ctx); i += 2 {
key := r.Ctx[i].(string)
if key == "logger" {
loggerName, strOk := r.Ctx[i+1].(string)
if strOk {
if filterLevel, ok := filters[loggerName]; ok {
return r.Lvl <= filterLevel
}
}
}
}
}
return r.Lvl <= maxLevel
}, h)
}
......@@ -20,6 +20,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
)
......@@ -40,9 +41,9 @@ func Logger() macaron.Handler {
status := rw.Status()
if status == 200 || status == 304 {
// if !setting.RouterLogging {
// return
// }
if !setting.RouterLogging {
return
}
}
if ctx, ok := c.Data["ctx"]; ok {
......
......@@ -29,7 +29,7 @@ type MigrationLog struct {
func NewMigrator(engine *xorm.Engine) *Migrator {
mg := &Migrator{}
mg.x = engine
mg.Logger = log.New("Migrator")
mg.Logger = log.New("migrator")
mg.migrations = make([]Migration, 0)
mg.dialect = NewDialect(mg.x.DriverName())
return mg
......
......@@ -546,7 +546,12 @@ func readSessionConfig() {
}
func initLogging() {
// split on comma
LogModes = strings.Split(Cfg.Section("log").Key("mode").MustString("console"), ",")
// also try space
if len(LogModes) == 1 {
LogModes = strings.Split(Cfg.Section("log").Key("mode").MustString("console"), " ")
}
LogsPath = makeAbsolute(Cfg.Section("paths").Key("logs").String(), HomePath)
log.ReadLoggingConfig(LogModes, LogsPath, Cfg)
}
......
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