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 9ed2bd7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// If set to `TimeFormatUnix/TimeFormatUnixMs/TimeFormatUnixWithMs`, timestamps are 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 9ed2bd7

Please sign in to comment.