Skip to content

Commit 1ca8931

Browse files
authored
feat : set the time zone for the service (#682)
* set the time zone for log output * feat: change to global time zone setting * feat: change to global time zone setting
1 parent d1d912e commit 1ca8931

5 files changed

Lines changed: 15 additions & 4 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
SERVER_HOST=0.0.0.0
22
SERVER_PORT=5002
33
SERVER_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi
4+
# default server tz UTC
5+
SERVER_TZ=
46
GIN_MODE=release
57
PLATFORM=local
68

cmd/server/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/kelseyhightower/envconfig"
78
"github.com/langgenius/dify-plugin-daemon/internal/server"
@@ -19,6 +20,12 @@ func main() {
1920

2021
config.SetDefault()
2122

23+
loc, err := time.LoadLocation(config.ServerTimeZone)
24+
if err != nil {
25+
log.Panic("load location error", "error", err)
26+
}
27+
time.Local = loc
28+
2229
logCloser, err := log.Init(config.LogOutputFormat == "json", config.LogFile)
2330
if err != nil {
2431
log.Panic("failed to init logger", "error", err)

internal/types/app/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const (
2424

2525
type Config struct {
2626
// server
27-
ServerHost string `envconfig:"SERVER_HOST"`
28-
ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
29-
ServerKey string `envconfig:"SERVER_KEY" validate:"required"`
27+
ServerHost string `envconfig:"SERVER_HOST"`
28+
ServerPort uint16 `envconfig:"SERVER_PORT" validate:"required"`
29+
ServerKey string `envconfig:"SERVER_KEY" validate:"required"`
30+
ServerTimeZone string `envconfig:"SERVER_TZ" default:"UTC" validate:"timezone"`
3031

3132
// admin api enable
3233
AdminApiEnabled bool `envconfig:"ADMIN_API_ENABLED" default:"false"`

internal/types/app/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func (config *Config) SetDefault() {
1212
}
1313
setDefaultString(&config.ServerHost, "0.0.0.0")
1414
setDefaultInt(&config.ServerPort, 5002)
15+
setDefaultString(&config.ServerTimeZone, "UTC")
1516
setDefaultInt(&config.RoutinePoolSize, 10000)
1617
setDefaultInt(&config.LifetimeCollectionGCInterval, 60)
1718
setDefaultInt(&config.LifetimeCollectionHeartbeatInterval, 5)

pkg/utils/log/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (h *Handler) Enabled(_ context.Context, level slog.Level) bool {
4444
func (h *Handler) Handle(ctx context.Context, r slog.Record) error {
4545
fields := make(map[string]any)
4646

47-
fields["ts"] = r.Time.UTC().Format(time.RFC3339Nano)
47+
fields["ts"] = r.Time.Local().Format(time.RFC3339Nano)
4848
fields["severity"] = levelToSeverity(r.Level)
4949
fields["service"] = h.opts.Service
5050
fields["caller"] = getCaller(r.PC)

0 commit comments

Comments
 (0)