Skip to content

Commit

Permalink
support set log output path (#508)
Browse files Browse the repository at this point in the history
* support set log output path

* err return definition.

Co-authored-by: John Sun <[email protected]>
  • Loading branch information
UnderTreeTech and John Sun authored Oct 19, 2020
1 parent 9cf97e1 commit 2a65561
Showing 1 changed file with 21 additions and 0 deletions.
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
}

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

0 comments on commit 2a65561

Please sign in to comment.