Skip to content

Commit b3e8bf7

Browse files
Add LOGGER_TIMEZONE env to control the time zone
Closes #23 If the name is "" or "UTC", LoadLocation returns UTC. If the name is "Local", LoadLocation returns Local. Otherwise, the name is taken to be a location name corresponding to a file in the IANA Time Zone database, such as "America/New_York". LoadLocation looks for the IANA Time Zone database in the following locations in order: the directory or uncompressed zip file named by the ZONEINFO environment variable on a Unix system, the system standard installation location $GOROOT/lib/time/zoneinfo.zip the time/tzdata package, if it was imported
1 parent 9d80161 commit b3e8bf7

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

logger/logger.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,23 @@ func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
5353
if level < l.level {
5454
return
5555
}
56-
timestamp := time.Now().Format("2006/01/02 15:04:05")
56+
57+
// Get timezone from environment variable or use local timezone
58+
timezone := os.Getenv("LOGGER_TIMEZONE")
59+
var location *time.Location
60+
var err error
61+
62+
if timezone != "" {
63+
location, err = time.LoadLocation(timezone)
64+
if err != nil {
65+
// If invalid timezone, fall back to local
66+
location = time.Local
67+
}
68+
} else {
69+
location = time.Local
70+
}
71+
72+
timestamp := time.Now().In(location).Format("2006/01/02 15:04:05")
5773
message := fmt.Sprintf(format, args...)
5874
l.logger.Printf("%s: %s %s", level.String(), timestamp, message)
5975
}

0 commit comments

Comments
 (0)