Merge pull request #1180 from prometheus/logup
Upgrade common/log vendoring
This commit is contained in:
commit
cf7fda4371
|
@ -16,6 +16,8 @@ package log
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
|
@ -39,10 +41,71 @@ func (f levelFlag) Set(level string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func setSyslogFormatter(appname, local string) error {
|
||||
if appname == "" {
|
||||
return fmt.Errorf("missing appname paramter")
|
||||
}
|
||||
if local == "" {
|
||||
return fmt.Errorf("missing local paramter")
|
||||
}
|
||||
|
||||
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 syslog: %v", err)
|
||||
return err
|
||||
}
|
||||
origLogger.Formatter = fmter
|
||||
return nil
|
||||
}
|
||||
|
||||
func setJSONFormatter() {
|
||||
origLogger.Formatter = &logrus.JSONFormatter{}
|
||||
}
|
||||
|
||||
type logFormatFlag struct{ uri string }
|
||||
|
||||
// String implements flag.Value.
|
||||
func (f logFormatFlag) String() string {
|
||||
return f.uri
|
||||
}
|
||||
|
||||
// Set implements flag.Value.
|
||||
func (f logFormatFlag) Set(format string) error {
|
||||
f.uri = format
|
||||
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()
|
||||
}
|
||||
|
||||
switch u.Opaque {
|
||||
case "syslog":
|
||||
appname := u.Query().Get("appname")
|
||||
facility := u.Query().Get("local")
|
||||
return setSyslogFormatter(appname, facility)
|
||||
case "stdout":
|
||||
origLogger.Out = os.Stdout
|
||||
case "stderr":
|
||||
origLogger.Out = os.Stderr
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unsupported logger %s", u.Opaque)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
// In order for this flag to take effect, the user of the package must call
|
||||
// In order for these flags 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(logFormatFlag{}, "log.format", "If set use a syslog logger or JSON logging. Example: logger:syslog?appname=bob&local=7 or logger:stdout?json=true. Defaults to stderr.")
|
||||
}
|
||||
|
||||
type Logger interface {
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
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, facility string, fmter logrus.Formatter) (*syslogger, error) {
|
||||
priority, err := getFacility(facility)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := syslog.New(priority, appname)
|
||||
return &syslogger{
|
||||
out: out,
|
||||
wrap: fmter,
|
||||
}, err
|
||||
}
|
||||
|
||||
func getFacility(facility string) (syslog.Priority, error) {
|
||||
switch facility {
|
||||
case "0":
|
||||
return syslog.LOG_LOCAL0, nil
|
||||
case "1":
|
||||
return syslog.LOG_LOCAL1, nil
|
||||
case "2":
|
||||
return syslog.LOG_LOCAL2, nil
|
||||
case "3":
|
||||
return syslog.LOG_LOCAL3, nil
|
||||
case "4":
|
||||
return syslog.LOG_LOCAL4, nil
|
||||
case "5":
|
||||
return syslog.LOG_LOCAL5, nil
|
||||
case "6":
|
||||
return syslog.LOG_LOCAL6, nil
|
||||
case "7":
|
||||
return syslog.LOG_LOCAL7, nil
|
||||
}
|
||||
return syslog.LOG_LOCAL0, fmt.Errorf("invalid local(%s) for syslog", facility)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
|
@ -169,8 +169,8 @@
|
|||
},
|
||||
{
|
||||
"path": "github.com/prometheus/common/log",
|
||||
"revision": "4c9032a3f7aceade2585f2bbe302bf8c9ea3e9a9",
|
||||
"revisionTime": "2015-10-01T18:35:54+02:00"
|
||||
"revision": "d34c5f15e0237463f3e12b7912ec8066e8fcda2f",
|
||||
"revisionTime": "2015-10-24T15:29:39-04:00"
|
||||
},
|
||||
{
|
||||
"path": "github.com/prometheus/common/model",
|
||||
|
@ -273,4 +273,4 @@
|
|||
"revisionTime": "2015-06-24T11:29:02+01:00"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue