feat: log time format (#269)

Carlos Alexandro Becker created

* 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>

Change summary

cmd/soft/root.go        |  3 +++
server/config/config.go | 16 ++++++++++------
server/config/file.go   |  5 ++---
3 files changed, 15 insertions(+), 9 deletions(-)

Detailed changes

cmd/soft/root.go 🔗

@@ -61,6 +61,9 @@ func main() {
 	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":

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 {

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