Commit b1af2812 by Arve Knudsen Committed by GitHub

Server: Fail when unable to create log directory (#20804)

parent 8b7cd153
......@@ -12,6 +12,7 @@ import (
"github.com/go-stack/stack"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/inconshreveable/log15"
isatty "github.com/mattn/go-isatty"
"gopkg.in/ini.v1"
......@@ -181,7 +182,7 @@ func getLogFormat(format string) log15.Format {
}
}
func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) error {
Close()
defaultLevelName, _ := getLogLevelFromConfig("log", "info", cfg)
......@@ -194,6 +195,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
sec, err := cfg.GetSection("log." + mode)
if err != nil {
Root.Error("Unknown log mode", "mode", mode)
return errutil.Wrapf(err, "failed to get config section log.%s", mode)
}
// Log level.
......@@ -212,7 +214,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
dpath := filepath.Dir(fileName)
if err := os.MkdirAll(dpath, os.ModePerm); err != nil {
Root.Error("Failed to create directory", "dpath", dpath, "err", err)
break
return errutil.Wrapf(err, "failed to create log directory %q", dpath)
}
fileHandler := NewFileWriter()
fileHandler.Filename = fileName
......@@ -223,8 +225,8 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
fileHandler.Daily = sec.Key("daily_rotate").MustBool(true)
fileHandler.Maxdays = sec.Key("max_days").MustInt64(7)
if err := fileHandler.Init(); err != nil {
Root.Error("Failed to create directory", "dpath", dpath, "err", err)
break
Root.Error("Failed to initialize file handler", "dpath", dpath, "err", err)
return errutil.Wrapf(err, "failed to initialize file handler")
}
loggersToClose = append(loggersToClose, fileHandler)
......@@ -236,6 +238,9 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
loggersToClose = append(loggersToClose, sysLogHandler)
handler = sysLogHandler
}
if handler == nil {
panic(fmt.Sprintf("Handler is uninitialized for mode %q", mode))
}
for key, value := range defaultFilters {
if _, exist := modeFilters[key]; !exist {
......@@ -254,6 +259,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
}
Root.SetHandler(log15.MultiHandler(handlers...))
return nil
}
func LogFilterHandler(maxLevel log15.Lvl, filters map[string]log15.Lvl, h log15.Handler) log15.Handler {
......
......@@ -444,7 +444,7 @@ func evalConfigValues(file *ini.File) {
}
}
func loadSpecifedConfigFile(configFile string, masterFile *ini.File) error {
func loadSpecifiedConfigFile(configFile string, masterFile *ini.File) error {
if configFile == "" {
configFile = filepath.Join(HomePath, CustomInitPath)
// return without error if custom file does not exist
......@@ -511,7 +511,7 @@ func (cfg *Cfg) loadConfiguration(args *CommandLineArgs) (*ini.File, error) {
applyCommandLineDefaultProperties(commandLineProps, parsedFile)
// load specified config file
err = loadSpecifedConfigFile(args.Config, parsedFile)
err = loadSpecifiedConfigFile(args.Config, parsedFile)
if err != nil {
err2 := cfg.initLogging(parsedFile)
if err2 != nil {
......@@ -1083,8 +1083,7 @@ func (cfg *Cfg) initLogging(file *ini.File) error {
return err
}
cfg.LogsPath = makeAbsolute(logsPath, HomePath)
log.ReadLoggingConfig(logModes, cfg.LogsPath, file)
return nil
return log.ReadLoggingConfig(logModes, cfg.LogsPath, file)
}
func (cfg *Cfg) LogConfigSources() {
......
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