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

Format Logs and add timestamp to logging output option #5898

Merged
merged 15 commits into from
Mar 12, 2021
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message

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())

})
}

}
quinqu marked this conversation as resolved.
Show resolved Hide resolved
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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message

Format []string `yaml:"format,omitempty"`
}

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