Commit 6d5c2e0d by 朱继来

添加日志

parents 4bd46ded 08a83b90
/config/config.toml
/.idea
/.DS_Store
/logs/*
\ No newline at end of file
异步任务处理 消息处理
配置文件修改:复制config/comfig.toml.demo config/config.toml
config.toml
[Liexin_databases]
dns="liexin:liexin#zsyM@tcp(192.168.2.232:3306)/liexin?parseTime=true"
[rabbitmq_ichunt]
queue_name="send_buyer_mail"
routing_key="send_buyer_mail"
exchange="ichunt_order_msg"
type="direct"
dns="amqp://guest:guest@192.168.2.232:5672/"
......@@ -8,5 +8,6 @@ require (
github.com/go-sql-driver/mysql v1.4.1
github.com/ichunt2019/go-msgserver v1.0.4
github.com/jmoiron/sqlx v1.2.0
github.com/prometheus/common v0.7.0 // indirect
github.com/tealeg/xlsx v1.0.5
)
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/ichunt2019/go-msgserver v1.0.4 h1:4BsRE1Ca4J0h9xyDqUF0pRMpMjDC5G7023hESrzszsY=
......@@ -17,7 +33,12 @@ github.com/ichunt2019/go-msgserver v1.0.4/go.mod h1:fWAvbry0W9nhmkqgT2agwRqYCWlg
github.com/ichunt2019/go-msgserver v1.0.5 h1:yanQ95Ld0etJzVhyZql0jhXXE18qGFX48qFgTCim5hg=
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=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
......@@ -25,21 +46,56 @@ github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdA
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.7.0 h1:L+1lyG48J1zAQXA3RBX/nG/B3gjlHq0zTt2tlbJLyCY=
github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271 h1:WhxRHzgeVGETMlmVfqhRn8RIeeNoPr2Czh33I4Zdccw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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)
}
2020-01-02 16:04:10.453 INFO (main.go:main.(*RecvPro).Consumer:25) 1111111111111111111
2020-01-02 16:04:10.465 INFO (main.go:main.(*RecvPro).Consumer:25) 1111111111111111111
2020-01-02 16:04:10.476 INFO (main.go:main.(*RecvPro).Consumer:25) 1111111111111111111
2020-01-02 16:04:10.487 INFO (main.go:main.(*RecvPro).Consumer:25) 1111111111111111111
......@@ -6,6 +6,7 @@ import (
"go-queue-server/dal/db"
"go-queue-server/order/sendPurchaseEmail/sendEmail"
"go-queue-server/util"
"go-queue-server/logger"
"sync"
"time"
)
......@@ -20,7 +21,7 @@ type RecvPro struct {
//// 实现消费者 消费消息失败 自动进入延时尝试 尝试3次之后入库db
func (t *RecvPro) Consumer(dataByte []byte) error {
// fmt.Println(string(dataByte))
logger.Info(string(dataByte))
err := sendEmail.SendPurchaseEmail(string(dataByte))
////return errors.New("顶顶顶顶")
return err
......@@ -28,9 +29,9 @@ func (t *RecvPro) Consumer(dataByte []byte) error {
//消息已经消费3次 失败了 请进行处理
func (t *RecvPro) FailAction(dataByte []byte) error {
fmt.Println(string(dataByte))
fmt.Println("任务处理失败了,我要进入db日志库了")
fmt.Println("任务处理失败了,发送钉钉消息通知主人")
logger.Error("任务处理失败了,我要进入db日志库了")
logger.Error("任务处理失败了,发送钉钉消息通知主人")
logger.Error(string(dataByte))
return nil
}
......@@ -49,6 +50,14 @@ func main() {
//初始化配置文件
util.Init()
//
logConfig := make(map[string]string)
logConfig["log_level"] = "ERROR"
logConfig["log_name"] = "orderSendPurchaseEmail"
logConfig["log_path"] = "logs"
logConfig["log_chan_size"] = "1000"
logger.InitLogger("file",logConfig)
logger.Init()
//初始化db
initDb(util.Configs.Liexin_databases.Dns)
......@@ -62,7 +71,12 @@ 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{
var wg sync.WaitGroup
......
......@@ -36,6 +36,7 @@ func Init(){
if err!=nil{
fmt.Println(err)
}
// fmt.Printf("%+v",Configs.Crm_domain)
// fmt.Printf("%+v",Configs.Rabbitmq_ichunt)
//fmt.Printf("%+v",Configs.Crm_domain)
//fmt.Printf("%+v",Configs.Rabbitmq_ichunt)
}
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