Skip to content

Commit

Permalink
fix: export FilenameEncoder and TimeEncoder (#3)
Browse files Browse the repository at this point in the history
* export FilenameEncoder and TimeEncoder

* fix unit test error

* fix unit test error

* remove unmarshal unit test of log
  • Loading branch information
shipengqi authored Jan 25, 2022
1 parent b54758b commit f4bf25a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 32 deletions.
2 changes: 1 addition & 1 deletion log/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
}

func rollingFileEncoder(opts *Options) (zapcore.WriteSyncer, io.Closer) {
encoded := opts.filenameEncoder()
encoded := opts.FilenameEncoder()
f := filepath.Join(opts.Output, encoded)
EncodedFilename = f
if opts.DisableRotate {
Expand Down
5 changes: 3 additions & 2 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ func TestDefaultLoggerWithoutTime(t *testing.T) {

func TestLoggerFile(t *testing.T) {
tmp := os.TempDir()
opts := NewOptions().WithFilenameEncoder(func() string {
opts := NewOptions()
opts.FilenameEncoder = func() string {
return "test.log"
})
}
opts.DisableConsole = true
opts.DisableFile = false
opts.Output = tmp
Expand Down
10 changes: 5 additions & 5 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func New(opts *Options) *Logger {
EncodeDuration: zapcore.MillisDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
if opts.timeEncoder != nil {
encoderConfig.EncodeTime = opts.timeEncoder
if opts.TimeEncoder != nil {
encoderConfig.EncodeTime = opts.TimeEncoder
}
if !opts.DisableConsole {
var consoleLevel Level
Expand Down Expand Up @@ -254,8 +254,8 @@ func (l *Logger) Flush() error {
}

func (l *Logger) Close() error {
if l.closer == nil {
return nil
if l.closer != nil {
return l.closer.Close()
}
return l.closer.Close()
return nil
}
22 changes: 5 additions & 17 deletions log/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ type Options struct {
// Output directory for logging when DisableFile is false
Output string `json:"output" mapstructure:"output"`
// FilenameEncoder log filename encoder
filenameEncoder FilenameEncoder
// TimeEncoder log filename encoder
timeEncoder TimeEncoder
FilenameEncoder FilenameEncoder
// TimeEncoder time encoder
TimeEncoder TimeEncoder
}

// NewOptions creates an Options with default parameters.
Expand All @@ -56,23 +56,11 @@ func NewOptions() *Options {
DisableConsoleCaller: true,
DisableFileCaller: true,
ConsoleLevel: InfoLevel.String(),
filenameEncoder: DefaultFilenameEncoder,
timeEncoder: DefaultTimeEncoder,
FilenameEncoder: DefaultFilenameEncoder,
TimeEncoder: DefaultTimeEncoder,
}
}

// WithFilenameEncoder use the given encoder instead DefaultFilenameEncoder.
func (o *Options) WithFilenameEncoder(encoder FilenameEncoder) *Options {
o.filenameEncoder = encoder
return o
}

// WithTimeEncoder use the given encoder instead DefaultTimeEncoder.
func (o *Options) WithTimeEncoder(encoder TimeEncoder) *Options {
o.timeEncoder = encoder
return o
}

// Validate validates the options fields.
func (o *Options) Validate() []error {
var errs []error
Expand Down
7 changes: 0 additions & 7 deletions log/options_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package log

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -39,9 +38,3 @@ func TestOptions_Validate(t *testing.T) {
expected = `[unrecognized level: "failed" unrecognized level: "failed" no log output]`
assert.Equal(t, expected, fmt.Sprintf("%s", errs))
}

func TestUnmarshal(t *testing.T) {
opts := NewOptions()
_, err := json.Marshal(opts)
assert.NoError(t, err)
}

0 comments on commit f4bf25a

Please sign in to comment.