Invalid config check

This commit is contained in:
世界
2022-07-03 01:57:04 +08:00
parent 6eae8e361f
commit 30444057bd
16 changed files with 276 additions and 162 deletions

View File

@@ -7,6 +7,8 @@ import (
)
type Logger interface {
Start() error
Close() error
Trace(args ...interface{})
Debug(args ...interface{})
Info(args ...interface{})
@@ -18,7 +20,6 @@ type Logger interface {
Panic(args ...interface{})
WithContext(ctx context.Context) Logger
WithPrefix(prefix string) Logger
Close() error
}
func NewLogger(options option.LogOption) (Logger, error) {

View File

@@ -15,7 +15,8 @@ var _ Logger = (*logrusLogger)(nil)
type logrusLogger struct {
abstractLogrusLogger
output *os.File
outputPath string
output *os.File
}
type abstractLogrusLogger interface {
@@ -28,7 +29,6 @@ func NewLogrusLogger(options option.LogOption) (*logrusLogger, error) {
logger.SetLevel(logrus.TraceLevel)
logger.Formatter.(*logrus.TextFormatter).ForceColors = true
logger.AddHook(new(logrusHook))
var output *os.File
var err error
if options.Level != "" {
logger.Level, err = logrus.ParseLevel(options.Level)
@@ -36,18 +36,26 @@ func NewLogrusLogger(options option.LogOption) (*logrusLogger, error) {
return nil, err
}
}
if options.Output != "" {
output, err = os.OpenFile(options.Output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
return &logrusLogger{logger, options.Output, nil}, nil
}
func (l *logrusLogger) Start() error {
if l.outputPath != "" {
output, err := os.OpenFile(l.outputPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, E.Extend(err, "open log output")
return E.Cause(err, "open log output")
}
logger.SetOutput(output)
l.abstractLogrusLogger.(*logrus.Logger).SetOutput(output)
}
return &logrusLogger{logger, output}, nil
return nil
}
func (l *logrusLogger) Close() error {
return common.Close(common.PtrOrNil(l.output))
}
func (l *logrusLogger) WithContext(ctx context.Context) Logger {
return &logrusLogger{l.abstractLogrusLogger.WithContext(ctx), nil}
return &logrusLogger{abstractLogrusLogger: l.abstractLogrusLogger.WithContext(ctx)}
}
func (l *logrusLogger) WithPrefix(prefix string) Logger {
@@ -57,9 +65,5 @@ func (l *logrusLogger) WithPrefix(prefix string) Logger {
prefix = F.ToString(loadedPrefix, prefix)
}
}
return &logrusLogger{l.WithField("prefix", prefix), nil}
}
func (l *logrusLogger) Close() error {
return common.Close(common.PtrOrNil(l.output))
return &logrusLogger{abstractLogrusLogger: l.WithField("prefix", prefix)}
}

View File

@@ -10,6 +10,14 @@ func NewNopLogger() Logger {
return (*nopLogger)(nil)
}
func (l *nopLogger) Start() error {
return nil
}
func (l *nopLogger) Close() error {
return nil
}
func (l *nopLogger) Trace(args ...interface{}) {
}
@@ -44,7 +52,3 @@ func (l *nopLogger) WithContext(ctx context.Context) Logger {
func (l *nopLogger) WithPrefix(prefix string) Logger {
return l
}
func (l *nopLogger) Close() error {
return nil
}