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

feat: make syslog logging useable #411

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
58 changes: 49 additions & 9 deletions accesslog/file_and_loggregator_access_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,47 @@ type FileAndLoggregatorAccessLogger struct {
logsender schema.LogSender
}

type CustomWriter struct {
Name string
Writer io.Writer
PerformTruncate bool
type CustomWriter interface {
Name() string
io.Writer
}

// SyslogWriter sends logs to a [syslog.Writer].
type SyslogWriter struct {
name string
truncate int
*syslog.Writer
}

func (w *SyslogWriter) Name() string {
return w.name
}

func (w *SyslogWriter) Write(b []byte) (int, error) {
n := len(b)
if w.truncate > 0 && n > w.truncate {
n = w.truncate
}
return w.Writer.Write(b[:n])
}

// FileWriter sends logs to a [os.File] and appends a new line to each line written to seperate log
// lines.
type FileWriter struct {
name string
*os.File
}

func (w *FileWriter) Name() string {
return w.name
}

func (w *FileWriter) Write(b []byte) (int, error) {
return w.File.Write(append(b, byte('\n')))
}

func CreateRunningAccessLogger(logger *slog.Logger, logsender schema.LogSender, config *config.Config) (AccessLogger, error) {
if config.AccessLog.File == "" && !config.Logging.LoggregatorEnabled {
if config.AccessLog.File == "" && !config.Logging.LoggregatorEnabled && !config.AccessLog.EnableStreaming {
return &NullAccessLogger{}, nil
}

Expand All @@ -66,7 +99,10 @@ func CreateRunningAccessLogger(logger *slog.Logger, logsender schema.LogSender,
return nil, err
}

accessLogger.addWriter(CustomWriter{Name: "accesslog", Writer: file, PerformTruncate: false})
accessLogger.addWriter(&FileWriter{
name: "accesslog",
File: file,
})
}

if config.AccessLog.EnableStreaming {
Expand All @@ -76,7 +112,11 @@ func CreateRunningAccessLogger(logger *slog.Logger, logsender schema.LogSender,
return nil, err
}

accessLogger.addWriter(CustomWriter{Name: "syslog", Writer: syslogWriter, PerformTruncate: true})
accessLogger.addWriter(&SyslogWriter{
name: "syslog",
truncate: config.Logging.SyslogTruncate,
Writer: syslogWriter,
})
}

go accessLogger.Run()
Expand All @@ -88,9 +128,9 @@ func (x *FileAndLoggregatorAccessLogger) Run() {
select {
case record := <-x.channel:
for _, w := range x.writers {
_, err := record.WriteTo(w.Writer)
_, err := record.WriteTo(w)
if err != nil {
x.logger.Error(fmt.Sprintf("error-emitting-access-log-to-writer-%s", w.Name), log.ErrAttr(err))
x.logger.Error(fmt.Sprintf("error-emitting-access-log-to-writer-%s", w.Name()), log.ErrAttr(err))
}
}
record.SendLog(x.logsender)
Expand Down
8 changes: 2 additions & 6 deletions accesslog/schema/access_log_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,8 @@ func formatHeader(headers http.Header, name string, performTruncate bool) string

// WriteTo allows the AccessLogRecord to implement the io.WriterTo interface
func (r *AccessLogRecord) WriteTo(w io.Writer) (int64, error) {
bytesWritten, err := w.Write(r.getRecord(false))
if err != nil {
return int64(bytesWritten), err
}
newline, err := w.Write([]byte("\n"))
return int64(bytesWritten + newline), err
n, err := w.Write(r.getRecord(false))
return int64(n), err
}

func (r *AccessLogRecord) SendLog(ls LogSender) {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type LoggingConfig struct {
Syslog string `yaml:"syslog"`
SyslogAddr string `yaml:"syslog_addr"`
SyslogNetwork string `yaml:"syslog_network"`
SyslogTruncate int `yaml:"syslog_truncate"`
Level string `yaml:"level"`
LoggregatorEnabled bool `yaml:"loggregator_enabled"`
MetronAddress string `yaml:"metron_address"`
Expand Down