Skip to content
Snippets Groups Projects
Commit d45d8b1f authored by Travis Ralston's avatar Travis Ralston
Browse files

Add structured logging support

parent e9f82230
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ...@@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased] ## [Unreleased]
### Added
* Added support for structured logging (JSON).
### Changed ### Changed
* Turned color-coded logs off by default. This can be changed in the config. * Turned color-coded logs off by default. This can be changed in the config.
......
...@@ -58,7 +58,7 @@ func main() { ...@@ -58,7 +58,7 @@ func main() {
realPsqlPassword = *postgresPassword realPsqlPassword = *postgresPassword
} }
err := logging.Setup(config.Get().General.LogDirectory) err := logging.Setup(config.Get().General.LogDirectory, config.Get().General.LogColors, config.Get().General.JsonLogs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -45,7 +45,7 @@ func main() { ...@@ -45,7 +45,7 @@ func main() {
assets.SetupTemplates(*templatesPath) assets.SetupTemplates(*templatesPath)
var err error var err error
err = logging.Setup(config.Get().General.LogDirectory) err = logging.Setup(config.Get().General.LogDirectory, config.Get().General.LogColors, config.Get().General.JsonLogs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -35,7 +35,7 @@ func main() { ...@@ -35,7 +35,7 @@ func main() {
assets.SetupMigrations(*migrationsPath) assets.SetupMigrations(*migrationsPath)
var err error var err error
err = logging.Setup(config.Get().General.LogDirectory) err = logging.Setup(config.Get().General.LogDirectory, config.Get().General.LogColors, config.Get().General.JsonLogs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -72,7 +72,7 @@ func main() { ...@@ -72,7 +72,7 @@ func main() {
realPsqlPassword = *postgresPassword realPsqlPassword = *postgresPassword
} }
err := logging.Setup(config.Get().General.LogDirectory) err := logging.Setup(config.Get().General.LogDirectory, config.Get().General.LogColors, config.Get().General.JsonLogs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -59,7 +59,7 @@ func main() { ...@@ -59,7 +59,7 @@ func main() {
assets.SetupTemplates(*templatesPath) assets.SetupTemplates(*templatesPath)
assets.SetupAssets(*assetsPath) assets.SetupAssets(*assetsPath)
err := logging.Setup(config.Get().General.LogDirectory, config.Get().General.LogColors) err := logging.Setup(config.Get().General.LogDirectory, config.Get().General.LogColors, config.Get().General.JsonLogs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
......
...@@ -25,6 +25,7 @@ func NewDefaultMainConfig() MainRepoConfig { ...@@ -25,6 +25,7 @@ func NewDefaultMainConfig() MainRepoConfig {
Port: 8000, Port: 8000,
LogDirectory: "logs", LogDirectory: "logs",
LogColors: false, LogColors: false,
JsonLogs: false,
TrustAnyForward: false, TrustAnyForward: false,
UseForwardedHost: true, UseForwardedHost: true,
}, },
......
...@@ -5,6 +5,7 @@ type GeneralConfig struct { ...@@ -5,6 +5,7 @@ type GeneralConfig struct {
Port int `yaml:"port"` Port int `yaml:"port"`
LogDirectory string `yaml:"logDirectory"` LogDirectory string `yaml:"logDirectory"`
LogColors bool `yaml:"logColors"` LogColors bool `yaml:"logColors"`
JsonLogs bool `yaml:"jsonLogs"`
TrustAnyForward bool `yaml:"trustAnyForwardedAddress"` TrustAnyForward bool `yaml:"trustAnyForwardedAddress"`
UseForwardedHost bool `yaml:"useForwardedHost"` UseForwardedHost bool `yaml:"useForwardedHost"`
} }
......
...@@ -19,17 +19,24 @@ func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) { ...@@ -19,17 +19,24 @@ func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) {
return f.Formatter.Format(entry) return f.Formatter.Format(entry)
} }
func Setup(dir string, colors bool) error { func Setup(dir string, colors bool, json bool) error {
formatter := &utcFormatter{ var lineFormatter logrus.Formatter
&logrus.TextFormatter{ if json {
lineFormatter = &logrus.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05.000 Z07:00",
DisableTimestamp: false,
}
} else {
lineFormatter = &logrus.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05.000 Z07:00", TimestampFormat: "2006-01-02 15:04:05.000 Z07:00",
FullTimestamp: true, FullTimestamp: true,
ForceColors: colors, ForceColors: colors,
DisableColors: !colors, DisableColors: !colors,
DisableTimestamp: false, DisableTimestamp: false,
QuoteEmptyFields: true, QuoteEmptyFields: true,
}, }
} }
formatter := &utcFormatter{lineFormatter}
logrus.SetFormatter(formatter) logrus.SetFormatter(formatter)
logrus.SetOutput(os.Stdout) logrus.SetOutput(os.Stdout)
......
...@@ -15,6 +15,10 @@ repo: ...@@ -15,6 +15,10 @@ repo:
# appear in logs which render them unreadable, which is why colors are disabled by default. # appear in logs which render them unreadable, which is why colors are disabled by default.
logColors: false logColors: false
# Set to true to enable JSON logging for consumption by things like logstash. Note that this is
# incompatible with the log color option and will always render without colors.
jsonLogs: false
# If true, the media repo will accept any X-Forwarded-For header without validation. In most cases # If true, the media repo will accept any X-Forwarded-For header without validation. In most cases
# this option should be left as "false". Note that the media repo already expects an X-Forwarded-For # this option should be left as "false". Note that the media repo already expects an X-Forwarded-For
# header, but validates it to ensure the IP being given makes sense. # header, but validates it to ensure the IP being given makes sense.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment