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

Added basic syslog support #486

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions log/extended_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package log

import "io"

// SpecializedWriter represents an extended io.Writer class that can perform
// contextualized writes.
type SpecializedWriter interface {
GetSpecializedWriter(keyvals ...interface{}) io.Writer
}

// specializedWriter returns a specialized writer for the specified keys or
// falls back to returning w if no specialized writer is available.
func specializedWriter(w io.Writer, keyvals ...interface{}) io.Writer {
if ew, ok := w.(SpecializedWriter); ok {
w = ew.GetSpecializedWriter(keyvals)
}

return w
}
2 changes: 1 addition & 1 deletion log/json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (l *jsonLogger) Log(keyvals ...interface{}) error {
}
merge(m, k, v)
}
return json.NewEncoder(l.Writer).Encode(m)
return json.NewEncoder(specializedWriter(l.Writer)).Encode(m)
}

func merge(dst map[string]interface{}, k, v interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion log/logfmt_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (l logfmtLogger) Log(keyvals ...interface{}) error {
// The Logger interface requires implementations to be safe for concurrent
// use by multiple goroutines. For this implementation that means making
// only one call to l.w.Write() for each call to Log.
if _, err := l.w.Write(enc.buf.Bytes()); err != nil {
if _, err := specializedWriter(l.w).Write(enc.buf.Bytes()); err != nil {
return err
}
return nil
Expand Down
86 changes: 86 additions & 0 deletions log/syslog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// +build linux,!appengine darwin freebsd openbsd

package log

import (
"io"
"log/syslog"

"github.com/go-kit/kit/log/level"
)

type syslogWriter struct {
*syslog.Writer
selector func(keyvals ...interface{}) syslog.Priority
}

type SyslogAdapterOption interface {
Apply(*syslogWriter)
}

func NewSyslogWriter(w *syslog.Writer, options ...SyslogAdapterOption) io.Writer {
writer := &syslogWriter{
syslog.Writer: w,
selector: defaultSyslogSelector,
}

for _, option := range options {
option.Apply(writer)
}

return *writer
}

type syslogWriterAdapter struct {
f func(string) error
}

func (a *syslogWriterAdapter) Write(b []byte) (int, error) {
return len(b), a.f(string(b))
}

func (w syslogWriter) GetSpecializedWriter(keyvals ...interface{}) io.Writer {
priority := w.selector(keyvals...)

switch priority {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A complete implementation will of course handle all supported syslog.Priority values.

case syslog.LOG_DEBUG:
return syslogWriterAdapter{f: w.Debug}
case syslog.LOG_INFO:
return syslogWriterAdapter{f: w.Info}
case syslog.LOG_WARN:
return syslogWriterAdapter{f: w.Warn}
case syslog.LOG_ERR:
return syslogWriterAdapter{f: w.Error}
}

return w
}

func defaultSyslogSelector(keyvals ...interface{}) syslog.Priority {
for i := 1; i < len(keyvals); i += 2 {
if v, ok := keyvals[i].(*Value); ok {
switch v {
case level.DebugValue():
return syslog.LOG_DEBUG
case level.InfoValue():
return syslog.LOG_INFO
case level.WarnValue():
return syslog.LOG_WARN
case level.ErrorValue():
return syslog.LOG_ERR
}
}
}

return syslog.LOG_INFO
}

// SyslogPrioritySelector is an option that specifies the syslog priority
// selector.
type SyslogPrioritySelector struct {
PrioritySelector func(keyvals ...interface{}) syslog.Priority
}

func (o SyslogPrioritySelector) Apply(w *syslogWriter) {
w.selector = o.PrioritySelector
}