Skip to content

Commit

Permalink
add tests and docs for time location
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Apr 22, 2024
1 parent b7a1745 commit e21bf3b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var DefaultLogger = Logger{
Writer: &IOWriter{os.Stderr},
}

// A Logger represents an active logging object that generates lines of JSON output to an io.Writer.
// Logger represents an active logging object that generates lines of JSON output to an io.Writer.
type Logger struct {
// Level defines log levels.
Level Level
Expand All @@ -59,13 +59,16 @@ type Logger struct {
// If Caller is negative, adds the full /path/to/file:line of the "caller" key.
Caller int

// TimeField defines the time filed name in output. It uses "time" in if empty.
// TimeField defines the time field name in output. It uses "time" in if empty.
TimeField string

// TimeFormat specifies the time format in output. It uses RFC3339 with millisecond if empty.
// If set with `TimeFormatUnix/TimeFormatUnixMs/TimeFormatUnixWithMs`, timestamps are formated.
// TimeFormat specifies the time format in output. Uses RFC3339 with millisecond if empty.
// If set to `TimeFormatUnix/TimeFormatUnixMs`, timestamps will be formatted.
TimeFormat string

// TimeLocation specifices that the location of TimeFormat used. Uses time.Local if empty.
TimeLocation *time.Location

// Writer specifies the writer of output. It uses a wrapped os.Stderr Writer in if empty.
Writer Writer
}
Expand Down
23 changes: 23 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,29 @@ func TestLoggerTimeFormat(t *testing.T) {
logger.Info().Int64("timestamp_ms", timeNow().UnixNano()/1000000).Msg("this is rfc3339 time log entry")
}

func TestLoggerTimeLocation(t *testing.T) {
logger := Logger{}

for _, format := range []string{"", time.RFC822} {
logger.TimeFormat = format

logger.TimeLocation = nil
logger.Info().Msgf("this is TimeFormat=%#v TimeLocation=nil log entry", logger.TimeFormat)

logger.TimeLocation = time.Local
logger.Info().Msgf("this is TimeFormat=%#v TimeLocation=time.Local log entry", logger.TimeFormat)

logger.TimeLocation = time.UTC
logger.Info().Msgf("this is TimeFormat=%#v TimeLocation=time.UTC log entry", logger.TimeFormat)

logger.TimeLocation, _ = time.LoadLocation("Asia/Singapore")
logger.Info().Msgf("this is TimeFormat=%#v TimeLocation=Asia/Singapore log entry", logger.TimeFormat)

logger.TimeLocation, _ = time.LoadLocation("America/New_York")
logger.Info().Msgf("this is TimeFormat=%#v TimeLocation=America/New_York log entry", logger.TimeFormat)
}
}

func TestLoggerTimeOffset(t *testing.T) {
logger := Logger{}

Expand Down

0 comments on commit e21bf3b

Please sign in to comment.