Skip to content

Commit

Permalink
feat: log time format (#269)
Browse files Browse the repository at this point in the history
* fix: log with datetime

Signed-off-by: Carlos Alexandro Becker <[email protected]>

* fix: make it customizable

Signed-off-by: Carlos Alexandro Becker <[email protected]>

---------

Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 authored May 9, 2023
1 parent 8855f56 commit 57df762
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cmd/soft/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
16 changes: 10 additions & 6 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/caarlos0/env/v7"
"github.com/charmbracelet/log"
Expand Down Expand Up @@ -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"`

Expand All @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions server/config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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.
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:
Expand Down Expand Up @@ -79,7 +79,6 @@ stats:
#initial_admin_keys:
# - "ssh-rsa AAAAB3NzaC1yc2..."
`))
)

func newConfigFile(cfg *Config) string {
var b bytes.Buffer
Expand Down

0 comments on commit 57df762

Please sign in to comment.