Commit 859d698b by 朱继来

Merge branch 'master' of http://119.23.72.7/sunlong_v5/golang-queue-server into…

Merge branch 'master' of http://119.23.72.7/sunlong_v5/golang-queue-server into zjl_send_email_20200102
parents a70c17c5 9f578ad4
......@@ -7,6 +7,7 @@ require (
github.com/gin-gonic/gin v1.5.0
github.com/go-sql-driver/mysql v1.4.1
github.com/ichunt2019/go-msgserver v1.0.4
github.com/ichunt2019/logger v1.0.4 // indirect
github.com/jmoiron/sqlx v1.2.0
github.com/prometheus/common v0.7.0 // indirect
github.com/tealeg/xlsx v1.0.5
......
......@@ -31,6 +31,10 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/ichunt2019/go-msgserver v1.0.4 h1:4BsRE1Ca4J0h9xyDqUF0pRMpMjDC5G7023hESrzszsY=
github.com/ichunt2019/go-msgserver v1.0.4/go.mod h1:fWAvbry0W9nhmkqgT2agwRqYCWlguUJXgy2rgoWOUmA=
github.com/ichunt2019/go-msgserver v1.0.5 h1:yanQ95Ld0etJzVhyZql0jhXXE18qGFX48qFgTCim5hg=
github.com/ichunt2019/logger v1.0.3 h1:sH4HfpzYIP9jGGx2AGqN1vRMaph299jB5/L7mwfUMwY=
github.com/ichunt2019/logger v1.0.3/go.mod h1:5IWMrrqJIWwOIGav9ACWOI+KOuYeteUvOei4zubclwg=
github.com/ichunt2019/logger v1.0.4 h1:y8xfaOLk/5Q++YBoq3x+NCf5Z4WpsQe4juCD2n/ul14=
github.com/ichunt2019/logger v1.0.4/go.mod h1:5IWMrrqJIWwOIGav9ACWOI+KOuYeteUvOei4zubclwg=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
......
package logger
import (
"fmt"
"os"
)
type ConsoleLogger struct {
level int
}
func NewConsoleLogger(config map[string]string) (log LogInterface, err error) {
logLevel, ok := config["log_level"]
if !ok {
err = fmt.Errorf("not found log_level ")
return
}
level := getLogLevel(logLevel)
log = &ConsoleLogger{
level: level,
}
return
}
func (c *ConsoleLogger) Init() {
}
func (c *ConsoleLogger) SetLevel(level int) {
if level < LogLevelDebug || level > LogLevelFatal {
level = LogLevelDebug
}
c.level = level
}
func (c *ConsoleLogger) Debug(format string, args ...interface{}) {
if c.level > LogLevelDebug {
return
}
logData := writeLog(LogLevelDebug, format, args...)
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
func (c *ConsoleLogger) Trace(format string, args ...interface{}) {
if c.level > LogLevelTrace {
return
}
logData := writeLog(LogLevelTrace, format, args...)
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
func (c *ConsoleLogger) Info(format string, args ...interface{}) {
if c.level > LogLevelInfo {
return
}
logData := writeLog(LogLevelInfo, format, args...)
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
func (c *ConsoleLogger) Warn(format string, args ...interface{}) {
if c.level > LogLevelWarn {
return
}
logData := writeLog(LogLevelWarn, format, args...)
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
func (c *ConsoleLogger) Error(format string, args ...interface{}) {
if c.level > LogLevelError {
return
}
logData := writeLog(LogLevelError, format, args...)
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
func (c *ConsoleLogger) Fatal(format string, args ...interface{}) {
if c.level > LogLevelFatal {
return
}
logData := writeLog(LogLevelFatal, format, args...)
fmt.Fprintf(os.Stdout, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
func (c *ConsoleLogger) Close() {
}
package logger
const (
LogLevelDebug = iota
LogLevelTrace
LogLevelInfo
LogLevelWarn
LogLevelError
LogLevelFatal
)
const (
LogSplitTypeHour = iota
LogSplitTypeSize
)
func getLevelText(level int) string {
switch level {
case LogLevelDebug:
return "DEBUG"
case LogLevelTrace:
return "TRACE"
case LogLevelInfo:
return "INFO"
case LogLevelWarn:
return "WARN"
case LogLevelError:
return "ERROR"
case LogLevelFatal:
return "FATAL"
}
return "UNKNOWN"
}
func getLogLevel(level string) int {
switch level {
case "debug":
return LogLevelDebug
case "trace":
return LogLevelTrace
case "info":
return LogLevelInfo
case "warn":
return LogLevelWarn
case "error":
return LogLevelError
case "fatal":
return LogLevelFatal
}
return LogLevelDebug
}
package logger
import (
"fmt"
"os"
"strconv"
"time"
)
//2018/3/26 0:01.383 DEBUG logDebug.go:29 this is a debug log
//2006-01-02 15:04:05.999
type FileLogger struct {
level int
logPath string
logName string
file *os.File
warnFile *os.File
LogDataChan chan *LogData
logSplitType int
logSplitSize int64
lastSplitHour int
}
func NewFileLogger(config map[string]string) (log LogInterface, err error) {
logPath, ok := config["log_path"]
if !ok {
err = fmt.Errorf("not found log_path ")
return
}
logName, ok := config["log_name"]
if !ok {
err = fmt.Errorf("not found log_name ")
return
}
logLevel, ok := config["log_level"]
if !ok {
err = fmt.Errorf("not found log_level ")
return
}
logChanSize, ok := config["log_chan_size"]
if !ok {
logChanSize = "50000"
}
var logSplitType int = LogSplitTypeHour
var logSplitSize int64
logSplitStr, ok := config["log_split_type"]
if !ok {
logSplitStr = "hour"
} else {
if logSplitStr == "size" {
logSplitSizeStr, ok := config["log_split_size"]
if !ok {
logSplitSizeStr = "104857600"
}
logSplitSize, err = strconv.ParseInt(logSplitSizeStr, 10, 64)
if err != nil {
logSplitSize = 104857600
}
logSplitType = LogSplitTypeSize
} else {
logSplitType = LogSplitTypeHour
}
}
chanSize, err := strconv.Atoi(logChanSize)
if err != nil {
chanSize = 50000
}
level := getLogLevel(logLevel)
log = &FileLogger{
level: level,
logPath: logPath,
logName: logName,
LogDataChan: make(chan *LogData, chanSize),
logSplitSize: logSplitSize,
logSplitType: logSplitType,
lastSplitHour: time.Now().Hour(),
}
return
}
func (f *FileLogger) Init() {
filename := fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
panic(fmt.Sprintf("open faile %s failed, err:%v", filename, err))
}
f.file = file
//写错误日志和fatal日志的文件
filename = fmt.Sprintf("%s/%s.log.wf", f.logPath, f.logName)
file, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
panic(fmt.Sprintf("open faile %s failed, err:%v", filename, err))
}
f.warnFile = file
go f.writeLogBackground()
}
func (f *FileLogger) splitFileHour(warnFile bool) {
now := time.Now()
hour := now.Hour()
if hour == f.lastSplitHour {
return
}
f.lastSplitHour = hour
var backupFilename string
var filename string
if warnFile {
backupFilename = fmt.Sprintf("%s/%s.log.wf_%04d%02d%02d%02d",
f.logPath, f.logName, now.Year(), now.Month(), now.Day(), f.lastSplitHour)
filename = fmt.Sprintf("%s/%s.log.wf", f.logPath, f.logName)
} else {
backupFilename = fmt.Sprintf("%s/%s.log_%04d%02d%02d%02d",
f.logPath, f.logName, now.Year(), now.Month(), now.Day(), f.lastSplitHour)
filename = fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
}
file := f.file
if warnFile {
file = f.warnFile
}
file.Close()
os.Rename(filename, backupFilename)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
return
}
if warnFile {
f.warnFile = file
} else {
f.file = file
}
}
func (f *FileLogger) splitFileSize(warnFile bool) {
file := f.file
if warnFile {
file = f.warnFile
}
statInfo, err := file.Stat()
if err != nil {
return
}
fileSize := statInfo.Size()
if fileSize <= f.logSplitSize {
return
}
var backupFilename string
var filename string
now := time.Now()
if warnFile {
backupFilename = fmt.Sprintf("%s/%s.log.wf_%04d%02d%02d%02d%02d%02d",
f.logPath, f.logName, now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
filename = fmt.Sprintf("%s/%s.log.wf", f.logPath, f.logName)
} else {
backupFilename = fmt.Sprintf("%s/%s.log_%04d%02d%02d%02d%02d%02d",
f.logPath, f.logName, now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
filename = fmt.Sprintf("%s/%s.log", f.logPath, f.logName)
}
file.Close()
os.Rename(filename, backupFilename)
file, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
if err != nil {
return
}
if warnFile {
f.warnFile = file
} else {
f.file = file
}
}
func (f *FileLogger) checkSplitFile(warnFile bool) {
if f.logSplitType == LogSplitTypeHour {
f.splitFileHour(warnFile)
return
}
f.splitFileSize(warnFile)
}
func (f *FileLogger) writeLogBackground() {
for logData := range f.LogDataChan {
var file *os.File = f.file
if logData.WarnAndFatal {
file = f.warnFile
}
f.checkSplitFile(logData.WarnAndFatal)
fmt.Fprintf(file, "%s %s (%s:%s:%d) %s\n", logData.TimeStr,
logData.LevelStr, logData.Filename, logData.FuncName, logData.LineNo, logData.Message)
}
}
func (f *FileLogger) SetLevel(level int) {
if level < LogLevelDebug || level > LogLevelFatal {
level = LogLevelDebug
}
f.level = level
}
func (f *FileLogger) Debug(format string, args ...interface{}) {
if f.level > LogLevelDebug {
return
}
logData := writeLog(LogLevelDebug, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}
func (f *FileLogger) Trace(format string, args ...interface{}) {
if f.level > LogLevelTrace {
return
}
logData := writeLog(LogLevelTrace, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}
func (f *FileLogger) Info(format string, args ...interface{}) {
if f.level > LogLevelInfo {
return
}
logData := writeLog(LogLevelInfo, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}
func (f *FileLogger) Warn(format string, args ...interface{}) {
if f.level > LogLevelWarn {
return
}
logData := writeLog(LogLevelWarn, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}
func (f *FileLogger) Error(format string, args ...interface{}) {
if f.level > LogLevelError {
return
}
logData := writeLog(LogLevelError, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}
func (f *FileLogger) Fatal(format string, args ...interface{}) {
if f.level > LogLevelFatal {
return
}
logData := writeLog(LogLevelFatal, format, args...)
select {
case f.LogDataChan <- logData:
default:
}
}
func (f *FileLogger) Close() {
f.file.Close()
f.warnFile.Close()
}
package logger
type LogInterface interface {
Init()
SetLevel(level int)
Debug(format string, args ...interface{})
Trace(format string, args ...interface{})
Info(format string, args ...interface{})
Warn(format string, args ...interface{})
Error(format string, args ...interface{})
Fatal(format string, args ...interface{})
Close()
}
package logger
import (
"fmt"
)
var log LogInterface
/*
file, "初始化一个文件日志实例"
console, "初始化console日志实例"
*/
func InitLogger(name string, config map[string]string) (err error) {
switch name {
case "file":
log, err = NewFileLogger(config)
case "console":
log, err = NewConsoleLogger(config)
default:
err = fmt.Errorf("unsupport logger name:%s", name)
}
return
}
func Debug(format string, args ...interface{}) {
log.Debug(format, args...)
}
func Trace(format string, args ...interface{}) {
log.Trace(format, args...)
}
func Info(format string, args ...interface{}) {
log.Info(format, args...)
}
func Warn(format string, args ...interface{}) {
log.Warn(format, args...)
}
func Error(format string, args ...interface{}) {
log.Error(format, args...)
}
func Fatal(format string, args ...interface{}) {
log.Fatal(format, args...)
}
func Init(){
log.Init()
}
package logger
import (
"fmt"
"path"
"runtime"
"time"
)
type LogData struct {
Message string
TimeStr string
LevelStr string
Filename string
FuncName string
LineNo int
WarnAndFatal bool
}
//util.go 10
func GetLineInfo() (fileName string, funcName string, lineNo int) {
pc, file, line, ok := runtime.Caller(4)
if ok {
fileName = file
funcName = runtime.FuncForPC(pc).Name()
lineNo = line
}
return
}
/*
1. 当业务调用打日志的方法时,我们把日志相关的数据写入到chan(队列)
2. 然后我们有一个后台的线程不断的从chan里面获取这些日志,最终写入到文件。
*/
func writeLog(level int, format string, args ...interface{}) *LogData {
now := time.Now()
nowStr := now.Format("2006-01-02 15:04:05.999")
levelStr := getLevelText(level)
fileName, funcName, lineNo := GetLineInfo()
fileName = path.Base(fileName)
funcName = path.Base(funcName)
msg := fmt.Sprintf(format, args...)
logData := &LogData{
Message: msg,
TimeStr: nowStr,
LevelStr: levelStr,
Filename: fileName,
FuncName: funcName,
LineNo: lineNo,
WarnAndFatal: false,
}
if level == LogLevelError || level == LogLevelWarn || level == LogLevelFatal {
logData.WarnAndFatal = true
}
return logData
//fmt.Fprintf(file, "%s %s (%s:%s:%d) %s\n", nowStr, levelStr, fileName, funcName, lineNo, msg)
}
......@@ -6,7 +6,7 @@ import (
"go-queue-server/dal/db"
"go-queue-server/order/sendPurchaseEmail/sendEmail"
"go-queue-server/util"
"go-queue-server/logger"
"github.com/ichunt2019/logger"
"sync"
"time"
"go-queue-server/dal/order/OrderActionLog"
......@@ -53,9 +53,7 @@ func main() {
//
logConfig := make(map[string]string)
logConfig["log_level"] = "ERROR"
logConfig["log_name"] = "orderSendPurchaseEmail"
logConfig["log_path"] = "logs"
logConfig["log_path"] = "logs/order/orderSendPurchaseEmail"
logConfig["log_chan_size"] = "1000"
logger.InitLogger("file",logConfig)
logger.Init()
......@@ -72,12 +70,14 @@ func main() {
util.Configs.Rabbitmq_ichunt.Dns,
}
//logger.Debug("记录日志555555555555555555555555555555")
//logger.Trace("记录日志555555555555555555555555555555")
//logger.Info("记录日志555555555555555555555555555555")
//logger.Warn("记录日志555555555555555555555555555555")
//logger.Error("记录日志555555555555555555555555555555")
//logger.Fatal("记录日志555555555555555555555555555555")
for i:=0;i<= 100;i++{
logger.Debug("Debug记录日志555555555555555555555555555555")
logger.Trace("Trace记录日志555555555555555555555555555555")
logger.Info("Info记录日志555555555555555555555555555555")
logger.Warn("Warn记录日志555555555555555555555555555555")
logger.Error("Error记录日志555555555555555555555555555555")
logger.Fatal("Fatal记录日志555555555555555555555555555555")
}
for{
var wg sync.WaitGroup
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment