Format logs and remove timestamp from default log format (#5979)

* make log formatting customizable

* import trace

* add documentation for log formatting

* add testing and validate input

* PR feedback

* nit

* PR feedback

* remove format values dupe

* remove message

* remove timestamp from default log formatting
This commit is contained in:
jane quin 2021-03-15 18:45:34 -07:00 committed by GitHub
parent cd399e704c
commit e174428adb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 16 deletions

View file

@ -56,11 +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). Possible format values include: timestamp, component, message, caller, and level.
# and ERROR (default). Possible format values include: timestamp, component, caller, and level.
log:
output: /var/lib/teleport/teleport.log
severity: ERROR
format: [level, timestamp, component, message]
format: [level, timestamp, component, caller]
# 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"

View file

@ -459,7 +459,7 @@ 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 lists the output fields from KnownFormatFields. Example format: [timestamp, component, caller]
Format []string `yaml:"format,omitempty"`
}

View file

@ -27,6 +27,7 @@ import (
"strings"
"time"
"github.com/gravitational/teleport/api/utils"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)
@ -75,7 +76,6 @@ func (tf *textFormatter) CheckAndSetDefaults() error {
// set log formatting
if tf.LogFormat == nil {
tf.timestampEnabled = true
tf.callerEnabled = true
tf.LogFormat = KnownFormatFields.names()
return nil
@ -85,11 +85,12 @@ func (tf *textFormatter) CheckAndSetDefaults() error {
if err != nil {
return trace.Wrap(err)
}
if contains(res, timestampField) {
if utils.SliceContainsStr(res, timestampField) {
tf.timestampEnabled = true
}
if contains(res, callerField) {
if utils.SliceContainsStr(res, callerField) {
tf.callerEnabled = true
}
@ -362,13 +363,3 @@ func parseInputFormat(formatInput []string) (result []string, err error) {
}
return result, nil
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}