Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging #60

Merged
merged 9 commits into from
Oct 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: logger & global var logger.Logger
HilkopterBob committed Oct 8, 2024

Verified

This commit was signed with the committer’s verified signature.
purajit purajit
commit cd13f0a35e7b606cc1a6e36b2408e8062a51139f
42 changes: 42 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package logger

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var Logger *zap.SugaredLogger

// InitLogger initializes the zap.Logger once and returns the instance.
func InitLogger() (*zap.SugaredLogger, error) {
var err error

// Ensure the logger is initialized only once
loggerConfig := zap.Config{
Encoding: "console", // You can also use "json"
OutputPaths: []string{"logs/app.log"},
ErrorOutputPaths: []string{"logs/app_error.log"},
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
// Build the logger
logger, err := loggerConfig.Build()
if err != nil {
// Handle logger initialization error (no panic)
logger = nil
}
// Return the initialized logger and any error that occurred
return logger.Sugar(), err
}