From e0055a0840f6fa736fb3cd8b7f839228af54f884 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 9 May 2023 14:16:56 +0000 Subject: [PATCH 1/2] fix: log with datetime Signed-off-by: Carlos Alexandro Becker --- cmd/soft/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/soft/root.go b/cmd/soft/root.go index b47e9cdd0..286060795 100644 --- a/cmd/soft/root.go +++ b/cmd/soft/root.go @@ -56,7 +56,7 @@ func main() { ctx := context.Background() logger := log.NewWithOptions(os.Stderr, log.Options{ ReportTimestamp: true, - TimeFormat: time.DateOnly, + TimeFormat: time.DateTime, }) if debug, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_DEBUG")); debug { logger.SetLevel(log.DebugLevel) From ee66667adb5667e1a5001bd24ba08282c771ae3e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 9 May 2023 14:52:08 +0000 Subject: [PATCH 2/2] fix: make it customizable Signed-off-by: Carlos Alexandro Becker --- cmd/soft/root.go | 5 ++++- server/config/config.go | 16 ++++++++++------ server/config/file.go | 5 ++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/soft/root.go b/cmd/soft/root.go index 286060795..edd7c2149 100644 --- a/cmd/soft/root.go +++ b/cmd/soft/root.go @@ -56,11 +56,14 @@ func main() { ctx := context.Background() logger := log.NewWithOptions(os.Stderr, log.Options{ ReportTimestamp: true, - TimeFormat: time.DateTime, + TimeFormat: time.DateOnly, }) if debug, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_DEBUG")); debug { logger.SetLevel(log.DebugLevel) } + if tsfmt := os.Getenv("SOFT_SERVE_LOG_TIME_FORMAT"); tsfmt != "" { + logger.SetTimeFormat(tsfmt) + } switch strings.ToLower(os.Getenv("SOFT_SERVE_LOG_FORMAT")) { case "json": diff --git a/server/config/config.go b/server/config/config.go index 2c68bda24..f75a7a6d2 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/caarlos0/env/v7" "github.com/charmbracelet/log" @@ -93,6 +94,10 @@ type Config struct { // Valid values are "json", "logfmt", and "text". LogFormat string `env:"LOG_FORMAT" yaml:"log_format"` + // Time format for the log `ts` field. + // Format must be described in Golang's time format. + LogTimeFormat string `env:"LOG_TIME_FORMAT" yaml:"log_time_format"` + // InitialAdminKeys is a list of public keys that will be added to the list of admins. InitialAdminKeys []string `env:"INITIAL_ADMIN_KEYS" envSeparator:"\n" yaml:"initial_admin_keys"` @@ -106,9 +111,10 @@ type Config struct { func parseConfig(path string) (*Config, error) { dataPath := filepath.Dir(path) cfg := &Config{ - Name: "Soft Serve", - LogFormat: "text", - DataPath: dataPath, + Name: "Soft Serve", + LogFormat: "text", + LogTimeFormat: time.DateOnly, + DataPath: dataPath, SSH: SSHConfig{ ListenAddr: ":23231", PublicURL: "ssh://localhost:23231", @@ -280,9 +286,7 @@ func (c *Config) AdminKeys() []ssh.PublicKey { return parseAuthKeys(c.InitialAdminKeys) } -var ( - configCtxKey = struct{ string }{"config"} -) +var configCtxKey = struct{ string }{"config"} // WithContext returns a new context with the configuration attached. func WithContext(ctx context.Context, cfg *Config) context.Context { diff --git a/server/config/file.go b/server/config/file.go index b3f8c8ec9..36c0a7a31 100644 --- a/server/config/file.go +++ b/server/config/file.go @@ -5,8 +5,7 @@ import ( "text/template" ) -var ( - configFileTmpl = template.Must(template.New("config").Parse(`# Soft Serve Server configurations +var configFileTmpl = template.Must(template.New("config").Parse(`# Soft Serve Server configurations # The name of the server. # This is the name that will be displayed in the UI. @@ -14,6 +13,7 @@ name: "{{ .Name }}" # Log format to use. Valid values are "json", "logfmt", and "text". log_format: "{{ .LogFormat }}" +log_time_format: "{{ .LogTimeFormat }}" # The SSH server configuration. ssh: @@ -79,7 +79,6 @@ stats: #initial_admin_keys: # - "ssh-rsa AAAAB3NzaC1yc2..." `)) -) func newConfigFile(cfg *Config) string { var b bytes.Buffer