Skip to content

Commit

Permalink
Add log output config option (#172)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Miceli <[email protected]>
  • Loading branch information
jahands and thomiceli authored Dec 27, 2023
1 parent 780cc42 commit 76ff41e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
3 changes: 3 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# Set the log level to one of the following: trace, debug, info, warn, error, fatal, panic. Default: warn
log-level: warn

# Set the log output to one or more of the following: `stdout`, `file`. Default: stdout,file
log-output: stdout,file

# Public URL for the Git HTTP/SSH connection.
# If not set, uses the URL from the request
external-url:
Expand Down
1 change: 1 addition & 0 deletions docs/configuration/cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
| YAML Config Key | Environment Variable | Default value | Description |
|-----------------------|--------------------------|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------|
| log-level | OG_LOG_LEVEL | `warn` | Set the log level to one of the following: `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `panic`. |
| log-output | OG_LOG_OUTPUT | `stdout,file` | Set the log output to one or more of the following: `stdout`, `file`. |
| external-url | OG_EXTERNAL_URL | none | Public URL for the Git HTTP/SSH connection. If not set, uses the URL from the request. |
| opengist-home | OG_OPENGIST_HOME | home directory | Path to the directory where Opengist stores its data. |
| db-filename | OG_DB_FILENAME | `opengist.db` | Name of the SQLite database file. |
Expand Down
40 changes: 34 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"io"
"net/url"
"os"
"path/filepath"
Expand All @@ -23,6 +24,7 @@ var C *config
// doesn't support dot notation in this case sadly
type config struct {
LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"`
LogOutput string `yaml:"log-output" env:"OG_LOG_OUTPUT"`
ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"`
OpengistHome string `yaml:"opengist-home" env:"OG_OPENGIST_HOME"`
DBFilename string `yaml:"db-filename" env:"OG_DB_FILENAME"`
Expand Down Expand Up @@ -59,6 +61,7 @@ func configWithDefaults() (*config, error) {
c := &config{}

c.LogLevel = "warn"
c.LogOutput = "stdout,file"
c.OpengistHome = ""
c.DBFilename = "opengist.db"

Expand Down Expand Up @@ -115,18 +118,43 @@ func InitLog() {
if err := os.MkdirAll(filepath.Join(GetHomeDir(), "log"), 0755); err != nil {
panic(err)
}
file, err := os.OpenFile(filepath.Join(GetHomeDir(), "log", "opengist.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}

var level zerolog.Level
level, err = zerolog.ParseLevel(C.LogLevel)
level, err := zerolog.ParseLevel(C.LogLevel)
if err != nil {
level = zerolog.InfoLevel
}

multi := zerolog.MultiLevelWriter(zerolog.NewConsoleWriter(), file)
var logWriters []io.Writer
logOutputTypes := utils.RemoveDuplicates[string](
strings.Split(strings.ToLower(C.LogOutput), ","),
)
for _, logOutputType := range logOutputTypes {
logOutputType = strings.TrimSpace(logOutputType)
if !utils.SliceContains([]string{"stdout", "file"}, logOutputType) {
defer func() { log.Warn().Msg("Invalid log output type: " + logOutputType) }()
continue
}

switch logOutputType {
case "stdout":
logWriters = append(logWriters, zerolog.NewConsoleWriter())
defer func() { log.Debug().Msg("Logging to stdout") }()
case "file":
file, err := os.OpenFile(filepath.Join(GetHomeDir(), "log", "opengist.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
logWriters = append(logWriters, file)
defer func() { log.Debug().Msg("Logging to file: " + file.Name()) }()
}
}
if len(logWriters) == 0 {
logWriters = append(logWriters, zerolog.NewConsoleWriter())
defer func() { log.Warn().Msg("No valid log outputs, defaulting to stdout") }()
}

multi := zerolog.MultiLevelWriter(logWriters...)
log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger()

if !utils.SliceContains([]string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, strings.ToLower(C.LogLevel)) {
Expand Down
12 changes: 12 additions & 0 deletions internal/utils/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ func SliceContains(slice []string, item string) bool {
}
return false
}

func RemoveDuplicates[T string | int](sliceList []T) []T {
allKeys := make(map[T]bool)
list := []T{}
for _, item := range sliceList {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}

0 comments on commit 76ff41e

Please sign in to comment.