Skip to content

Commit

Permalink
Add structured logging to json and support for syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkanwisher committed Oct 21, 2015
1 parent 1cc3e4c commit ca23863
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
51 changes: 51 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package log
import (
"flag"
"fmt"
"net/url"
"os"
"runtime"
"strings"

Expand All @@ -39,10 +41,59 @@ func (f levelFlag) Set(level string) error {
return nil
}

func setFormatterSylog(appname, local string) error {
if appname != "" && local != "" {
return nil
}

fmter, err := newSyslogger(appname, local, origLogger.Formatter)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating syslog formatter: %v\n", err)
origLogger.Errorf("can't connect logger to syslogs: %v", err)
return err
}
origLogger.Formatter = fmter
return nil
}

func setJsonFormatter() {
origLogger.Formatter = &logrus.JSONFormatter{}
}

type formatLog struct{}

// String implements flag.Value.
func (f formatLog) String() string {
return origLogger.Level.String()
}

// Set implements flag.Value.
func (f formatLog) Set(format string) error {
u, err := url.Parse(format)
if err != nil {
return err
}
if u.Scheme != "logger" {
return fmt.Errorf("Invalid scheme %s", u.Scheme)
}
jsonq := u.Query().Get("json")
if jsonq == "true" {
setJsonFormatter()
}

if u.Opaque == "syslog" {
appname := u.Query().Get("appname")
local := u.Query().Get("local")
return setFormatterSylog(appname, local)
}
return nil
}

func init() {
// In order for this flag to take effect, the user of the package must call
// flag.Parse() before logging anything.
flag.Var(levelFlag{}, "log.level", "Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal].")
flag.Var(formatLog{}, "log.format", "If set use a syslog logger or json logging. Example: logger:syslog?appname=bob&local=7 or logger:stdout@?json=true ")
}

type Logger interface {
Expand Down
81 changes: 81 additions & 0 deletions log/syslog_formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package log

import (
"fmt"
"log/syslog"
"os"

"github.com/Sirupsen/logrus"
)

var ceeTag = []byte("@cee:")

type syslogger struct {
wrap logrus.Formatter
out *syslog.Writer
}

func newSyslogger(appname string, level string, fmter logrus.Formatter) (*syslogger, error) {
priority := getLocal(level)
out, err := syslog.New(priority, appname)
return &syslogger{
out: out,
wrap: fmter,
}, err
}

func getLocal(level string) syslog.Priority {
switch level {
case "0":
return syslog.LOG_LOCAL0
case "1":
return syslog.LOG_LOCAL1
case "2":
return syslog.LOG_LOCAL2
case "3":
return syslog.LOG_LOCAL3
case "4":
return syslog.LOG_LOCAL4
case "5":
return syslog.LOG_LOCAL5
case "6":
return syslog.LOG_LOCAL6
case "7":
return syslog.LOG_LOCAL7
}
return syslog.LOG_LOCAL0
}

func (s *syslogger) Format(e *logrus.Entry) ([]byte, error) {
data, err := s.wrap.Format(e)
if err != nil {
fmt.Fprintf(os.Stderr, "syslogger: can't format entry: %v\n", err)
return data, err
}
// only append tag to data sent to syslog (line), not to what
// is returned
line := string(append(ceeTag, data...))

switch e.Level {
case logrus.PanicLevel:
err = s.out.Crit(line)
case logrus.FatalLevel:
err = s.out.Crit(line)
case logrus.ErrorLevel:
err = s.out.Err(line)
case logrus.WarnLevel:
err = s.out.Warning(line)
case logrus.InfoLevel:
err = s.out.Info(line)
case logrus.DebugLevel:
err = s.out.Debug(line)
default:
err = s.out.Notice(line)
}

if err != nil {
fmt.Fprintf(os.Stderr, "syslogger: can't send log to syslog: %v\n", err)
}

return data, err
}

0 comments on commit ca23863

Please sign in to comment.