-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace zap logger with logrus (#116)
- Loading branch information
Showing
9 changed files
with
158 additions
and
159 deletions.
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
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,115 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
prefixed "github.com/x-cray/logrus-prefixed-formatter" | ||
) | ||
|
||
type LogrusConfig struct { | ||
EnableConsole bool | ||
EnableFile bool | ||
Structured bool | ||
Level logrus.Level | ||
FileLocation string | ||
} | ||
|
||
type LogrusLogger struct { | ||
Config LogrusConfig | ||
Logger *logrus.Logger | ||
} | ||
|
||
type LogrusNestedLogger struct { | ||
Logger *logrus.Entry | ||
} | ||
|
||
func NewLogrusLogger(cfg LogrusConfig) *LogrusLogger { | ||
appLogger := logrus.New() | ||
|
||
var output io.Writer | ||
switch { | ||
case cfg.EnableConsole && cfg.EnableFile: | ||
logFile, err := os.OpenFile(cfg.FileLocation, os.O_WRONLY|os.O_CREATE, 0755) | ||
if err != nil { | ||
panic(fmt.Errorf("unable to setup log file: %w", err)) | ||
} | ||
output = io.MultiWriter(os.Stderr, logFile) | ||
case cfg.EnableConsole: | ||
output = os.Stderr | ||
case cfg.EnableFile: | ||
logFile, err := os.OpenFile(cfg.FileLocation, os.O_WRONLY|os.O_CREATE, 0755) | ||
if err != nil { | ||
panic(fmt.Errorf("unable to setup log file: %w", err)) | ||
} | ||
output = logFile | ||
default: | ||
output = ioutil.Discard | ||
} | ||
|
||
appLogger.SetOutput(output) | ||
appLogger.SetLevel(cfg.Level) | ||
|
||
if cfg.Structured { | ||
appLogger.SetFormatter(&logrus.JSONFormatter{ | ||
TimestampFormat: "2006-01-02 15:04:05", | ||
DisableTimestamp: false, | ||
DisableHTMLEscape: false, | ||
PrettyPrint: false, | ||
}) | ||
} else { | ||
appLogger.SetFormatter(&prefixed.TextFormatter{ | ||
TimestampFormat: "2006-01-02 15:04:05", | ||
ForceColors: true, | ||
ForceFormatting: true, | ||
}) | ||
} | ||
|
||
return &LogrusLogger{ | ||
Config: cfg, | ||
Logger: appLogger, | ||
} | ||
} | ||
|
||
func (l *LogrusLogger) Debugf(format string, args ...interface{}) { | ||
l.Logger.Debugf(format, args...) | ||
} | ||
|
||
func (l *LogrusLogger) Infof(format string, args ...interface{}) { | ||
l.Logger.Infof(format, args...) | ||
} | ||
|
||
func (l *LogrusLogger) Debug(args ...interface{}) { | ||
l.Logger.Debug(args...) | ||
} | ||
|
||
func (l *LogrusLogger) Info(args ...interface{}) { | ||
l.Logger.Info(args...) | ||
} | ||
|
||
func (l *LogrusLogger) Errorf(format string, args ...interface{}) { | ||
l.Logger.Errorf(format, args...) | ||
} | ||
|
||
func (l *LogrusNestedLogger) Debugf(format string, args ...interface{}) { | ||
l.Logger.Debugf(format, args...) | ||
} | ||
|
||
func (l *LogrusNestedLogger) Infof(format string, args ...interface{}) { | ||
l.Logger.Infof(format, args...) | ||
} | ||
|
||
func (l *LogrusNestedLogger) Debug(args ...interface{}) { | ||
l.Logger.Debug(args...) | ||
} | ||
|
||
func (l *LogrusNestedLogger) Info(args ...interface{}) { | ||
l.Logger.Info(args...) | ||
} | ||
|
||
func (l *LogrusNestedLogger) Errorf(format string, args ...interface{}) { | ||
l.Logger.Errorf(format, args...) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.