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

support set log output path #508

Merged
merged 2 commits into from
Oct 19, 2020
Merged
Changes from all commits
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
21 changes: 21 additions & 0 deletions rlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Logger interface {
Error(msg string, fields map[string]interface{})
Fatal(msg string, fields map[string]interface{})
Level(level string)
OutputPath(path string) (err error)
}

func init() {
Expand Down Expand Up @@ -102,6 +103,7 @@ func (l *defaultLogger) Fatal(msg string, fields map[string]interface{}) {
}
l.logger.WithFields(fields).Fatal(msg)
}

func (l *defaultLogger) Level(level string) {
switch strings.ToLower(level) {
case "debug":
Expand All @@ -115,6 +117,17 @@ func (l *defaultLogger) Level(level string) {
}
}

func (l *defaultLogger) OutputPath(path string) (err error) {
var file *os.File
file, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return
UnderTreeTech marked this conversation as resolved.
Show resolved Hide resolved
}

l.logger.Out = file
return
}

// SetLogger use specified logger user customized, in general, we suggest user to replace the default logger with specified
func SetLogger(logger Logger) {
rLog = logger
Expand All @@ -126,6 +139,14 @@ func SetLogLevel(level string) {
rLog.Level(level)
}

func SetOutputPath(path string) (err error) {
if "" == path {
return
}

return rLog.OutputPath(path)
}

func Debug(msg string, fields map[string]interface{}) {
rLog.Debug(msg, fields)
}
Expand Down