Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TELEGRAF-7: Support telegraf log into syslog #39

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"io"
"log"
"log/syslog"
"os"
"regexp"
"strings"
Expand All @@ -19,6 +20,7 @@ var prefixRegex = regexp.MustCompile("^[DIWE]!")
const (
LogTargetFile = "file"
LogTargetStderr = "stderr"
LogTargetSyslog = "syslog"
)

// LogConfig contains the log configuration settings
Expand Down Expand Up @@ -134,6 +136,12 @@ func (t *telegrafLogCreator) CreateLogger(config LogConfig) (io.Writer, error) {
}
case LogTargetStderr, "":
writer = defaultWriter
case LogTargetSyslog:
var e error
writer, e = syslog.New(syslog.LOG_NOTICE, "telegraf")
if e != nil {
writer = defaultWriter
}
default:
log.Printf("E! Unsupported logtarget: %s, using stderr", config.LogTarget)
writer = defaultWriter
Expand Down Expand Up @@ -177,6 +185,7 @@ func newLogWriter(config LogConfig) io.Writer {
func init() {
tlc := &telegrafLogCreator{}
registerLogger("", tlc)
registerLogger(LogTargetSyslog, tlc)
registerLogger(LogTargetStderr, tlc)
registerLogger(LogTargetFile, tlc)
}
11 changes: 7 additions & 4 deletions plugins/inputs/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
ReOpen: true,
Follow: true,
Location: seek,
MustExist: true,
MustExist: false,
Poll: poll,
Pipe: t.Pipe,
Logger: tail.DiscardingLogger,
Expand All @@ -281,9 +281,6 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
t.Log.Debugf("Failed to open file (%s): %v", file, err)
return
}
t.tailers[tailer.Filename] = tailer

t.Log.Debugf("Tail added for %q", file)

parser, err := t.parserFunc()
if err != nil {
Expand Down Expand Up @@ -367,6 +364,12 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
case <-timeout:
}

_, isTailerAdded := t.tailers[tailer.Filename]
if !isTailerAdded {
t.tailers[tailer.Filename] = tailer
t.Log.Debugf("Tail added for %q", tailer.Filename)
}

var text string

if line != nil {
Expand Down
Loading