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{
InitFormat: loggerConfig.Format,
EnableColors: trace.IsTerminal(os.Stderr),
}
err := formatter.CheckAndSetDefaults()
if err != nil {
return trace.Wrap(err)
}
logger.Formatter = formatter
return nil
}

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

func TestCheckAndSetDefaults(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The name of the test method does not hint that it has anything to do with the text formatter type.

tests := []struct {
comment string
formatConfig []string
hasError bool
}{
{
comment: "invalid key (does not exist)",
formatConfig: []string{"level", "invalid key"},
hasError: true,
quinqu marked this conversation as resolved.
Show resolved Hide resolved
},
{
comment: "valid keys and formatting",
formatConfig: []string{"level", "component", "message"},
hasError: false,
},
}

for _, tt := range tests {
t.Run(tt.comment, func(t *testing.T) {
formatter := &textFormatter{
InitFormat: tt.formatConfig,
}
err := formatter.CheckAndSetDefaults()
if tt.hasError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}

}
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 defines the format in which each log line will be outputted. 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.

Suggested change
// Format defines the format in which each log line will be outputted. Example format: [timestamp, component, message]
// Format lists the mandatory output fields from TextFormatterFields. Example format: [timestamp, component, message]

hence I would store all known fields in a variable (TextFormatterFields) that can be referred to.

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

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