Skip to content

Commit

Permalink
chore: add zap logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayheaven committed Jan 17, 2024
1 parent 6a41869 commit 1c2401e
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 35 deletions.
1 change: 1 addition & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func start() {

// init logger
logger.Init()

logger := logger.Logger

// load env config
Expand Down
27 changes: 25 additions & 2 deletions configs/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@
server:
port: ":8080"

# mysql config
# jwt
jwt:
secret: secret

# mysql
mysql:
dsn: "root:123456@tcp(127.0.0.1:3306)/beta?charset=utf8mb4&parseTime=True&loc=Local"
host: ec2-13-251-219-215.ap-southeast-1.compute.amazonaws.com
port: 2306
user: root
password: 123456
database: beta

# redis
redis:
host: ec2-13-251-219-215.ap-southeast-1.compute.amazonaws.com
port: 6379
password: 123456
db: 0

# mongo
# mongo:
# host: ec2-13-251-219-215.ap-southeast-1.compute.amazonaws.com
# port: 12017
# user: beta
# password: Hdx960201
# database: beta
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.2
go.mongodb.org/mongo-driver v1.13.1
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.18.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/driver/mysql v1.5.2
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7Jul
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk=
go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
Expand Down
33 changes: 0 additions & 33 deletions internal/pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,34 +1 @@
package logger

import (
"io"
"os"

"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)

var Logger *logrus.Logger

func Init() *logrus.Logger {
logger := logrus.New()

logger.SetLevel(logrus.TraceLevel)
logger.SetFormatter(&logrus.JSONFormatter{})
// logger.SetReportCaller(true)

logFile := &lumberjack.Logger{
Filename: "logs/app.log",
MaxSize: 10,
Compress: true,
}

// multi writer, both file and stdout
writers := []io.Writer{logFile, os.Stdout}
logger.SetOutput(io.MultiWriter(writers...))

// export Logger
Logger = logger

return logger
}
36 changes: 36 additions & 0 deletions internal/pkg/logger/logrus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package logger

import (
"io"
"os"

"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)

var Logger *logrus.Logger

var LogrusLogger *logrus.Logger

func Init() *logrus.Logger {
logger := logrus.New()

logger.SetLevel(logrus.TraceLevel)
logger.SetFormatter(&logrus.JSONFormatter{})
// logger.SetReportCaller(true)

logFile := &lumberjack.Logger{
Filename: "logs/app.log",
MaxSize: 10,
Compress: true,
}

// multi writer, both file and stdout
writers := []io.Writer{logFile, os.Stdout}
logger.SetOutput(io.MultiWriter(writers...))

// export Logger
LogrusLogger = logger

return logger
}
65 changes: 65 additions & 0 deletions internal/pkg/logger/zap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package logger

import (
"fmt"
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

var (
ZapLogger *zap.Logger
ZapSugar *zap.SugaredLogger
)

func InitZapLogger() {

encoder := getEncoder()

// First, define our level-handling logic.

// level: debug,info,warning
infoLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
// level: error, dpanic, panic, fatal
errorLevel := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})

infoLevel_writerSyncer := getWriterSyncer("info")
errorLevel_writerSyncer := getWriterSyncer("error")

info_multiWriteSyncer := zapcore.NewMultiWriteSyncer(infoLevel_writerSyncer, os.Stdout)
error_multiWriteSyncer := zapcore.NewMultiWriteSyncer(errorLevel_writerSyncer, os.Stdout)

core := zapcore.NewCore(encoder, info_multiWriteSyncer, infoLevel)
errorCore := zapcore.NewCore(encoder, error_multiWriteSyncer, errorLevel)

coreArr := []zapcore.Core{core, errorCore}

// export
ZapLogger = zap.New(zapcore.NewTee(coreArr...), zap.AddCaller()) // zap.AddCaller() will add line number and file name
ZapSugar = ZapLogger.Sugar()
}

func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}

func getWriterSyncer(level string) zapcore.WriteSyncer {
lumberWriteSyncer := &lumberjack.Logger{
Filename: fmt.Sprintf("logs/%s.log", level),
MaxSize: 10, // megabytes
Compress: true,
}
// file, _ := os.Create("logs/app.log")
return zapcore.Lock(zapcore.AddSync(lumberWriteSyncer))

}

0 comments on commit 1c2401e

Please sign in to comment.