Browse Source

Add complete log configuration

Unknown 11 years ago
parent
commit
cef9bbd530
4 changed files with 64 additions and 11 deletions
  1. 6 6
      conf/app.ini
  2. 1 1
      gogs.go
  3. 54 1
      modules/base/conf.go
  4. 3 3
      modules/log/log.go

+ 6 - 6
conf/app.ini

@@ -47,20 +47,20 @@ BUFFER_LEN = 10000
 ; Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "Trace"
 LEVEL = Trace
 
-; For "console" mode only, all log files will be stored in "/log" directory
+; For "console" mode only
 [log.console]
 LEVEL = 
 
 ; For "file" mode only
 [log.file]
 LEVEL = 
-FILE_NAME = "gogs.log"
+FILE_NAME = log/gogs.log
 ; This enables automated log rotate(switch of following options), default is true
-LOG_ROTATE = 
+LOG_ROTATE = true
 ; Max line number of single file, default is 1000000
 MAX_LINES = 1000000
-; Max size of single file, default is 1 << 28, 256MB
-MAX_SIZE = 1 << 28
+; Max size shift of single file, default is 28 means 1 << 28, 256MB
+MAX_SIZE_SHIFT = 28
 ; Segment log daily, default is true
 DAILY_ROTATE = true
 ; Expired days of log file(delete after max days), default is 7
@@ -88,5 +88,5 @@ HOST =
 ; Mailer user name and password
 USER = 
 PASSWD =
-; Receivers, can be one or more
+; Receivers, can be one or more, e.g. ["[email protected]","[email protected]"]
 RECEIVERS = 

+ 1 - 1
gogs.go

@@ -20,7 +20,7 @@ import (
 // Test that go1.1 tag above is included in builds. main.go refers to this definition.
 const go11tag = true
 
-const APP_VER = "0.1.0.0318.1"
+const APP_VER = "0.1.0.0319.1"
 
 func init() {
 	base.AppVer = APP_VER

+ 54 - 1
modules/base/conf.go

@@ -44,8 +44,61 @@ func exeDir() (string, error) {
 	return path.Dir(p), nil
 }
 
+var logLevels = map[string]string{
+	"Trace":    "0",
+	"Debug":    "1",
+	"Info":     "2",
+	"Warn":     "3",
+	"Error":    "4",
+	"Critical": "5",
+}
+
 func newLogService() {
-	log.NewLogger()
+	// Get and check log mode.
+	mode := Cfg.MustValue("log", "MODE", "console")
+	modeSec := "log." + mode
+	if _, err := Cfg.GetSection(modeSec); err != nil {
+		fmt.Printf("Unknown log mode: %s\n", mode)
+		os.Exit(2)
+	}
+
+	// Log level.
+	level, ok := logLevels[Cfg.MustValue("log."+mode, "LEVEL", "Trace")]
+	if !ok {
+		fmt.Printf("Unknown log level: %s\n", Cfg.MustValue("log."+mode, "LEVEL", "Trace"))
+		os.Exit(2)
+	}
+
+	// Generate log configuration.
+	var config string
+	switch mode {
+	case "console":
+		config = fmt.Sprintf(`{"level":%s}`, level)
+	case "file":
+		config = fmt.Sprintf(
+			`{"level":%s,"filename":%s,"rotate":%v,"maxlines":%d,"maxsize",%d,"daily":%v,"maxdays":%d}`, level,
+			Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log"),
+			Cfg.MustBool(modeSec, "LOG_ROTATE", true),
+			Cfg.MustInt(modeSec, "MAX_LINES", 1000000),
+			1<<uint(Cfg.MustInt(modeSec, "MAX_SIZE_SHIFT", 28)),
+			Cfg.MustBool(modeSec, "DAILY_ROTATE", true),
+			Cfg.MustInt(modeSec, "MAX_DAYS", 7))
+	case "conn":
+		config = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":%s,"addr":%s}`, level,
+			Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false),
+			Cfg.MustBool(modeSec, "RECONNECT", false),
+			Cfg.MustValue(modeSec, "PROTOCOL", "tcp"),
+			Cfg.MustValue(modeSec, "ADDR", ":7020"))
+	case "smtp":
+		config = fmt.Sprintf(`{"level":%s,"username":%s,"password":%s,"host":%s,"sendTos":%s,"subject":%s}`, level,
+			Cfg.MustValue(modeSec, "USER", "[email protected]"),
+			Cfg.MustValue(modeSec, "PASSWD", "******"),
+			Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"),
+			Cfg.MustValue(modeSec, "RECEIVERS", "[]"),
+			Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve"))
+	}
+
+	log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), mode, config)
 }
 
 func newMailService() {

+ 3 - 3
modules/log/log.go

@@ -11,9 +11,9 @@ import (
 
 var logger *logs.BeeLogger
 
-func NewLogger() {
-	logger = logs.NewLogger(10000)
-	logger.SetLogger("console", "")
+func NewLogger(bufLen int64, mode, config string) {
+	logger = logs.NewLogger(bufLen)
+	logger.SetLogger(mode, config)
 }
 
 func Trace(format string, v ...interface{}) {