Go 日志配置
package logger
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/sirupsen/logrus"
)
var loglevelmap = map[string]logrus.Level{
"panic": logrus.PanicLevel,
"fatal": logrus.FatalLevel,
"error": logrus.ErrorLevel,
"warn": logrus.WarnLevel,
"info": logrus.InfoLevel,
"debug": logrus.DebugLevel,
"trace": logrus.TraceLevel,
}
const birthdayGolang = "2006-01-02 15:04:05"
func LogTemp(format string, a ...interface{}) (n int, err error) {
fileLineInfo := ":"
_, file, line, ok := runtime.Caller(1)
if ok {
tmp := strings.Split(file, "/")
fileLineInfo = fmt.Sprintf("%s:%d", tmp[len(tmp)-1], line)
}
prefix := fmt.Sprintf("[%s] [%s] ", time.Now().Format(birthdayGolang), fileLineInfo)
return fmt.Printf(prefix+format+"\n", a...)
}
func getFileLineInfo() string {
fileLineInfo := "[:] "
_, file, line, ok := runtime.Caller(3)
if ok {
tmp := strings.Split(file, "/")
fileLineInfo = fmt.Sprintf("[%s:%d] ", tmp[len(tmp)-1], line)
}
return fileLineInfo
}
type commonLoger struct {
log *logrus.Logger
}
var defaultLogger = &commonLoger{}
func (c *commonLoger) Initialize(level string, logFilePath string, output string) error {
c.log = logrus.New()
c.SetLevel(level)
c.log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: birthdayGolang,
})
switch output {
case "stdout":
c.log.SetOutput(os.Stdout)
case "stderr":
c.log.SetOutput(os.Stderr)
default:
dir := filepath.Dir(logFilePath)
fDir, err := os.Stat(dir)
if err == nil {
if !fDir.IsDir() {
return fmt.Errorf("log path [%s] exists, but is not dir", dir)
}
} else {
if os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
return err
}
}
f, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
c.log.SetOutput(f)
}
return nil
}
func (c *commonLoger) SetLogger(adapterName string, configs ...string) error {
return nil
}
func (c *commonLoger) SetLevel(level string) {
c.log.SetLevel(loglevelmap[strings.ToLower(level)])
}
func (c *commonLoger) Panic(format string, args ...interface{}) {
c.log.Panicf(getFileLineInfo()+format, args...)
}
func (c *commonLoger) Fatal(format string, args ...interface{}) {
c.log.Fatalf(getFileLineInfo()+format, args...)
}
func (c *commonLoger) Error(format string, args ...interface{}) {
c.log.Errorf(getFileLineInfo()+format, args...)
}
func (c *commonLoger) Warn(format string, args ...interface{}) {
c.log.Warnf(getFileLineInfo()+format, args...)
}
func (c *commonLoger) Info(format string, args ...interface{}) {
c.log.Infof(getFileLineInfo()+format, args...)
}
func (c *commonLoger) Debug(format string, args ...interface{}) {
c.log.Debugf(getFileLineInfo()+format, args...)
}
func (c *commonLoger) Trace(format string, args ...interface{}) {
c.log.Tracef(getFileLineInfo()+format, args...)
}
func Initialize(level string, logFilePath string, output string) error {
return defaultLogger.Initialize(level, logFilePath, output)
}
func SetLevel(level string) {
defaultLogger.log.SetLevel(loglevelmap[level])
}
func Panic(format string, args ...interface{}) {
defaultLogger.Panic(format, args...)
}
func Fatal(format string, args ...interface{}) {
defaultLogger.Fatal(format, args...)
}
func Error(format string, args ...interface{}) {
defaultLogger.Error(format, args...)
}
func Warn(format string, args ...interface{}) {
defaultLogger.Warn(format, args...)
}
func Info(format string, args ...interface{}) {
defaultLogger.Info(format, args...)
}
func Debug(format string, args ...interface{}) {
defaultLogger.Debug(format, args...)
}
func Trace(format string, args ...interface{}) {
defaultLogger.Trace(format, args...)
}
调用
logger.Debug("debug !!!")
logger.Info("info !!!")
logger.Warn("warning !!!")
logger.Error("error !!!")