57df7627af02b5bbf688e26ad8c7001d086ce64e

Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat: log time format (#269)

* fix: log with datetime

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: make it customizable

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Diff

 1diff --git a/cmd/soft/root.go b/cmd/soft/root.go
 2index b47e9cdd04bf8393c4b537e1599d0ed2b47e3dcc..edd7c21492974d1923c7cf8a6967237fa24bc09b 100644
 3--- a/cmd/soft/root.go
 4+++ b/cmd/soft/root.go
 5@@ -61,6 +61,9 @@ func main() {
 6 	if debug, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_DEBUG")); debug {
 7 		logger.SetLevel(log.DebugLevel)
 8 	}
 9+	if tsfmt := os.Getenv("SOFT_SERVE_LOG_TIME_FORMAT"); tsfmt != "" {
10+		logger.SetTimeFormat(tsfmt)
11+	}
12 
13 	switch strings.ToLower(os.Getenv("SOFT_SERVE_LOG_FORMAT")) {
14 	case "json":
15diff --git a/server/config/config.go b/server/config/config.go
16index 2c68bda24328db19d129749abe0166b954ab469d..f75a7a6d20a9ca062f5567876f84effae3745561 100644
17--- a/server/config/config.go
18+++ b/server/config/config.go
19@@ -7,6 +7,7 @@ import (
20 	"os"
21 	"path/filepath"
22 	"strings"
23+	"time"
24 
25 	"github.com/caarlos0/env/v7"
26 	"github.com/charmbracelet/log"
27@@ -93,6 +94,10 @@ type Config struct {
28 	// Valid values are "json", "logfmt", and "text".
29 	LogFormat string `env:"LOG_FORMAT" yaml:"log_format"`
30 
31+	// Time format for the log `ts` field.
32+	// Format must be described in Golang's time format.
33+	LogTimeFormat string `env:"LOG_TIME_FORMAT" yaml:"log_time_format"`
34+
35 	// InitialAdminKeys is a list of public keys that will be added to the list of admins.
36 	InitialAdminKeys []string `env:"INITIAL_ADMIN_KEYS" envSeparator:"\n" yaml:"initial_admin_keys"`
37 
38@@ -106,9 +111,10 @@ type Config struct {
39 func parseConfig(path string) (*Config, error) {
40 	dataPath := filepath.Dir(path)
41 	cfg := &Config{
42-		Name:      "Soft Serve",
43-		LogFormat: "text",
44-		DataPath:  dataPath,
45+		Name:          "Soft Serve",
46+		LogFormat:     "text",
47+		LogTimeFormat: time.DateOnly,
48+		DataPath:      dataPath,
49 		SSH: SSHConfig{
50 			ListenAddr:    ":23231",
51 			PublicURL:     "ssh://localhost:23231",
52@@ -280,9 +286,7 @@ func (c *Config) AdminKeys() []ssh.PublicKey {
53 	return parseAuthKeys(c.InitialAdminKeys)
54 }
55 
56-var (
57-	configCtxKey = struct{ string }{"config"}
58-)
59+var configCtxKey = struct{ string }{"config"}
60 
61 // WithContext returns a new context with the configuration attached.
62 func WithContext(ctx context.Context, cfg *Config) context.Context {
63diff --git a/server/config/file.go b/server/config/file.go
64index b3f8c8ec91c32e0c0527d24eeaf6c4aa98e3fa34..36c0a7a311bd96d924c33eeeea33a701d37e8d0d 100644
65--- a/server/config/file.go
66+++ b/server/config/file.go
67@@ -5,8 +5,7 @@ import (
68 	"text/template"
69 )
70 
71-var (
72-	configFileTmpl = template.Must(template.New("config").Parse(`# Soft Serve Server configurations
73+var configFileTmpl = template.Must(template.New("config").Parse(`# Soft Serve Server configurations
74 
75 # The name of the server.
76 # This is the name that will be displayed in the UI.
77@@ -14,6 +13,7 @@ name: "{{ .Name }}"
78 
79 # Log format to use. Valid values are "json", "logfmt", and "text".
80 log_format: "{{ .LogFormat }}"
81+log_time_format: "{{ .LogTimeFormat }}"
82 
83 # The SSH server configuration.
84 ssh:
85@@ -79,7 +79,6 @@ stats:
86 #initial_admin_keys:
87 #  - "ssh-rsa AAAAB3NzaC1yc2..."
88 `))
89-)
90 
91 func newConfigFile(cfg *Config) string {
92 	var b bytes.Buffer