Skip to content

Commit

Permalink
refactor(logger): Moved initialization function
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Nov 26, 2023
1 parent 006fe3b commit 6ffbd14
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
38 changes: 2 additions & 36 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import (
"EverythingSuckz/fsb/utils"
"fmt"
"net/http"
"os"
"time"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

const versionString = "v0.0.1"

var startTime time.Time = time.Now()

func main() {
log := initLogger()
utils.InitLogger()
log := utils.Logger
log.Info("Starting server...")
config.Load(log)
router := getRouter(log)
Expand All @@ -41,38 +39,6 @@ func main() {

}

func initLogger() *zap.Logger {
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("02/01/2006 03:04 PM"))
}
consoleConfig := zap.NewDevelopmentEncoderConfig()
consoleConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
consoleConfig.EncodeTime = customTimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig)
defaultLogLevel := zapcore.DebugLevel

fileEncoderConfig := zap.NewProductionEncoderConfig()
fileEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
fileEncoder := zapcore.NewJSONEncoder(fileEncoderConfig)

fileWriter := zapcore.AddSync(&lumberjack.Logger{
Filename: "logs/app.log",
MaxSize: 10,
MaxBackups: 3,
MaxAge: 7,
Compress: true,
})

core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), defaultLogLevel),
zapcore.NewCore(fileEncoder, fileWriter, defaultLogLevel),
)

logger := zap.New(core, zap.AddStacktrace(zapcore.ErrorLevel))
return logger

}

func getRouter(log *zap.Logger) *gin.Engine {
if config.ValueOf.Dev {
gin.SetMode(gin.DebugMode)
Expand Down
42 changes: 42 additions & 0 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package utils

import (
"os"
"time"

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

var Logger *zap.Logger

func InitLogger() {
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("02/01/2006 03:04 PM"))
}
consoleConfig := zap.NewDevelopmentEncoderConfig()
consoleConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
consoleConfig.EncodeTime = customTimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig)
defaultLogLevel := zapcore.DebugLevel

fileEncoderConfig := zap.NewProductionEncoderConfig()
fileEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
fileEncoder := zapcore.NewJSONEncoder(fileEncoderConfig)

fileWriter := zapcore.AddSync(&lumberjack.Logger{
Filename: "logs/app.log",
MaxSize: 10,
MaxBackups: 3,
MaxAge: 7,
Compress: true,
})

core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), defaultLogLevel),
zapcore.NewCore(fileEncoder, fileWriter, defaultLogLevel),
)

Logger = zap.New(core, zap.AddStacktrace(zapcore.ErrorLevel))
}

0 comments on commit 6ffbd14

Please sign in to comment.