From 5748ee2e9c476ac2752fbf77a3146d8597b5dea6 Mon Sep 17 00:00:00 2001 From: natsuki-hoshino Date: Tue, 5 Dec 2023 05:00:28 +0000 Subject: [PATCH] expose logfile config parameters --- logsource_file.go | 36 ++++++++++++++++++++++++++---------- logsource_file_test.go | 4 ++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/logsource_file.go b/logsource_file.go index afcfa68..77465c7 100644 --- a/logsource_file.go +++ b/logsource_file.go @@ -9,20 +9,22 @@ import ( "github.com/nxadm/tail" ) +var defaultConfig = tail.Config{ + ReOpen: true, // reopen the file if it's rotated + MustExist: true, // fail immediately if the file is missing or has incorrect permissions + Follow: true, // run in follow mode + Location: &tail.SeekInfo{Whence: io.SeekEnd}, // seek to end of file + Logger: tail.DiscardingLogger, + } + // A FileLogSource can read lines from a file. type FileLogSource struct { tailer *tail.Tail } // NewFileLogSource creates a new log source, tailing the given file. -func NewFileLogSource(path string) (*FileLogSource, error) { - tailer, err := tail.TailFile(path, tail.Config{ - ReOpen: true, // reopen the file if it's rotated - MustExist: true, // fail immediately if the file is missing or has incorrect permissions - Follow: true, // run in follow mode - Location: &tail.SeekInfo{Whence: io.SeekEnd}, // seek to end of file - Logger: tail.DiscardingLogger, - }) +func NewFileLogSource(path string, config tail.Config) (*FileLogSource, error) { + tailer, err := tail.TailFile(path, config) if err != nil { return nil, err } @@ -62,11 +64,25 @@ func (s *FileLogSource) Read(ctx context.Context) (string, error) { // Because this factory is enabled by default, it must always be // registered last. type fileLogSourceFactory struct { - path string + path string + mustExist bool + debug bool } func (f *fileLogSourceFactory) Init(app *kingpin.Application) { app.Flag("postfix.logfile_path", "Path where Postfix writes log entries.").Default("/var/log/mail.log").StringVar(&f.path) + app.Flag("postfix.logfile_must_exist", "Fail if the log file doesn't exist.").Default("true").BoolVar(&f.mustExist) + app.Flag("postfix.logfile_debug", "Enable debug logging for the log file.").Default("false").BoolVar(&f.debug) +} + +// config returns a tail.Config configured from the factory's fields. +func (f fileLogSourceFactory) config() tail.Config { + conf := defaultConfig + conf.MustExist = f.mustExist + if f.debug { + conf.Logger = tail.DefaultLogger + } + return conf } func (f *fileLogSourceFactory) New(ctx context.Context) (LogSourceCloser, error) { @@ -74,5 +90,5 @@ func (f *fileLogSourceFactory) New(ctx context.Context) (LogSourceCloser, error) return nil, nil } log.Printf("Reading log events from %s", f.path) - return NewFileLogSource(f.path) + return NewFileLogSource(f.path, f.config()) } diff --git a/logsource_file_test.go b/logsource_file_test.go index 83baeef..e031163 100644 --- a/logsource_file_test.go +++ b/logsource_file_test.go @@ -18,7 +18,7 @@ func TestFileLogSource_Path(t *testing.T) { } defer close() - src, err := NewFileLogSource(path) + src, err := NewFileLogSource(path, defaultConfig) if err != nil { t.Fatalf("NewFileLogSource failed: %v", err) } @@ -36,7 +36,7 @@ func TestFileLogSource_Read(t *testing.T) { } defer close() - src, err := NewFileLogSource(path) + src, err := NewFileLogSource(path, defaultConfig) if err != nil { t.Fatalf("NewFileLogSource failed: %v", err) }