aboutsummaryrefslogtreecommitdiff
path: root/internal/conf/log.go
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-03-02 01:01:52 +0800
committerᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-03-02 01:01:52 +0800
commit7382c23a17cad73c137e9bca66be0cd1b04b75fd (patch)
treeec1eda9a202c9605a2cb5b92002c876672aa276d /internal/conf/log.go
parent0b86aa5d29478c2316f6f29eb5fdcfb074b459b6 (diff)
cmd: init minimal logging config in hook mode
Diffstat (limited to 'internal/conf/log.go')
-rw-r--r--internal/conf/log.go35
1 files changed, 24 insertions, 11 deletions
diff --git a/internal/conf/log.go b/internal/conf/log.go
index 3087c7c8..58e17f87 100644
--- a/internal/conf/log.go
+++ b/internal/conf/log.go
@@ -28,12 +28,19 @@ type logConf struct {
// Log settings
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,
// we need to remove it in case the user doesn't configure to use it after the logging
// 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"))
+ if hookMode {
+ return &logConf{
+ RootPath: ensureAbs(rootPath),
+ }, false, nil
+ }
+
modes := strings.Split(cfg.Section("log").Key("MODE").MustString("console"), ",")
lc := &logConf{
RootPath: ensureAbs(rootPath),
@@ -118,12 +125,24 @@ func initLogConf(cfg *ini.File) (_ *logConf, hasConsole bool, _ error) {
return lc, hasConsole, nil
}
-// InitLogging initializes the logging service of the application.
-func InitLogging() {
- logConf, hasConsole, err := initLogConf(File)
+// InitLogging initializes the logging service of the application. When the
+// argument "hookMode" is true, it only initializes the root path for log files
+// without creating any logger.
+func InitLogging(hookMode bool) {
+ logConf, hasConsole, err := initLogConf(File, hookMode)
if err != nil {
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)
if err != nil {
@@ -158,10 +177,4 @@ func InitLogging() {
}
log.Trace("Log mode: %s (%s)", strings.Title(mode), strings.Title(strings.ToLower(level.String())))
}
-
- if !hasConsole {
- log.Remove(log.DefaultConsoleName)
- }
-
- Log = logConf
}