Skip to content

Commit

Permalink
Config Option to Enable Logging with Local Time (influxdata#9123)
Browse files Browse the repository at this point in the history
* Configurable local time logging

* make timezone configurable

* Address linter feedback.

* update with example
  • Loading branch information
ivorybilled authored Apr 16, 2021
1 parent e9a69a0 commit 1a86fd1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 = `
Expand Down
4 changes: 4 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:
Expand Down
4 changes: 4 additions & 0 deletions etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions etc/telegraf_windows.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 24 additions & 5 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"regexp"
"strings"
"time"

"github.com/influxdata/telegraf/config"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1a86fd1

Please sign in to comment.