2016-09-30 21:37:56 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2019-05-03 17:25:28 +00:00
|
|
|
"errors"
|
2016-09-30 21:37:56 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-04-12 17:41:26 +00:00
|
|
|
"regexp"
|
2016-10-11 12:06:17 +00:00
|
|
|
"time"
|
2016-09-30 21:37:56 +00:00
|
|
|
|
2019-05-03 17:25:28 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/internal/rotate"
|
2016-09-30 21:37:56 +00:00
|
|
|
"github.com/influxdata/wlog"
|
|
|
|
)
|
|
|
|
|
2017-04-12 17:41:26 +00:00
|
|
|
var prefixRegex = regexp.MustCompile("^[DIWE]!")
|
|
|
|
|
2019-08-28 21:34:44 +00:00
|
|
|
const (
|
|
|
|
LogTargetFile = "file"
|
|
|
|
LogTargetStderr = "stderr"
|
|
|
|
)
|
2016-09-30 21:37:56 +00:00
|
|
|
|
2019-05-03 17:25:28 +00:00
|
|
|
// LogConfig contains the log configuration settings
|
|
|
|
type LogConfig struct {
|
|
|
|
// will set the log level to DEBUG
|
|
|
|
Debug bool
|
|
|
|
//will set the log level to ERROR
|
|
|
|
Quiet bool
|
2019-08-28 21:34:44 +00:00
|
|
|
//stderr, stdout, file or eventlog (Windows only)
|
|
|
|
LogTarget string
|
2019-05-03 17:25:28 +00:00
|
|
|
// will direct the logging output to a file. Empty string is
|
|
|
|
// interpreted as stderr. If there is an error opening the file the
|
|
|
|
// logger will fallback to stderr
|
|
|
|
Logfile string
|
|
|
|
// will rotate when current file at the specified time interval
|
|
|
|
RotationInterval internal.Duration
|
|
|
|
// will rotate when current file size exceeds this parameter.
|
|
|
|
RotationMaxSize internal.Size
|
|
|
|
// maximum rotated files to keep (older ones will be deleted)
|
|
|
|
RotationMaxArchives int
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:34:44 +00:00
|
|
|
type LoggerCreator interface {
|
|
|
|
CreateLogger(config LogConfig) (io.Writer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var loggerRegistry map[string]LoggerCreator
|
|
|
|
|
|
|
|
func registerLogger(name string, loggerCreator LoggerCreator) {
|
|
|
|
if loggerRegistry == nil {
|
|
|
|
loggerRegistry = make(map[string]LoggerCreator)
|
|
|
|
}
|
|
|
|
loggerRegistry[name] = loggerCreator
|
|
|
|
}
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
type telegrafLog struct {
|
2019-05-03 17:25:28 +00:00
|
|
|
writer io.Writer
|
|
|
|
internalWriter io.Writer
|
2016-09-30 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:06:17 +00:00
|
|
|
func (t *telegrafLog) Write(b []byte) (n int, err error) {
|
2017-04-12 17:41:26 +00:00
|
|
|
var line []byte
|
|
|
|
if !prefixRegex.Match(b) {
|
|
|
|
line = append([]byte(time.Now().UTC().Format(time.RFC3339)+" I! "), b...)
|
|
|
|
} else {
|
|
|
|
line = append([]byte(time.Now().UTC().Format(time.RFC3339)+" "), b...)
|
|
|
|
}
|
|
|
|
return t.writer.Write(line)
|
2016-09-30 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 17:25:28 +00:00
|
|
|
func (t *telegrafLog) Close() error {
|
2019-08-28 21:34:44 +00:00
|
|
|
var stdErrWriter io.Writer
|
|
|
|
stdErrWriter = os.Stderr
|
|
|
|
// avoid closing stderr
|
|
|
|
if t.internalWriter != stdErrWriter {
|
|
|
|
closer, isCloser := t.internalWriter.(io.Closer)
|
|
|
|
if !isCloser {
|
|
|
|
return errors.New("the underlying writer cannot be closed")
|
|
|
|
}
|
|
|
|
return closer.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTelegrafWriter returns a logging-wrapped writer.
|
|
|
|
func newTelegrafWriter(w io.Writer) io.Writer {
|
|
|
|
return &telegrafLog{
|
|
|
|
writer: wlog.NewWriter(w),
|
|
|
|
internalWriter: w,
|
2019-05-03 17:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
// SetupLogging configures the logging output.
|
2019-05-03 17:25:28 +00:00
|
|
|
func SetupLogging(config LogConfig) {
|
|
|
|
newLogWriter(config)
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:34:44 +00:00
|
|
|
type telegrafLogCreator struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *telegrafLogCreator) CreateLogger(config LogConfig) (io.Writer, error) {
|
|
|
|
var writer, defaultWriter io.Writer
|
|
|
|
defaultWriter = os.Stderr
|
|
|
|
|
|
|
|
switch config.LogTarget {
|
|
|
|
case LogTargetFile:
|
|
|
|
if config.Logfile != "" {
|
|
|
|
var err error
|
|
|
|
if writer, err = rotate.NewFileWriter(config.Logfile, config.RotationInterval.Duration, config.RotationMaxSize.Size, config.RotationMaxArchives); err != nil {
|
|
|
|
log.Printf("E! Unable to open %s (%s), using stderr", config.Logfile, err)
|
|
|
|
writer = defaultWriter
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writer = defaultWriter
|
|
|
|
}
|
|
|
|
case LogTargetStderr, "":
|
|
|
|
writer = defaultWriter
|
|
|
|
default:
|
|
|
|
log.Printf("E! Unsupported logtarget: %s, using stderr", config.LogTarget)
|
|
|
|
writer = defaultWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
return newTelegrafWriter(writer), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track what is actually set as a log output, because log package doesn't provide a getter.
|
|
|
|
// It allows closing previous writer if re-set and have possibility to test what is actually set
|
|
|
|
var actualLogger io.Writer
|
|
|
|
|
2019-05-03 17:25:28 +00:00
|
|
|
func newLogWriter(config LogConfig) io.Writer {
|
2016-10-11 12:06:17 +00:00
|
|
|
log.SetFlags(0)
|
2019-05-03 17:25:28 +00:00
|
|
|
if config.Debug {
|
2016-09-30 21:37:56 +00:00
|
|
|
wlog.SetLevel(wlog.DEBUG)
|
|
|
|
}
|
2019-05-03 17:25:28 +00:00
|
|
|
if config.Quiet {
|
2016-09-30 21:37:56 +00:00
|
|
|
wlog.SetLevel(wlog.ERROR)
|
|
|
|
}
|
2019-08-28 21:34:44 +00:00
|
|
|
if !config.Debug && !config.Quiet {
|
|
|
|
wlog.SetLevel(wlog.INFO)
|
|
|
|
}
|
|
|
|
var logWriter io.Writer
|
|
|
|
if logCreator, ok := loggerRegistry[config.LogTarget]; ok {
|
|
|
|
logWriter, _ = logCreator.CreateLogger(config)
|
|
|
|
}
|
|
|
|
if logWriter == nil {
|
|
|
|
logWriter, _ = (&telegrafLogCreator{}).CreateLogger(config)
|
|
|
|
}
|
2016-09-30 21:37:56 +00:00
|
|
|
|
2019-08-28 21:34:44 +00:00
|
|
|
if closer, isCloser := actualLogger.(io.Closer); isCloser {
|
|
|
|
closer.Close()
|
2016-09-30 21:37:56 +00:00
|
|
|
}
|
2019-08-28 21:34:44 +00:00
|
|
|
log.SetOutput(logWriter)
|
|
|
|
actualLogger = logWriter
|
|
|
|
|
|
|
|
return logWriter
|
|
|
|
}
|
2016-09-30 21:37:56 +00:00
|
|
|
|
2019-08-28 21:34:44 +00:00
|
|
|
func init() {
|
|
|
|
tlc := &telegrafLogCreator{}
|
|
|
|
registerLogger("", tlc)
|
|
|
|
registerLogger(LogTargetStderr, tlc)
|
|
|
|
registerLogger(LogTargetFile, tlc)
|
2016-09-30 21:37:56 +00:00
|
|
|
}
|