Config Option to Enable Logging with Local Time (#9123)

* Configurable local time logging

* make timezone configurable

* Address linter feedback.

* update with example
This commit is contained in:
David Bennett 2021-04-16 14:39:19 -04:00 committed by GitHub
parent e9a69a0c6b
commit 1a86fd1a2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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