PMail/server/main.go
2023-08-27 19:42:00 +08:00

90 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"bytes"
"fmt"
log "github.com/sirupsen/logrus"
"os"
"pmail/config"
"pmail/dto"
"pmail/res_init"
"time"
)
type logFormatter struct {
}
// Format 定义日志输出格式
func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
b := bytes.Buffer{}
b.WriteString(fmt.Sprintf("[%s]", entry.Level.String()))
b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05")))
if entry.Context != nil {
b.WriteString(fmt.Sprintf("[%s]", entry.Context.(*dto.Context).GetValue(dto.LogID)))
}
b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
b.WriteString(entry.Message)
b.WriteString("\n")
return b.Bytes(), nil
}
var (
gitHash string
buildTime string
goVersion string
)
func main() {
// 设置日志格式为json格式
//log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&logFormatter{})
log.SetReportCaller(true)
// 设置将日志输出到标准输出默认的输出为stderr,标准错误)
// 日志消息输出可以是任意的io.writer类型
log.SetOutput(os.Stdout)
var cst, _ = time.LoadLocation("Asia/Shanghai")
time.Local = cst
config.Init()
if config.Instance != nil {
switch config.Instance.LogLevel {
case "":
log.SetLevel(log.InfoLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.InfoLevel)
}
} else {
log.SetLevel(log.InfoLevel)
}
log.Infoln("***************************************************")
log.Infof("***\tServer Start Success Version:%s\n", config.Version)
log.Infof("***\tGit Commit Hash: %s ", gitHash)
log.Infof("***\tBuild TimeStamp: %s ", buildTime)
log.Infof("***\tBuild GoLang Version: %s ", goVersion)
log.Infoln("***************************************************")
// 定时任务启动
//go cron_server.Start()
// 核心服务启动
res_init.Init()
s := make(chan bool)
<-s
}