-
Notifications
You must be signed in to change notification settings - Fork 17
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
zap logger added #19
Open
sushmitaw
wants to merge
2
commits into
joshsoftware:master
Choose a base branch
from
sushmitaw:feature/upgrade-logger
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
zap logger added #19
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package logger | ||
|
||
type ContextKey string | ||
|
||
const requestIDKey ContextKey = "request-id" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var sugaredLogger *zap.SugaredLogger | ||
|
||
func SetupLogger(env string) (*zap.SugaredLogger, error) { | ||
logger, err := getLoggerbyEnv(env) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sugaredLogger = logger.Sugar() | ||
return sugaredLogger, nil | ||
} | ||
|
||
func getLoggerbyEnv(env string) (logger *zap.Logger, err error) { | ||
option := zap.AddCallerSkip(1) | ||
|
||
if env == "production" { | ||
return zap.NewProduction(option) | ||
} | ||
|
||
config := zap.NewDevelopmentConfig() | ||
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
return config.Build(option) | ||
} | ||
|
||
func Errorw(ctx context.Context, message string, args ...interface{}) { | ||
args = appendRequestIDIntoArgs(ctx, args) | ||
sugaredLogger.Errorw(message, args...) | ||
} | ||
|
||
func Errorf(ctx context.Context, message string, args ...interface{}) { | ||
sugaredLogger.Errorf(message, args...) | ||
} | ||
|
||
func Error(ctx context.Context, args ...interface{}) { | ||
sugaredLogger.Error(args...) | ||
} | ||
|
||
func Infow(ctx context.Context, message string, args ...interface{}) { | ||
args = appendRequestIDIntoArgs(ctx, args) | ||
sugaredLogger.Infow(message, args...) | ||
} | ||
|
||
func Infof(ctx context.Context, message string, args ...interface{}) { | ||
sugaredLogger.Infof(message, args...) | ||
} | ||
|
||
func Info(ctx context.Context, args ...interface{}) { | ||
sugaredLogger.Info(args...) | ||
} | ||
|
||
func Warnw(ctx context.Context, message string, args ...interface{}) { | ||
args = appendRequestIDIntoArgs(ctx, args) | ||
sugaredLogger.Warnw(message, args...) | ||
} | ||
|
||
func Warnf(ctx context.Context, message string, args ...interface{}) { | ||
sugaredLogger.Warnf(message, args...) | ||
} | ||
|
||
func Warn(ctx context.Context, args ...interface{}) { | ||
sugaredLogger.Warn(args...) | ||
} | ||
|
||
func Debugw(ctx context.Context, message string, args ...interface{}) { | ||
args = appendRequestIDIntoArgs(ctx, args) | ||
sugaredLogger.Debugw(message, args...) | ||
} | ||
|
||
func Debugf(ctx context.Context, message string, args ...interface{}) { | ||
sugaredLogger.Debugf(message, args...) | ||
} | ||
|
||
func Debug(ctx context.Context, args ...interface{}) { | ||
sugaredLogger.Debug(args...) | ||
} | ||
|
||
func appendRequestIDIntoArgs(ctx context.Context, args []interface{}) []interface{} { | ||
ridValue, ok := ctx.Value(requestIDKey).(string) | ||
if !ok { | ||
return args | ||
} | ||
args = append(args, "request-id") | ||
args = append(args, ridValue) | ||
return args | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please move
production
key to const and how about if no env is defined we set the logger in production mode