2017-04-03 07:27:19 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cihub/seelog"
|
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-05-02 08:17:34 +00:00
|
|
|
"runtime"
|
2017-04-03 07:27:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 日志库
|
|
|
|
|
|
|
|
type Level int8
|
|
|
|
|
|
|
|
var logger seelog.LoggerInterface
|
|
|
|
|
|
|
|
const (
|
|
|
|
DEBUG = iota
|
|
|
|
INFO
|
|
|
|
WARN
|
|
|
|
ERROR
|
|
|
|
FATAL
|
|
|
|
)
|
|
|
|
|
|
|
|
func InitLogger() {
|
|
|
|
config := getLogConfig()
|
|
|
|
l, err := seelog.LoggerFromConfigAsString(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
logger = l
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(v ...interface{}) {
|
2017-05-02 08:17:34 +00:00
|
|
|
if macaron.Env != macaron.DEV {
|
|
|
|
return
|
|
|
|
}
|
2017-04-03 07:27:19 +00:00
|
|
|
write(DEBUG, v)
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
func Debugf(format string, v ...interface{}) {
|
2017-05-02 08:17:34 +00:00
|
|
|
if macaron.Env != macaron.DEV {
|
|
|
|
return
|
|
|
|
}
|
2017-04-21 09:41:59 +00:00
|
|
|
writef(DEBUG, format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 07:27:19 +00:00
|
|
|
func Info(v ...interface{}) {
|
|
|
|
write(INFO, v)
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
func Infof(format string, v ...interface{}) {
|
|
|
|
writef(INFO, format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 07:27:19 +00:00
|
|
|
func Warn(v ...interface{}) {
|
|
|
|
write(WARN, v)
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
func Warnf(format string, v ...interface{}) {
|
|
|
|
writef(WARN, format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 07:27:19 +00:00
|
|
|
func Error(v ...interface{}) {
|
|
|
|
write(ERROR, v)
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
writef(ERROR, format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 07:27:19 +00:00
|
|
|
func Fatal(v ...interface{}) {
|
|
|
|
write(FATAL, v)
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
func Fatalf(format string, v ...interface{}) {
|
|
|
|
writef(FATAL, format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
func write(level Level, v ...interface{}) {
|
2017-04-03 07:27:19 +00:00
|
|
|
defer logger.Flush()
|
|
|
|
|
2017-05-02 08:17:34 +00:00
|
|
|
content := ""
|
2017-05-04 10:05:25 +00:00
|
|
|
if macaron.Env == macaron.DEV {
|
|
|
|
pc, file, line, ok := runtime.Caller(2)
|
|
|
|
if ok {
|
|
|
|
content = fmt.Sprintf("#%s#%s#%d行#", file, runtime.FuncForPC(pc).Name(), line)
|
|
|
|
}
|
2017-05-02 08:17:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 07:27:19 +00:00
|
|
|
switch level {
|
|
|
|
case DEBUG:
|
2017-05-02 08:17:34 +00:00
|
|
|
logger.Debug(content, v)
|
2017-04-03 07:27:19 +00:00
|
|
|
case INFO:
|
2017-05-02 08:17:34 +00:00
|
|
|
logger.Info(content, v)
|
2017-04-03 07:27:19 +00:00
|
|
|
case WARN:
|
2017-05-02 08:17:34 +00:00
|
|
|
logger.Warn(content, v)
|
2017-04-03 07:27:19 +00:00
|
|
|
case FATAL:
|
2017-05-02 08:17:34 +00:00
|
|
|
logger.Critical(content, v)
|
2017-04-03 07:27:19 +00:00
|
|
|
os.Exit(1)
|
|
|
|
case ERROR:
|
2017-05-02 08:17:34 +00:00
|
|
|
logger.Error(content, v)
|
2017-04-03 07:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 09:41:59 +00:00
|
|
|
func writef(level Level, format string, v ...interface{}) {
|
2017-04-14 13:46:02 +00:00
|
|
|
defer logger.Flush()
|
|
|
|
|
2017-05-02 08:17:34 +00:00
|
|
|
content := ""
|
2017-05-04 10:05:25 +00:00
|
|
|
if macaron.Env == macaron.DEV {
|
|
|
|
pc, file, line, ok := runtime.Caller(2)
|
|
|
|
if ok {
|
|
|
|
content = fmt.Sprintf("#%s#%s#%d行#", file, runtime.FuncForPC(pc).Name(), line)
|
|
|
|
}
|
2017-05-02 08:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format = content + format
|
|
|
|
|
2017-04-14 13:46:02 +00:00
|
|
|
switch level {
|
|
|
|
case DEBUG:
|
2017-04-21 09:41:59 +00:00
|
|
|
logger.Debugf(format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
case INFO:
|
2017-04-21 09:41:59 +00:00
|
|
|
logger.Infof(format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
case WARN:
|
2017-04-21 09:41:59 +00:00
|
|
|
logger.Warnf(format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
case FATAL:
|
2017-04-21 09:41:59 +00:00
|
|
|
logger.Criticalf(format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
os.Exit(1)
|
|
|
|
case ERROR:
|
2017-04-21 09:41:59 +00:00
|
|
|
logger.Errorf(format, v...)
|
2017-04-14 13:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 07:27:19 +00:00
|
|
|
func getLogConfig() string {
|
|
|
|
config := `
|
|
|
|
<seelog>
|
|
|
|
<outputs formatid="main">
|
|
|
|
%s
|
|
|
|
<filter levels="info,critical,error,warn">
|
|
|
|
<file path="log/cron.log" />
|
|
|
|
</filter>
|
|
|
|
</outputs>
|
|
|
|
<formats>
|
|
|
|
<format id="main" format="%%Date/%%Time [%%LEV] %%Msg%%n"/>
|
|
|
|
</formats>
|
|
|
|
</seelog>`
|
|
|
|
|
|
|
|
consoleConfig := ""
|
|
|
|
if macaron.Env == macaron.DEV {
|
|
|
|
consoleConfig =
|
|
|
|
`
|
|
|
|
<filter levels="info,debug,critical,warn,error">
|
|
|
|
<console />
|
|
|
|
</filter>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
config = fmt.Sprintf(config, consoleConfig)
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|