Skip to content

Commit

Permalink
Format Logs and add timestamp to logging output option (#5898)
Browse files Browse the repository at this point in the history
  • Loading branch information
quinqu authored Mar 12, 2021
1 parent 0d1a53b commit 4d916e4
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/pages/config-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ teleport:

# Logging configuration. Possible output values to disk via '/var/lib/teleport/teleport.log',
# 'stdout', 'stderr' and 'syslog'. Possible severity values are INFO, WARN
# and ERROR (default).
# and ERROR (default). Possible format values include: timestamp, component, message, caller, and level.
log:
output: /var/lib/teleport/teleport.log
severity: ERROR
format: [level, timestamp, component, message]

# Configuration for the storage back-end used for the cluster state and the
# audit log. Several back-end types are supported. See the "High Availability"
Expand Down
10 changes: 10 additions & 0 deletions lib/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ func applyLogConfig(loggerConfig Log, logger *log.Logger) error {
default:
return trace.BadParameter("unsupported logger severity: %q", loggerConfig.Severity)
}

formatter := &textFormatter{
LogFormat: loggerConfig.Format,
EnableColors: trace.IsTerminal(os.Stderr),
}
err := formatter.CheckAndSetDefaults()
if err != nil {
return trace.Wrap(err)
}
logger.Formatter = formatter
return nil
}

Expand Down
30 changes: 30 additions & 0 deletions lib/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,3 +1363,33 @@ func TestDatabaseFlags(t *testing.T) {
})
}
}

func TestTextFormatter(t *testing.T) {
tests := []struct {
comment string
formatConfig []string
assertErr require.ErrorAssertionFunc
}{
{
comment: "invalid key (does not exist)",
formatConfig: []string{"level", "invalid key"},
assertErr: require.Error,
},
{
comment: "valid keys and formatting",
formatConfig: []string{"level", "component", "timestamp"},
assertErr: require.NoError,
},
}

for _, tt := range tests {
t.Run(tt.comment, func(t *testing.T) {
formatter := &textFormatter{
LogFormat: tt.formatConfig,
}
tt.assertErr(t, formatter.CheckAndSetDefaults())

})
}

}
2 changes: 2 additions & 0 deletions lib/config/fileconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ type Log struct {
Output string `yaml:"output,omitempty"`
// Severity defines how verbose the log will be. Possible valus are "error", "info", "warn"
Severity string `yaml:"severity,omitempty"`
// Format lists the output fields from KnownFormatFields. Example format: [timestamp, component, message]
Format []string `yaml:"format,omitempty"`
}

// Global is 'teleport' (global) section of the config file
Expand Down
Loading

0 comments on commit 4d916e4

Please sign in to comment.