Skip to content

Commit

Permalink
expose logfile config parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Hsn723 committed Jan 29, 2024
1 parent f83d249 commit 5748ee2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
36 changes: 26 additions & 10 deletions logsource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -62,17 +64,31 @@ 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) {
if f.path == "" {
return nil, nil
}
log.Printf("Reading log events from %s", f.path)
return NewFileLogSource(f.path)
return NewFileLogSource(f.path, f.config())
}
4 changes: 2 additions & 2 deletions logsource_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 5748ee2

Please sign in to comment.