mirror of https://github.com/gogs/gogs.git
cmd: init minimal logging config in hook mode
parent
0b86aa5d29
commit
7382c23a17
|
@ -49,6 +49,8 @@ func fail(userMessage, logMessage string, args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setup(c *cli.Context, logPath string, connectDB bool) {
|
func setup(c *cli.Context, logPath string, connectDB bool) {
|
||||||
|
conf.HookMode = true
|
||||||
|
|
||||||
var customConf string
|
var customConf string
|
||||||
if c.IsSet("config") {
|
if c.IsSet("config") {
|
||||||
customConf = c.String("config")
|
customConf = c.String("config")
|
||||||
|
@ -60,6 +62,7 @@ func setup(c *cli.Context, logPath string, connectDB bool) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail("Internal error", "Failed to init configuration: %v", err)
|
fail("Internal error", "Failed to init configuration: %v", err)
|
||||||
}
|
}
|
||||||
|
conf.InitLogging(true)
|
||||||
|
|
||||||
level := log.LevelTrace
|
level := log.LevelTrace
|
||||||
if conf.IsProdMode() {
|
if conf.IsProdMode() {
|
||||||
|
|
|
@ -28,12 +28,19 @@ type logConf struct {
|
||||||
// Log settings
|
// Log settings
|
||||||
var Log *logConf
|
var Log *logConf
|
||||||
|
|
||||||
// initLogConf returns parsed logging configuration from given INI file.
|
// initLogConf returns parsed logging configuration from given INI file. When the
|
||||||
|
// argument "hookMode" is true, it only initializes the root path for log files.
|
||||||
// NOTE: Because we always create a console logger as the primary logger at init time,
|
// NOTE: Because we always create a console logger as the primary logger at init time,
|
||||||
// we need to remove it in case the user doesn't configure to use it after the logging
|
// we need to remove it in case the user doesn't configure to use it after the logging
|
||||||
// service is initalized.
|
// service is initalized.
|
||||||
func initLogConf(cfg *ini.File) (_ *logConf, hasConsole bool, _ error) {
|
func initLogConf(cfg *ini.File, hookMode bool) (_ *logConf, hasConsole bool, _ error) {
|
||||||
rootPath := cfg.Section("log").Key("ROOT_PATH").MustString(filepath.Join(WorkDir(), "log"))
|
rootPath := cfg.Section("log").Key("ROOT_PATH").MustString(filepath.Join(WorkDir(), "log"))
|
||||||
|
if hookMode {
|
||||||
|
return &logConf{
|
||||||
|
RootPath: ensureAbs(rootPath),
|
||||||
|
}, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
modes := strings.Split(cfg.Section("log").Key("MODE").MustString("console"), ",")
|
modes := strings.Split(cfg.Section("log").Key("MODE").MustString("console"), ",")
|
||||||
lc := &logConf{
|
lc := &logConf{
|
||||||
RootPath: ensureAbs(rootPath),
|
RootPath: ensureAbs(rootPath),
|
||||||
|
@ -118,12 +125,24 @@ func initLogConf(cfg *ini.File) (_ *logConf, hasConsole bool, _ error) {
|
||||||
return lc, hasConsole, nil
|
return lc, hasConsole, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitLogging initializes the logging service of the application.
|
// InitLogging initializes the logging service of the application. When the
|
||||||
func InitLogging() {
|
// argument "hookMode" is true, it only initializes the root path for log files
|
||||||
logConf, hasConsole, err := initLogConf(File)
|
// without creating any logger.
|
||||||
|
func InitLogging(hookMode bool) {
|
||||||
|
logConf, hasConsole, err := initLogConf(File, hookMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to init logging configuration: %v", err)
|
log.Fatal("Failed to init logging configuration: %v", err)
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if !hasConsole {
|
||||||
|
log.Remove(log.DefaultConsoleName)
|
||||||
|
}
|
||||||
|
Log = logConf
|
||||||
|
}()
|
||||||
|
|
||||||
|
if hookMode {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = os.MkdirAll(logConf.RootPath, os.ModePerm)
|
err = os.MkdirAll(logConf.RootPath, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -158,10 +177,4 @@ func InitLogging() {
|
||||||
}
|
}
|
||||||
log.Trace("Log mode: %s (%s)", strings.Title(mode), strings.Title(strings.ToLower(level.String())))
|
log.Trace("Log mode: %s (%s)", strings.Title(mode), strings.Title(strings.ToLower(level.String())))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hasConsole {
|
|
||||||
log.Remove(log.DefaultConsoleName)
|
|
||||||
}
|
|
||||||
|
|
||||||
Log = logConf
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -492,6 +492,8 @@ func handleDeprecated() {
|
||||||
|
|
||||||
// HookMode indicates whether program starts as Git server-side hook callback.
|
// HookMode indicates whether program starts as Git server-side hook callback.
|
||||||
// All operations should be done synchronously to prevent program exits before finishing.
|
// All operations should be done synchronously to prevent program exits before finishing.
|
||||||
|
//
|
||||||
|
// ⚠️ WARNING: Should only be set by "internal/cmd/serv.go".
|
||||||
var HookMode bool
|
var HookMode bool
|
||||||
|
|
||||||
// Indicates which database backend is currently being used.
|
// Indicates which database backend is currently being used.
|
||||||
|
|
|
@ -54,7 +54,7 @@ func GlobalInit(customConf string) error {
|
||||||
return errors.Wrap(err, "init configuration")
|
return errors.Wrap(err, "init configuration")
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.InitLogging()
|
conf.InitLogging(false)
|
||||||
log.Info("%s %s", conf.App.BrandName, conf.App.Version)
|
log.Info("%s %s", conf.App.BrandName, conf.App.Version)
|
||||||
log.Trace("Work directory: %s", conf.WorkDir())
|
log.Trace("Work directory: %s", conf.WorkDir())
|
||||||
log.Trace("Custom path: %s", conf.CustomDir())
|
log.Trace("Custom path: %s", conf.CustomDir())
|
||||||
|
|
Loading…
Reference in New Issue