-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Make Xorm log configurable #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package log | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-xorm/core" | ||
) | ||
|
||
// XORMLogBridge a logger bridge from Logger to xorm | ||
type XORMLogBridge struct { | ||
loggers []*Logger | ||
showSQL bool | ||
level core.LogLevel | ||
} | ||
|
||
var ( | ||
// XORMLogger the logger for xorm | ||
XORMLogger *XORMLogBridge | ||
) | ||
|
||
// NewXORMLogger generate logger for xorm FIXME: configable | ||
func NewXORMLogger(bufferlen int64, mode, config string) { | ||
logger := newLogger(bufferlen) | ||
logger.SetLogger(mode, config) | ||
if XORMLogger == nil { | ||
XORMLogger = &XORMLogBridge{ | ||
showSQL: true, | ||
} | ||
} | ||
XORMLogger.loggers = append(XORMLogger.loggers, logger) | ||
} | ||
|
||
func (l *XORMLogBridge) writerMsg(skip, level int, msg string) error { | ||
for _, logger := range l.loggers { | ||
if err := logger.writerMsg(skip, level, msg); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Debug show debug log | ||
func (l *XORMLogBridge) Debug(v ...interface{}) { | ||
if l.level <= core.LOG_DEBUG { | ||
msg := fmt.Sprint(v...) | ||
l.writerMsg(0, DEBUG, "[D]"+msg) | ||
} | ||
} | ||
|
||
// Debugf show debug log | ||
func (l *XORMLogBridge) Debugf(format string, v ...interface{}) { | ||
if l.level <= core.LOG_DEBUG { | ||
for _, logger := range l.loggers { | ||
logger.Debug(format, v...) | ||
} | ||
} | ||
} | ||
|
||
// Error show error log | ||
func (l *XORMLogBridge) Error(v ...interface{}) { | ||
if l.level <= core.LOG_ERR { | ||
msg := fmt.Sprint(v...) | ||
l.writerMsg(0, ERROR, "[E]"+msg) | ||
} | ||
} | ||
|
||
// Errorf show error log | ||
func (l *XORMLogBridge) Errorf(format string, v ...interface{}) { | ||
if l.level <= core.LOG_ERR { | ||
for _, logger := range l.loggers { | ||
logger.Error(0, format, v...) | ||
} | ||
} | ||
} | ||
|
||
// Info show information level log | ||
func (l *XORMLogBridge) Info(v ...interface{}) { | ||
if l.level <= core.LOG_INFO { | ||
msg := fmt.Sprint(v...) | ||
l.writerMsg(0, INFO, "[I]"+msg) | ||
} | ||
} | ||
|
||
// Infof show information level log | ||
func (l *XORMLogBridge) Infof(format string, v ...interface{}) { | ||
if l.level <= core.LOG_INFO { | ||
for _, logger := range l.loggers { | ||
logger.Info(format, v...) | ||
} | ||
} | ||
} | ||
|
||
// Warn show warnning log | ||
func (l *XORMLogBridge) Warn(v ...interface{}) { | ||
if l.level <= core.LOG_WARNING { | ||
msg := fmt.Sprint(v...) | ||
l.writerMsg(0, WARN, "[W] "+msg) | ||
} | ||
} | ||
|
||
// Warnf show warnning log | ||
func (l *XORMLogBridge) Warnf(format string, v ...interface{}) { | ||
if l.level <= core.LOG_WARNING { | ||
for _, logger := range l.loggers { | ||
logger.Warn(format, v...) | ||
} | ||
} | ||
} | ||
|
||
// Level get logger level | ||
func (l *XORMLogBridge) Level() core.LogLevel { | ||
return l.level | ||
} | ||
|
||
// SetLevel set logger level | ||
func (l *XORMLogBridge) SetLevel(level core.LogLevel) { | ||
l.level = level | ||
} | ||
|
||
// ShowSQL set if record SQL | ||
func (l *XORMLogBridge) ShowSQL(show ...bool) { | ||
if len(show) > 0 { | ||
l.showSQL = show[0] | ||
} else { | ||
l.showSQL = true | ||
} | ||
} | ||
|
||
// IsShowSQL if record SQL | ||
func (l *XORMLogBridge) IsShowSQL() bool { | ||
return l.showSQL | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import ( | |
_ "github.com/go-macaron/cache/redis" | ||
"github.com/go-macaron/session" | ||
_ "github.com/go-macaron/session/redis" // redis plugin for store session | ||
"github.com/go-xorm/core" | ||
ini "gopkg.in/ini.v1" | ||
"strk.kbt.io/projects/go/libravatar" | ||
) | ||
|
@@ -944,18 +945,18 @@ func newLogService() { | |
LogConfigs = make([]string, len(LogModes)) | ||
|
||
useConsole := false | ||
for _, mode := range LogModes { | ||
if mode == "console" { | ||
for i := 0; i < len(LogModes); i++ { | ||
LogModes[i] = strings.TrimSpace(LogModes[i]) | ||
if LogModes[i] == "console" { | ||
useConsole = true | ||
} | ||
} | ||
|
||
if !useConsole { | ||
log.DelLogger("console") | ||
} | ||
|
||
for i, mode := range LogModes { | ||
mode = strings.TrimSpace(mode) | ||
|
||
sec, err := Cfg.GetSection("log." + mode) | ||
|
||
if err != nil { | ||
|
@@ -1014,6 +1015,87 @@ func newLogService() { | |
} | ||
} | ||
|
||
// NewXORMLogService initializes xorm logger service | ||
func NewXORMLogService(disableConsole bool) { | ||
logModes := strings.Split(Cfg.Section("log").Key("MODE").MustString("console"), ",") | ||
var logConfigs string | ||
for _, mode := range logModes { | ||
mode = strings.TrimSpace(mode) | ||
|
||
if disableConsole && mode == "console" { | ||
continue | ||
} | ||
sec, err := Cfg.GetSection("log." + mode) | ||
if err != nil { | ||
log.Fatal(4, "Unknown log mode: %s", mode) | ||
} | ||
|
||
validLevels := []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"} | ||
// Log level. | ||
levelName := Cfg.Section("log."+mode).Key("LEVEL").In( | ||
Cfg.Section("log").Key("LEVEL").In("Trace", validLevels), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trace is also part of the slice above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is the default log level |
||
validLevels) | ||
level, ok := logLevels[levelName] | ||
if !ok { | ||
log.Fatal(4, "Unknown log level: %s", levelName) | ||
} | ||
|
||
// Generate log configuration. | ||
switch mode { | ||
case "console": | ||
logConfigs = fmt.Sprintf(`{"level":%s}`, level) | ||
case "file": | ||
logPath := sec.Key("FILE_NAME").MustString(path.Join(LogRootPath, "xorm.log")) | ||
if err = os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil { | ||
panic(err.Error()) | ||
} | ||
logPath = filepath.Join(filepath.Dir(logPath), "xorm.log") | ||
|
||
logConfigs = fmt.Sprintf( | ||
`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level, | ||
logPath, | ||
sec.Key("LOG_ROTATE").MustBool(true), | ||
sec.Key("MAX_LINES").MustInt(1000000), | ||
1<<uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)), | ||
sec.Key("DAILY_ROTATE").MustBool(true), | ||
sec.Key("MAX_DAYS").MustInt(7)) | ||
case "conn": | ||
logConfigs = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level, | ||
sec.Key("RECONNECT_ON_MSG").MustBool(), | ||
sec.Key("RECONNECT").MustBool(), | ||
sec.Key("PROTOCOL").In("tcp", []string{"tcp", "unix", "udp"}), | ||
sec.Key("ADDR").MustString(":7020")) | ||
case "smtp": | ||
logConfigs = fmt.Sprintf(`{"level":%s,"username":"%s","password":"%s","host":"%s","sendTos":"%s","subject":"%s"}`, level, | ||
sec.Key("USER").MustString("example@example.com"), | ||
sec.Key("PASSWD").MustString("******"), | ||
sec.Key("HOST").MustString("127.0.0.1:25"), | ||
sec.Key("RECEIVERS").MustString("[]"), | ||
sec.Key("SUBJECT").MustString("Diagnostic message from serve")) | ||
case "database": | ||
logConfigs = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level, | ||
sec.Key("DRIVER").String(), | ||
sec.Key("CONN").String()) | ||
} | ||
|
||
log.NewXORMLogger(Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000), mode, logConfigs) | ||
log.Info("XORM Log Mode: %s(%s)", strings.Title(mode), levelName) | ||
|
||
var lvl core.LogLevel | ||
switch levelName { | ||
case "Trace", "Debug": | ||
lvl = core.LOG_DEBUG | ||
case "Info": | ||
lvl = core.LOG_INFO | ||
case "Warn": | ||
lvl = core.LOG_WARNING | ||
case "Error", "Critical": | ||
lvl = core.LOG_ERR | ||
} | ||
log.XORMLogger.SetLevel(lvl) | ||
} | ||
} | ||
|
||
func newCacheService() { | ||
CacheAdapter = Cfg.Section("cache").Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"}) | ||
switch CacheAdapter { | ||
|
@@ -1140,6 +1222,7 @@ func newWebhookService() { | |
func NewServices() { | ||
newService() | ||
newLogService() | ||
NewXORMLogService(false) | ||
newCacheService() | ||
newSessionService() | ||
newMailService() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copyright?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.