Log to different adapter according to level

This commit is contained in:
Unknown 2014-05-11 14:37:12 -04:00
parent 98eeec4cbb
commit 0970d6cc38
5 changed files with 104 additions and 69 deletions

View file

@ -143,9 +143,9 @@ HOST =
PROVIDER = file PROVIDER = file
; Provider config options ; Provider config options
; memory: not have any config yet ; memory: not have any config yet
; file: session file path, e.g. data/sessions ; file: session file path, e.g. "data/sessions"
; redis: config like redis server addr, poolSize, password, e.g. 127.0.0.1:6379,100,astaxie ; redis: config like redis server addr, poolSize, password, e.g. "127.0.0.1:6379,100,astaxie"
; mysql: go-sql-driver/mysql dsn config string, e.g. root:password@/session_table ; mysql: go-sql-driver/mysql dsn config string, e.g. "root:password@/session_table"
PROVIDER_CONFIG = data/sessions PROVIDER_CONFIG = data/sessions
; Session cookie name ; Session cookie name
COOKIE_NAME = i_like_gogits COOKIE_NAME = i_like_gogits
@ -169,6 +169,7 @@ DISABLE_GRAVATAR = false
[log] [log]
; Either "console", "file", "conn", "smtp" or "database", default is "console" ; Either "console", "file", "conn", "smtp" or "database", default is "console"
; Use comma to separate multiple modes, e.g. "console, file"
MODE = console MODE = console
; Buffer length of channel, keep it as it is if you don't know what it is. ; Buffer length of channel, keep it as it is if you don't know what it is.
BUFFER_LEN = 10000 BUFFER_LEN = 10000

View file

@ -70,8 +70,8 @@ var (
MailService *Mailer MailService *Mailer
OauthService *Oauther OauthService *Oauther
LogMode string LogModes []string
LogConfig string LogConfigs []string
Cache cache.Cache Cache cache.Cache
CacheAdapter string CacheAdapter string
@ -130,28 +130,33 @@ func newService() {
} }
func newLogService() { func newLogService() {
log.Info("%s %s", AppName, AppVer)
// Get and check log mode. // Get and check log mode.
LogMode = Cfg.MustValue("log", "MODE", "console") LogModes = strings.Split(Cfg.MustValue("log", "MODE", "console"), ",")
modeSec := "log." + LogMode LogConfigs = make([]string, len(LogModes))
for i, mode := range LogModes {
mode = strings.TrimSpace(mode)
modeSec := "log." + mode
if _, err := Cfg.GetSection(modeSec); err != nil { if _, err := Cfg.GetSection(modeSec); err != nil {
qlog.Fatalf("Unknown log mode: %s\n", LogMode) qlog.Fatalf("Unknown log mode: %s\n", mode)
} }
// Log level. // Log level.
levelName := Cfg.MustValue("log."+LogMode, "LEVEL", "Trace") levelName := Cfg.MustValue("log."+mode, "LEVEL", "Trace")
level, ok := logLevels[levelName] level, ok := logLevels[levelName]
if !ok { if !ok {
qlog.Fatalf("Unknown log level: %s\n", levelName) qlog.Fatalf("Unknown log level: %s\n", levelName)
} }
// Generate log configuration. // Generate log configuration.
switch LogMode { switch mode {
case "console": case "console":
LogConfig = fmt.Sprintf(`{"level":%s}`, level) LogConfigs[i] = fmt.Sprintf(`{"level":%s}`, level)
case "file": case "file":
logPath := Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log") logPath := Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log")
os.MkdirAll(path.Dir(logPath), os.ModePerm) os.MkdirAll(path.Dir(logPath), os.ModePerm)
LogConfig = fmt.Sprintf( LogConfigs[i] = fmt.Sprintf(
`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level, `{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
logPath, logPath,
Cfg.MustBool(modeSec, "LOG_ROTATE", true), Cfg.MustBool(modeSec, "LOG_ROTATE", true),
@ -160,27 +165,27 @@ func newLogService() {
Cfg.MustBool(modeSec, "DAILY_ROTATE", true), Cfg.MustBool(modeSec, "DAILY_ROTATE", true),
Cfg.MustInt(modeSec, "MAX_DAYS", 7)) Cfg.MustInt(modeSec, "MAX_DAYS", 7))
case "conn": case "conn":
LogConfig = fmt.Sprintf(`{"level":"%s","reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level, LogConfigs[i] = fmt.Sprintf(`{"level":"%s","reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level,
Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false), Cfg.MustBool(modeSec, "RECONNECT_ON_MSG", false),
Cfg.MustBool(modeSec, "RECONNECT", false), Cfg.MustBool(modeSec, "RECONNECT", false),
Cfg.MustValue(modeSec, "PROTOCOL", "tcp"), Cfg.MustValue(modeSec, "PROTOCOL", "tcp"),
Cfg.MustValue(modeSec, "ADDR", ":7020")) Cfg.MustValue(modeSec, "ADDR", ":7020"))
case "smtp": case "smtp":
LogConfig = fmt.Sprintf(`{"level":"%s","username":"%s","password":"%s","host":"%s","sendTos":"%s","subject":"%s"}`, level, LogConfigs[i] = fmt.Sprintf(`{"level":"%s","username":"%s","password":"%s","host":"%s","sendTos":"%s","subject":"%s"}`, level,
Cfg.MustValue(modeSec, "USER", "example@example.com"), Cfg.MustValue(modeSec, "USER", "example@example.com"),
Cfg.MustValue(modeSec, "PASSWD", "******"), Cfg.MustValue(modeSec, "PASSWD", "******"),
Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"), Cfg.MustValue(modeSec, "HOST", "127.0.0.1:25"),
Cfg.MustValue(modeSec, "RECEIVERS", "[]"), Cfg.MustValue(modeSec, "RECEIVERS", "[]"),
Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve")) Cfg.MustValue(modeSec, "SUBJECT", "Diagnostic message from serve"))
case "database": case "database":
LogConfig = fmt.Sprintf(`{"level":"%s","driver":"%s","conn":"%s"}`, level, LogConfigs[i] = fmt.Sprintf(`{"level":"%s","driver":"%s","conn":"%s"}`, level,
Cfg.MustValue(modeSec, "Driver"), Cfg.MustValue(modeSec, "Driver"),
Cfg.MustValue(modeSec, "CONN")) Cfg.MustValue(modeSec, "CONN"))
} }
log.Info("%s %s", AppName, AppVer) log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), mode, LogConfigs[i])
log.NewLogger(Cfg.MustInt64("log", "BUFFER_LEN", 10000), LogMode, LogConfig) log.Info("Log Mode: %s(%s)", strings.Title(mode), levelName)
log.Info("Log Mode: %s(%s)", strings.Title(LogMode), levelName) }
} }
func newLdapService() { func newLdapService() {

View file

@ -10,8 +10,7 @@ import (
) )
var ( var (
logger *logs.BeeLogger loggers []*logs.BeeLogger
Mode, Config string
) )
func init() { func init() {
@ -19,32 +18,54 @@ func init() {
} }
func NewLogger(bufLen int64, mode, config string) { func NewLogger(bufLen int64, mode, config string) {
Mode, Config = mode, config logger := logs.NewLogger(bufLen)
logger = logs.NewLogger(bufLen)
isExist := false
for _, l := range loggers {
if l.Adapter == mode {
isExist = true
l = logger
}
}
if !isExist {
loggers = append(loggers, logger)
}
logger.SetLogFuncCallDepth(3) logger.SetLogFuncCallDepth(3)
logger.SetLogger(mode, config) logger.SetLogger(mode, config)
} }
func Trace(format string, v ...interface{}) { func Trace(format string, v ...interface{}) {
for _, logger := range loggers {
logger.Trace(format, v...) logger.Trace(format, v...)
} }
}
func Debug(format string, v ...interface{}) { func Debug(format string, v ...interface{}) {
for _, logger := range loggers {
logger.Debug(format, v...) logger.Debug(format, v...)
} }
}
func Info(format string, v ...interface{}) { func Info(format string, v ...interface{}) {
for _, logger := range loggers {
logger.Info(format, v...) logger.Info(format, v...)
} }
}
func Error(format string, v ...interface{}) { func Error(format string, v ...interface{}) {
for _, logger := range loggers {
logger.Error(format, v...) logger.Error(format, v...)
} }
}
func Warn(format string, v ...interface{}) { func Warn(format string, v ...interface{}) {
for _, logger := range loggers {
logger.Warn(format, v...) logger.Warn(format, v...)
} }
}
func Critical(format string, v ...interface{}) { func Critical(format string, v ...interface{}) {
for _, logger := range loggers {
logger.Critical(format, v...) logger.Critical(format, v...)
} }
}

View file

@ -211,8 +211,14 @@ func Config(ctx *middleware.Context) {
ctx.Data["PictureService"] = base.PictureService ctx.Data["PictureService"] = base.PictureService
ctx.Data["DisableGravatar"] = base.DisableGravatar ctx.Data["DisableGravatar"] = base.DisableGravatar
ctx.Data["LogMode"] = base.LogMode type logger struct {
ctx.Data["LogConfig"] = base.LogConfig Mode, Config string
}
loggers := make([]*logger, len(base.LogModes))
for i := range base.LogModes {
loggers[i] = &logger{base.LogModes[i], base.LogConfigs[i]}
}
ctx.Data["Loggers"] = loggers
ctx.HTML(200, "admin/config") ctx.HTML(200, "admin/config")
} }

View file

@ -194,12 +194,14 @@
<div class="panel-body"> <div class="panel-body">
<dl class="dl-horizontal admin-dl-horizontal"> <dl class="dl-horizontal admin-dl-horizontal">
{{range .Loggers}}
<dt>Log Mode</dt> <dt>Log Mode</dt>
<dd>{{.LogMode}}</dd> <dd>{{.Mode}}</dd>
<dt>Log Config</dt> <dt>Log Config</dt>
<dd> <dd>
<div style="padding-top: 5px;"><pre>{{.LogConfig}}</pre></div> <div style="padding-top: 5px;"><pre>{{.Config}}</pre></div>
</dd> </dd>
{{end}}
</dl> </dl>
</div> </div>