diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index f7df79272..60001fb60 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -174,6 +174,7 @@ func runAgent(ctx context.Context, RotationInterval: ag.Config.Agent.LogfileRotationInterval, RotationMaxSize: ag.Config.Agent.LogfileRotationMaxSize, RotationMaxArchives: ag.Config.Agent.LogfileRotationMaxArchives, + LogWithTimezone: ag.Config.Agent.LogWithTimezone, } logger.SetupLogging(logConfig) diff --git a/config/config.go b/config/config.go index 610f79cc3..c1bf9235f 100644 --- a/config/config.go +++ b/config/config.go @@ -188,6 +188,9 @@ type AgentConfig struct { // If set to -1, no archives are removed. LogfileRotationMaxArchives int `toml:"logfile_rotation_max_archives"` + // Pick a timezone to use when logging or type 'local' for local time. + LogWithTimezone string `toml:"log_with_timezone"` + Hostname string OmitHostname bool } @@ -356,11 +359,14 @@ var agentConfig = ` ## If set to -1, no archives are removed. # logfile_rotation_max_archives = 5 + ## Pick a timezone to use when logging or type 'local' for local time. + ## Example: America/Chicago + # log_with_timezone = "" + ## Override default hostname, if empty use os.Hostname() hostname = "" ## If set to true, do no set the "host" tag in the telegraf agent. omit_hostname = false - ` var outputHeader = ` diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index d97c86ba0..4965a4337 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -219,6 +219,10 @@ The agent table configures Telegraf and the defaults used across all plugins. Maximum number of rotated archives to keep, any older logs are deleted. If set to -1, no archives are removed. +- **log_with_timezone**: + Pick a timezone to use when logging or type 'local' for local time. Example: 'America/Chicago'. + [See this page for options/formats.](https://socketloop.com/tutorials/golang-display-list-of-timezones-with-gmt) + - **hostname**: Override default hostname, if empty use os.Hostname() - **omit_hostname**: diff --git a/etc/telegraf.conf b/etc/telegraf.conf index 32ccfd62b..9e02cc4c3 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -90,6 +90,10 @@ ## If set to -1, no archives are removed. # logfile_rotation_max_archives = 5 + ## Pick a timezone to use when logging or type 'local' for local time. Example: 'America/Chicago'. + ## See https://socketloop.com/tutorials/golang-display-list-of-timezones-with-gmt for timezone formatting options. + # log_with_timezone = "" + ## Override default hostname, if empty use os.Hostname() hostname = "" ## If set to true, do no set the "host" tag in the telegraf agent. diff --git a/etc/telegraf_windows.conf b/etc/telegraf_windows.conf index 5b7092899..5b7ca9505 100644 --- a/etc/telegraf_windows.conf +++ b/etc/telegraf_windows.conf @@ -90,6 +90,10 @@ ## If set to -1, no archives are removed. # logfile_rotation_max_archives = 5 + ## Pick a timezone to use when logging or type 'local' for local time. Example: 'America/Chicago'. + ## See https://socketloop.com/tutorials/golang-display-list-of-timezones-with-gmt for timezone formatting options. + # log_with_timezone = "" + ## Override default hostname, if empty use os.Hostname() hostname = "" ## If set to true, do no set the "host" tag in the telegraf agent. diff --git a/logger/logger.go b/logger/logger.go index c365c0573..27e3c79f1 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -6,6 +6,7 @@ import ( "log" "os" "regexp" + "strings" "time" "github.com/influxdata/telegraf/config" @@ -38,6 +39,8 @@ type LogConfig struct { RotationMaxSize config.Size // maximum rotated files to keep (older ones will be deleted) RotationMaxArchives int + // pick a timezone to use when logging. or type 'local' for local time. + LogWithTimezone string } type LoggerCreator interface { @@ -56,15 +59,19 @@ func registerLogger(name string, loggerCreator LoggerCreator) { type telegrafLog struct { writer io.Writer internalWriter io.Writer + timezone *time.Location } func (t *telegrafLog) Write(b []byte) (n int, err error) { var line []byte + timeToPrint := time.Now().In(t.timezone) + if !prefixRegex.Match(b) { - line = append([]byte(time.Now().UTC().Format(time.RFC3339)+" I! "), b...) + line = append([]byte(timeToPrint.Format(time.RFC3339)+" I! "), b...) } else { - line = append([]byte(time.Now().UTC().Format(time.RFC3339)+" "), b...) + line = append([]byte(timeToPrint.Format(time.RFC3339)+" "), b...) } + return t.writer.Write(line) } @@ -82,11 +89,23 @@ func (t *telegrafLog) Close() error { } // newTelegrafWriter returns a logging-wrapped writer. -func newTelegrafWriter(w io.Writer) io.Writer { +func newTelegrafWriter(w io.Writer, c LogConfig) (io.Writer, error) { + timezoneName := c.LogWithTimezone + + if strings.ToLower(timezoneName) == "local" { + timezoneName = "Local" + } + + tz, err := time.LoadLocation(timezoneName) + if err != nil { + return nil, errors.New("error while setting logging timezone: " + err.Error()) + } + return &telegrafLog{ writer: wlog.NewWriter(w), internalWriter: w, - } + timezone: tz, + }, nil } // SetupLogging configures the logging output. @@ -119,7 +138,7 @@ func (t *telegrafLogCreator) CreateLogger(config LogConfig) (io.Writer, error) { writer = defaultWriter } - return newTelegrafWriter(writer), nil + return newTelegrafWriter(writer, config) } // Keep track what is actually set as a log output, because log package doesn't provide a getter. diff --git a/logger/logger_test.go b/logger/logger_test.go index 8bb01e8e5..d2c699da5 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -137,7 +137,10 @@ func TestLogTargetSettings(t *testing.T) { func BenchmarkTelegrafLogWrite(b *testing.B) { var msg = []byte("test") var buf bytes.Buffer - w := newTelegrafWriter(&buf) + w, err := newTelegrafWriter(&buf, LogConfig{}) + if err != nil { + panic("Unable to create log writer.") + } for i := 0; i < b.N; i++ { buf.Reset() w.Write(msg)