Browse Source

Make Xorm log configurable (#174)

* make xorm log configable

* bug fixed for other sub commands except web

* rebase and fix xorm log

* bug fix for TrimSpace
Lunny Xiao 2 years ago
parent
commit
04fdeb9d8d
5 changed files with 231 additions and 17 deletions
  1. 2 0
      cmd/admin.go
  2. 1 1
      cmd/serve.go
  3. 5 12
      models/models.go
  4. 136 0
      modules/log/xorm.go
  5. 87 4
      modules/setting/setting.go

+ 2 - 0
cmd/admin.go

@@ -74,6 +74,8 @@ func runCreateUser(c *cli.Context) error {
74 74
 
75 75
 	setting.NewContext()
76 76
 	models.LoadConfigs()
77
+
78
+	setting.NewXORMLogService(false)
77 79
 	if err := models.SetEngine(); err != nil {
78 80
 		return fmt.Errorf("models.SetEngine: %v", err)
79 81
 	}

+ 1 - 1
cmd/serve.go

@@ -50,7 +50,6 @@ var CmdServ = cli.Command{
50 50
 func setup(logPath string) error {
51 51
 	setting.NewContext()
52 52
 	log.NewGitLogger(filepath.Join(setting.LogRootPath, logPath))
53
-
54 53
 	models.LoadConfigs()
55 54
 
56 55
 	if setting.UseSQLite3 || setting.UseTiDB {
@@ -60,6 +59,7 @@ func setup(logPath string) error {
60 59
 		}
61 60
 	}
62 61
 
62
+	setting.NewXORMLogService(true)
63 63
 	return models.SetEngine()
64 64
 }
65 65
 

+ 5 - 12
models/models.go

@@ -25,6 +25,7 @@ import (
25 25
 	_ "github.com/denisenkom/go-mssqldb"
26 26
 
27 27
 	"code.gitea.io/gitea/models/migrations"
28
+	"code.gitea.io/gitea/modules/log"
28 29
 	"code.gitea.io/gitea/modules/setting"
29 30
 )
30 31
 
@@ -226,6 +227,7 @@ func getEngine() (*xorm.Engine, error) {
226 227
 	default:
227 228
 		return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
228 229
 	}
230
+
229 231
 	return xorm.NewEngine(DbCfg.Type, connStr)
230 232
 }
231 233
 
@@ -236,6 +238,8 @@ func NewTestEngine(x *xorm.Engine) (err error) {
236 238
 		return fmt.Errorf("Connect to database: %v", err)
237 239
 	}
238 240
 
241
+	setting.NewXORMLogService(false)
242
+
239 243
 	x.SetMapper(core.GonicMapper{})
240 244
 	return x.StoreEngine("InnoDB").Sync2(tables...)
241 245
 }
@@ -248,20 +252,9 @@ func SetEngine() (err error) {
248 252
 	}
249 253
 
250 254
 	x.SetMapper(core.GonicMapper{})
251
-
252 255
 	// WARNING: for serv command, MUST remove the output to os.stdout,
253 256
 	// so use log file to instead print to stdout.
254
-	logPath := path.Join(setting.LogRootPath, "xorm.log")
255
-
256
-	if err := os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil {
257
-		return fmt.Errorf("Failed to create dir %s: %v", logPath, err)
258
-	}
259
-
260
-	f, err := os.Create(logPath)
261
-	if err != nil {
262
-		return fmt.Errorf("Failed to create xorm.log: %v", err)
263
-	}
264
-	x.SetLogger(xorm.NewSimpleLogger(f))
257
+	x.SetLogger(log.XORMLogger)
265 258
 	x.ShowSQL(true)
266 259
 	return nil
267 260
 }

+ 136 - 0
modules/log/xorm.go

@@ -0,0 +1,136 @@
1
+// Copyright 2017 The Gitea Authors. All rights reserved.
2
+// Use of this source code is governed by a MIT-style
3
+// license that can be found in the LICENSE file.
4
+
5
+package log
6
+
7
+import (
8
+	"fmt"
9
+
10
+	"github.com/go-xorm/core"
11
+)
12
+
13
+// XORMLogBridge a logger bridge from Logger to xorm
14
+type XORMLogBridge struct {
15
+	loggers []*Logger
16
+	showSQL bool
17
+	level   core.LogLevel
18
+}
19
+
20
+var (
21
+	// XORMLogger the logger for xorm
22
+	XORMLogger *XORMLogBridge
23
+)
24
+
25
+// NewXORMLogger generate logger for xorm FIXME: configable
26
+func NewXORMLogger(bufferlen int64, mode, config string) {
27
+	logger := newLogger(bufferlen)
28
+	logger.SetLogger(mode, config)
29
+	if XORMLogger == nil {
30
+		XORMLogger = &XORMLogBridge{
31
+			showSQL: true,
32
+		}
33
+	}
34
+	XORMLogger.loggers = append(XORMLogger.loggers, logger)
35
+}
36
+
37
+func (l *XORMLogBridge) writerMsg(skip, level int, msg string) error {
38
+	for _, logger := range l.loggers {
39
+		if err := logger.writerMsg(skip, level, msg); err != nil {
40
+			return err
41
+		}
42
+	}
43
+	return nil
44
+}
45
+
46
+// Debug show debug log
47
+func (l *XORMLogBridge) Debug(v ...interface{}) {
48
+	if l.level <= core.LOG_DEBUG {
49
+		msg := fmt.Sprint(v...)
50
+		l.writerMsg(0, DEBUG, "[D]"+msg)
51
+	}
52
+}
53
+
54
+// Debugf show debug log
55
+func (l *XORMLogBridge) Debugf(format string, v ...interface{}) {
56
+	if l.level <= core.LOG_DEBUG {
57
+		for _, logger := range l.loggers {
58
+			logger.Debug(format, v...)
59
+		}
60
+	}
61
+}
62
+
63
+// Error show error log
64
+func (l *XORMLogBridge) Error(v ...interface{}) {
65
+	if l.level <= core.LOG_ERR {
66
+		msg := fmt.Sprint(v...)
67
+		l.writerMsg(0, ERROR, "[E]"+msg)
68
+	}
69
+}
70
+
71
+// Errorf show error log
72
+func (l *XORMLogBridge) Errorf(format string, v ...interface{}) {
73
+	if l.level <= core.LOG_ERR {
74
+		for _, logger := range l.loggers {
75
+			logger.Error(0, format, v...)
76
+		}
77
+	}
78
+}
79
+
80
+// Info show information level log
81
+func (l *XORMLogBridge) Info(v ...interface{}) {
82
+	if l.level <= core.LOG_INFO {
83
+		msg := fmt.Sprint(v...)
84
+		l.writerMsg(0, INFO, "[I]"+msg)
85
+	}
86
+}
87
+
88
+// Infof show information level log
89
+func (l *XORMLogBridge) Infof(format string, v ...interface{}) {
90
+	if l.level <= core.LOG_INFO {
91
+		for _, logger := range l.loggers {
92
+			logger.Info(format, v...)
93
+		}
94
+	}
95
+}
96
+
97
+// Warn show warnning log
98
+func (l *XORMLogBridge) Warn(v ...interface{}) {
99
+	if l.level <= core.LOG_WARNING {
100
+		msg := fmt.Sprint(v...)
101
+		l.writerMsg(0, WARN, "[W] "+msg)
102
+	}
103
+}
104
+
105
+// Warnf show warnning log
106
+func (l *XORMLogBridge) Warnf(format string, v ...interface{}) {
107
+	if l.level <= core.LOG_WARNING {
108
+		for _, logger := range l.loggers {
109
+			logger.Warn(format, v...)
110
+		}
111
+	}
112
+}
113
+
114
+// Level get logger level
115
+func (l *XORMLogBridge) Level() core.LogLevel {
116
+	return l.level
117
+}
118
+
119
+// SetLevel set logger level
120
+func (l *XORMLogBridge) SetLevel(level core.LogLevel) {
121
+	l.level = level
122
+}
123
+
124
+// ShowSQL set if record SQL
125
+func (l *XORMLogBridge) ShowSQL(show ...bool) {
126
+	if len(show) > 0 {
127
+		l.showSQL = show[0]
128
+	} else {
129
+		l.showSQL = true
130
+	}
131
+}
132
+
133
+// IsShowSQL if record SQL
134
+func (l *XORMLogBridge) IsShowSQL() bool {
135
+	return l.showSQL
136
+}

+ 87 - 4
modules/setting/setting.go

@@ -30,6 +30,7 @@ import (
30 30
 	_ "github.com/go-macaron/cache/redis"
31 31
 	"github.com/go-macaron/session"
32 32
 	_ "github.com/go-macaron/session/redis" // redis plugin for store session
33
+	"github.com/go-xorm/core"
33 34
 	ini "gopkg.in/ini.v1"
34 35
 	"strk.kbt.io/projects/go/libravatar"
35 36
 )
@@ -944,18 +945,18 @@ func newLogService() {
944 945
 	LogConfigs = make([]string, len(LogModes))
945 946
 
946 947
 	useConsole := false
947
-	for _, mode := range LogModes {
948
-		if mode == "console" {
948
+	for i := 0; i < len(LogModes); i++ {
949
+		LogModes[i] = strings.TrimSpace(LogModes[i])
950
+		if LogModes[i] == "console" {
949 951
 			useConsole = true
950 952
 		}
951 953
 	}
954
+
952 955
 	if !useConsole {
953 956
 		log.DelLogger("console")
954 957
 	}
955 958
 
956 959
 	for i, mode := range LogModes {
957
-		mode = strings.TrimSpace(mode)
958
-
959 960
 		sec, err := Cfg.GetSection("log." + mode)
960 961
 
961 962
 		if err != nil {
@@ -1014,6 +1015,87 @@ func newLogService() {
1014 1015
 	}
1015 1016
 }
1016 1017
 
1018
+// NewXORMLogService initializes xorm logger service
1019
+func NewXORMLogService(disableConsole bool) {
1020
+	logModes := strings.Split(Cfg.Section("log").Key("MODE").MustString("console"), ",")
1021
+	var logConfigs string
1022
+	for _, mode := range logModes {
1023
+		mode = strings.TrimSpace(mode)
1024
+
1025
+		if disableConsole && mode == "console" {
1026
+			continue
1027
+		}
1028
+		sec, err := Cfg.GetSection("log." + mode)
1029
+		if err != nil {
1030
+			log.Fatal(4, "Unknown log mode: %s", mode)
1031
+		}
1032
+
1033
+		validLevels := []string{"Trace", "Debug", "Info", "Warn", "Error", "Critical"}
1034
+		// Log level.
1035
+		levelName := Cfg.Section("log."+mode).Key("LEVEL").In(
1036
+			Cfg.Section("log").Key("LEVEL").In("Trace", validLevels),
1037
+			validLevels)
1038
+		level, ok := logLevels[levelName]
1039
+		if !ok {
1040
+			log.Fatal(4, "Unknown log level: %s", levelName)
1041
+		}
1042
+
1043
+		// Generate log configuration.
1044
+		switch mode {
1045
+		case "console":
1046
+			logConfigs = fmt.Sprintf(`{"level":%s}`, level)
1047
+		case "file":
1048
+			logPath := sec.Key("FILE_NAME").MustString(path.Join(LogRootPath, "xorm.log"))
1049
+			if err = os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil {
1050
+				panic(err.Error())
1051
+			}
1052
+			logPath = filepath.Join(filepath.Dir(logPath), "xorm.log")
1053
+
1054
+			logConfigs = fmt.Sprintf(
1055
+				`{"level":%s,"filename":"%s","rotate":%v,"maxlines":%d,"maxsize":%d,"daily":%v,"maxdays":%d}`, level,
1056
+				logPath,
1057
+				sec.Key("LOG_ROTATE").MustBool(true),
1058
+				sec.Key("MAX_LINES").MustInt(1000000),
1059
+				1<<uint(sec.Key("MAX_SIZE_SHIFT").MustInt(28)),
1060
+				sec.Key("DAILY_ROTATE").MustBool(true),
1061
+				sec.Key("MAX_DAYS").MustInt(7))
1062
+		case "conn":
1063
+			logConfigs = fmt.Sprintf(`{"level":%s,"reconnectOnMsg":%v,"reconnect":%v,"net":"%s","addr":"%s"}`, level,
1064
+				sec.Key("RECONNECT_ON_MSG").MustBool(),
1065
+				sec.Key("RECONNECT").MustBool(),
1066
+				sec.Key("PROTOCOL").In("tcp", []string{"tcp", "unix", "udp"}),
1067
+				sec.Key("ADDR").MustString(":7020"))
1068
+		case "smtp":
1069
+			logConfigs = fmt.Sprintf(`{"level":%s,"username":"%s","password":"%s","host":"%s","sendTos":"%s","subject":"%s"}`, level,
1070
+				sec.Key("USER").MustString("example@example.com"),
1071
+				sec.Key("PASSWD").MustString("******"),
1072
+				sec.Key("HOST").MustString("127.0.0.1:25"),
1073
+				sec.Key("RECEIVERS").MustString("[]"),
1074
+				sec.Key("SUBJECT").MustString("Diagnostic message from serve"))
1075
+		case "database":
1076
+			logConfigs = fmt.Sprintf(`{"level":%s,"driver":"%s","conn":"%s"}`, level,
1077
+				sec.Key("DRIVER").String(),
1078
+				sec.Key("CONN").String())
1079
+		}
1080
+
1081
+		log.NewXORMLogger(Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000), mode, logConfigs)
1082
+		log.Info("XORM Log Mode: %s(%s)", strings.Title(mode), levelName)
1083
+
1084
+		var lvl core.LogLevel
1085
+		switch levelName {
1086
+		case "Trace", "Debug":
1087
+			lvl = core.LOG_DEBUG
1088
+		case "Info":
1089
+			lvl = core.LOG_INFO
1090
+		case "Warn":
1091
+			lvl = core.LOG_WARNING
1092
+		case "Error", "Critical":
1093
+			lvl = core.LOG_ERR
1094
+		}
1095
+		log.XORMLogger.SetLevel(lvl)
1096
+	}
1097
+}
1098
+
1017 1099
 func newCacheService() {
1018 1100
 	CacheAdapter = Cfg.Section("cache").Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
1019 1101
 	switch CacheAdapter {
@@ -1140,6 +1222,7 @@ func newWebhookService() {
1140 1222
 func NewServices() {
1141 1223
 	newService()
1142 1224
 	newLogService()
1225
+	NewXORMLogService(false)
1143 1226
 	newCacheService()
1144 1227
 	newSessionService()
1145 1228
 	newMailService()