Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log time format #269

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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