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
Merged

Conversation

quinqu
Copy link
Contributor

@quinqu quinqu commented Mar 8, 2021

Purpose

Format logs with according to teleport config specification.

Implementation

Created a new formatter based off of logrus's text formatter, this new formatter writes to the log line in the order that the user specifies in the file.

Example log config in teleport config file:

log:
    format: [timestamp, component, message]

Resolves #2676

@quinqu quinqu force-pushed the jane/formatLogs branch from 0041318 to e1a74f5 Compare March 8, 2021 22:37
Copy link
Contributor

@a-palchikov a-palchikov left a comment

Choose a reason for hiding this comment

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

cannot seem to find a way to delete a misplaced PR comment - I did not mean to approve it, there are still open questions.

lib/config/configuration.go Outdated Show resolved Hide resolved
lib/config/configuration.go Outdated Show resolved Hide resolved
lib/config/configuration.go Outdated Show resolved Hide resolved
lib/config/configuration.go Outdated Show resolved Hide resolved
lib/config/configuration.go Outdated Show resolved Hide resolved
docs/pages/config-reference.mdx Outdated Show resolved Hide resolved
for _, match := range tf.InputFormat {
switch match {
case "level":
// level
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this

lib/config/configuration.go Outdated Show resolved Hide resolved
lib/config/configuration.go Outdated Show resolved Hide resolved
lib/config/configuration.go Outdated Show resolved Hide resolved
@xacrimon
Copy link
Contributor

xacrimon commented Mar 9, 2021

LGTM once existing comments are resolved.

@quinqu quinqu marked this pull request as ready for review March 9, 2021 22:20
lib/config/configuration_test.go Outdated Show resolved Hide resolved
@@ -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.

}

// DefaultLoggerFormat is the default format in which to display logs (i.e. log.format in teleport.yaml is empty)
var DefaultLoggerFormat = []string{"level", "component", "message", "caller"}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: move known formatter field literals into corresponding consts since they're used on multiple occasions.

// Defaults to filePathAndLine() if unspecified
FormatCaller func() (caller string)
// InitFormat is the initial format of log configuration
InitFormat []string
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need both InitFormat and InputFormat - looks like they're the same most of the time and InitFormat is only used for validation.

lib/config/formatter.go Outdated Show resolved Hide resolved
@@ -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.

lib/config/configuration_test.go Show resolved Hide resolved
lib/config/formatter.go Outdated Show resolved Hide resolved
lib/config/formatter.go Outdated Show resolved Hide resolved
lib/config/formatter.go Outdated Show resolved Hide resolved
lib/config/formatter.go Outdated Show resolved Hide resolved
lib/config/formatter.go Outdated Show resolved Hide resolved
lib/config/formatter.go Outdated Show resolved Hide resolved
lib/config/formatter.go Show resolved Hide resolved
Copy link
Contributor

@russjones russjones left a comment

Choose a reason for hiding this comment

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

Bot.

@quinqu quinqu merged commit 4d916e4 into master Mar 12, 2021
@quinqu quinqu deleted the jane/formatLogs branch March 12, 2021 00:45
if err != nil {
return trace.Wrap(err)
}
if contains(res, timestampField) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for late reply: contains is a dupe for api.types.SliceContainsStr

@@ -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

@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Timestamp to logging output
6 participants