Commit b1af2812 by Arve Knudsen Committed by GitHub

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

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