update dependency go-logging

socks-filter
Bruce Leidl 9 years ago
parent 4208f93f94
commit 9259438ae5

3
Godeps/Godeps.json generated

@ -43,7 +43,8 @@
}, },
{ {
"ImportPath": "github.com/op/go-logging", "ImportPath": "github.com/op/go-logging",
"Rev": "dfaf3dff9b631bc4236201d90d41ee0de9202889" "Comment": "v1-7-g970db52",
"Rev": "970db520ece77730c7e4724c61121037378659d9"
}, },
{ {
"ImportPath": "github.com/subgraph/go-procsnitch", "ImportPath": "github.com/subgraph/go-procsnitch",

@ -0,0 +1,19 @@
# Changelog
## 2.0.0-rc1 (2016-02-11)
Time flies and it has been three years since this package was first released.
There have been a couple of API changes I have wanted to do for some time but
I've tried to maintain backwards compatibility. Some inconsistencies in the
API have started to show, proper vendor support in Go out of the box and
the fact that `go vet` will give warnings -- I have decided to bump the major
version.
* Make eg. `Info` and `Infof` do different things. You want to change all calls
to `Info` with a string format go to `Infof` etc. In many cases, `go vet` will
guide you.
* `Id` in `Record` is now called `ID`
## 1.0.0 (2013-02-21)
Initial release

@ -7,6 +7,10 @@ is customizable and supports different logging backends like syslog, file and
memory. Multiple backends can be utilized with different log levels per backend memory. Multiple backends can be utilized with different log levels per backend
and logger. and logger.
**_NOTE:_** backwards compatibility promise have been dropped for master. Please
vendor this package or use `gopkg.in/op/go-logging.v1` for previous version. See
[changelog](CHANGELOG.md) for details.
## Example ## Example
Let's have a look at an [example](examples/example.go) which demonstrates most Let's have a look at an [example](examples/example.go) which demonstrates most
@ -74,7 +78,7 @@ func main() {
After this command *go-logging* is ready to use. Its source will be in: After this command *go-logging* is ready to use. Its source will be in:
$GOROOT/src/pkg/github.com/op/go-logging $GOPATH/src/pkg/github.com/op/go-logging
You can use `go get -u` to update the package. You can use `go get -u` to update the package.
@ -86,4 +90,4 @@ For docs, see http://godoc.org/github.com/op/go-logging or run:
## Additional resources ## Additional resources
* [wslog](https://godoc.org/github.com/cryptix/go/logging/wslog) -- exposes log messages through a WebSocket. * [wslog](https://godoc.org/github.com/cryptix/exp/wslog) -- exposes log messages through a WebSocket.

@ -14,6 +14,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -39,6 +40,7 @@ const (
fmtVerbShortpkg fmtVerbShortpkg
fmtVerbLongfunc fmtVerbLongfunc
fmtVerbShortfunc fmtVerbShortfunc
fmtVerbCallpath
fmtVerbLevelColor fmtVerbLevelColor
// Keep last, there are no match for these below. // Keep last, there are no match for these below.
@ -60,6 +62,7 @@ var fmtVerbs = []string{
"shortpkg", "shortpkg",
"longfunc", "longfunc",
"shortfunc", "shortfunc",
"callpath",
"color", "color",
} }
@ -79,6 +82,7 @@ var defaultVerbsLayout = []string{
"s", "s",
"s", "s",
"s", "s",
"0",
"", "",
} }
@ -159,6 +163,7 @@ type stringFormatter struct {
// %{message} Message (string) // %{message} Message (string)
// %{longfile} Full file name and line number: /a/b/c/d.go:23 // %{longfile} Full file name and line number: /a/b/c/d.go:23
// %{shortfile} Final file name element and line number: d.go:23 // %{shortfile} Final file name element and line number: d.go:23
// %{callpath} Callpath like main.a.b.c...c "..." meaning recursive call ~. meaning truncated path
// %{color} ANSI color based on log level // %{color} ANSI color based on log level
// //
// For normal types, the output can be customized by using the 'verbs' defined // For normal types, the output can be customized by using the 'verbs' defined
@ -175,6 +180,9 @@ type stringFormatter struct {
// "%{color:bold}%{time:15:04:05} %{level:-8s}%{color:reset} %{message}" will // "%{color:bold}%{time:15:04:05} %{level:-8s}%{color:reset} %{message}" will
// just colorize the time and level, leaving the message uncolored. // just colorize the time and level, leaving the message uncolored.
// //
// For the 'callpath' verb, the output can be adjusted to limit the printing
// the stack depth. i.e. '%{callpath:3}' will print '~.a.b.c'
//
// Colors on Windows is unfortunately not supported right now and is currently // Colors on Windows is unfortunately not supported right now and is currently
// a no-op. // a no-op.
// //
@ -187,6 +195,7 @@ type stringFormatter struct {
// %{shortpkg} Base package path, eg. go-logging // %{shortpkg} Base package path, eg. go-logging
// %{longfunc} Full function name, eg. littleEndian.PutUint32 // %{longfunc} Full function name, eg. littleEndian.PutUint32
// %{shortfunc} Base function name, eg. PutUint32 // %{shortfunc} Base function name, eg. PutUint32
// %{callpath} Call function path, eg. main.a.b.c
func NewStringFormatter(format string) (Formatter, error) { func NewStringFormatter(format string) (Formatter, error) {
var fmter = &stringFormatter{} var fmter = &stringFormatter{}
@ -211,12 +220,12 @@ func NewStringFormatter(format string) (Formatter, error) {
} }
// Handle layout customizations or use the default. If this is not for the // Handle layout customizations or use the default. If this is not for the
// time or color formatting, we need to prefix with %. // time, color formatting or callpath, we need to prefix with %.
layout := defaultVerbsLayout[verb] layout := defaultVerbsLayout[verb]
if m[4] != -1 { if m[4] != -1 {
layout = format[m[4]:m[5]] layout = format[m[4]:m[5]]
} }
if verb != fmtVerbTime && verb != fmtVerbLevelColor { if verb != fmtVerbTime && verb != fmtVerbLevelColor && verb != fmtVerbCallpath {
layout = "%" + layout layout = "%" + layout
} }
@ -233,12 +242,13 @@ func NewStringFormatter(format string) (Formatter, error) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
testFmt := "hello %s"
r := &Record{ r := &Record{
Id: 12345, ID: 12345,
Time: t, Time: t,
Module: "logger", Module: "logger",
fmt: "hello %s", Args: []interface{}{"go"},
args: []interface{}{"go"}, fmt: &testFmt,
} }
if err := fmter.Format(0, r, &bytes.Buffer{}); err != nil { if err := fmter.Format(0, r, &bytes.Buffer{}); err != nil {
return nil, err return nil, err
@ -269,6 +279,12 @@ func (f *stringFormatter) Format(calldepth int, r *Record, output io.Writer) err
output.Write([]byte(r.Time.Format(part.layout))) output.Write([]byte(r.Time.Format(part.layout)))
} else if part.verb == fmtVerbLevelColor { } else if part.verb == fmtVerbLevelColor {
doFmtVerbLevelColor(part.layout, r.Level, output) doFmtVerbLevelColor(part.layout, r.Level, output)
} else if part.verb == fmtVerbCallpath {
depth, err := strconv.Atoi(part.layout)
if err != nil {
depth = 0
}
output.Write([]byte(formatCallpath(calldepth+1, depth)))
} else { } else {
var v interface{} var v interface{}
switch part.verb { switch part.verb {
@ -276,7 +292,7 @@ func (f *stringFormatter) Format(calldepth int, r *Record, output io.Writer) err
v = r.Level v = r.Level
break break
case fmtVerbID: case fmtVerbID:
v = r.Id v = r.ID
break break
case fmtVerbPid: case fmtVerbPid:
v = pid v = pid
@ -343,6 +359,39 @@ func formatFuncName(v fmtVerb, f string) string {
panic("unexpected func formatter") panic("unexpected func formatter")
} }
func formatCallpath(calldepth int, depth int) string {
v := ""
callers := make([]uintptr, 64)
n := runtime.Callers(calldepth+2, callers)
oldPc := callers[n-1]
start := n - 3
if depth > 0 && start >= depth {
start = depth - 1
v += "~."
}
recursiveCall := false
for i := start; i >= 0; i-- {
pc := callers[i]
if oldPc == pc {
recursiveCall = true
continue
}
oldPc = pc
if recursiveCall {
recursiveCall = false
v += ".."
}
if i < start {
v += "."
}
if f := runtime.FuncForPC(pc); f != nil {
v += formatFuncName(fmtVerbShortfunc, f.Name())
}
}
return v
}
// backendFormatter combines a backend with a specific formatter making it // backendFormatter combines a backend with a specific formatter making it
// possible to have different log formats for different backends. // possible to have different log formats for different backends.
type backendFormatter struct { type backendFormatter struct {

@ -16,30 +16,30 @@ import (
type color int type color int
const ( const (
colorBlack = iota + 30 ColorBlack = iota + 30
colorRed ColorRed
colorGreen ColorGreen
colorYellow ColorYellow
colorBlue ColorBlue
colorMagenta ColorMagenta
colorCyan ColorCyan
colorWhite ColorWhite
) )
var ( var (
colors = []string{ colors = []string{
CRITICAL: colorSeq(colorMagenta), CRITICAL: ColorSeq(ColorMagenta),
ERROR: colorSeq(colorRed), ERROR: ColorSeq(ColorRed),
WARNING: colorSeq(colorYellow), WARNING: ColorSeq(ColorYellow),
NOTICE: colorSeq(colorGreen), NOTICE: ColorSeq(ColorGreen),
DEBUG: colorSeq(colorCyan), DEBUG: ColorSeq(ColorCyan),
} }
boldcolors = []string{ boldcolors = []string{
CRITICAL: colorSeqBold(colorMagenta), CRITICAL: ColorSeqBold(ColorMagenta),
ERROR: colorSeqBold(colorRed), ERROR: ColorSeqBold(ColorRed),
WARNING: colorSeqBold(colorYellow), WARNING: ColorSeqBold(ColorYellow),
NOTICE: colorSeqBold(colorGreen), NOTICE: ColorSeqBold(ColorGreen),
DEBUG: colorSeqBold(colorCyan), DEBUG: ColorSeqBold(ColorCyan),
} }
) )
@ -47,6 +47,7 @@ var (
type LogBackend struct { type LogBackend struct {
Logger *log.Logger Logger *log.Logger
Color bool Color bool
ColorConfig []string
} }
// NewLogBackend creates a new LogBackend. // NewLogBackend creates a new LogBackend.
@ -57,8 +58,13 @@ func NewLogBackend(out io.Writer, prefix string, flag int) *LogBackend {
// Log implements the Backend interface. // Log implements the Backend interface.
func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error { func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
if b.Color { if b.Color {
col := colors[level]
if len(b.ColorConfig) > int(level) && b.ColorConfig[level] != "" {
col = b.ColorConfig[level]
}
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
buf.Write([]byte(colors[level])) buf.Write([]byte(col))
buf.Write([]byte(rec.Formatted(calldepth + 1))) buf.Write([]byte(rec.Formatted(calldepth + 1)))
buf.Write([]byte("\033[0m")) buf.Write([]byte("\033[0m"))
// For some reason, the Go logger arbitrarily decided "2" was the correct // For some reason, the Go logger arbitrarily decided "2" was the correct
@ -69,11 +75,26 @@ func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
return b.Logger.Output(calldepth+2, rec.Formatted(calldepth+1)) return b.Logger.Output(calldepth+2, rec.Formatted(calldepth+1))
} }
func colorSeq(color color) string { // ConvertColors takes a list of ints representing colors for log levels and
// converts them into strings for ANSI color formatting
func ConvertColors(colors []int, bold bool) []string {
converted := []string{}
for _, i := range colors {
if bold {
converted = append(converted, ColorSeqBold(color(i)))
} else {
converted = append(converted, ColorSeq(color(i)))
}
}
return converted
}
func ColorSeq(color color) string {
return fmt.Sprintf("\033[%dm", int(color)) return fmt.Sprintf("\033[%dm", int(color))
} }
func colorSeqBold(color color) string { func ColorSeqBold(color color) string {
return fmt.Sprintf("\033[%d;1m", int(color)) return fmt.Sprintf("\033[%d;1m", int(color))
} }

@ -41,16 +41,16 @@ var (
// was created, an increasing id, filename and line and finally the actual // was created, an increasing id, filename and line and finally the actual
// formatted log line. // formatted log line.
type Record struct { type Record struct {
Id uint64 ID uint64
Time time.Time Time time.Time
Module string Module string
Level Level Level Level
Args []interface{}
// message is kept as a pointer to have shallow copies update this once // message is kept as a pointer to have shallow copies update this once
// needed. // needed.
message *string message *string
args []interface{} fmt *string
fmt string
formatter Formatter formatter Formatter
formatted string formatted string
} }
@ -69,12 +69,20 @@ func (r *Record) Formatted(calldepth int) string {
func (r *Record) Message() string { func (r *Record) Message() string {
if r.message == nil { if r.message == nil {
// Redact the arguments that implements the Redactor interface // Redact the arguments that implements the Redactor interface
for i, arg := range r.args { for i, arg := range r.Args {
if redactor, ok := arg.(Redactor); ok == true { if redactor, ok := arg.(Redactor); ok == true {
r.args[i] = redactor.Redacted() r.Args[i] = redactor.Redacted()
} }
} }
msg := fmt.Sprintf(r.fmt, r.args...) var buf bytes.Buffer
if r.fmt != nil {
fmt.Fprintf(&buf, *r.fmt, r.Args...)
} else {
// use Fprintln to make sure we always get space between arguments
fmt.Fprintln(&buf, r.Args...)
buf.Truncate(buf.Len() - 1) // strip newline
}
msg := buf.String()
r.message = &msg r.message = &msg
} }
return *r.message return *r.message
@ -132,19 +140,19 @@ func (l *Logger) IsEnabledFor(level Level) bool {
return defaultBackend.IsEnabledFor(level, l.Module) return defaultBackend.IsEnabledFor(level, l.Module)
} }
func (l *Logger) log(lvl Level, format string, args ...interface{}) { func (l *Logger) log(lvl Level, format *string, args ...interface{}) {
if !l.IsEnabledFor(lvl) { if !l.IsEnabledFor(lvl) {
return return
} }
// Create the logging record and pass it in to the backend // Create the logging record and pass it in to the backend
record := &Record{ record := &Record{
Id: atomic.AddUint64(&sequenceNo, 1), ID: atomic.AddUint64(&sequenceNo, 1),
Time: timeNow(), Time: timeNow(),
Module: l.Module, Module: l.Module,
Level: lvl, Level: lvl,
fmt: format, fmt: format,
args: args, Args: args,
} }
// TODO use channels to fan out the records to all backends? // TODO use channels to fan out the records to all backends?
@ -164,84 +172,86 @@ func (l *Logger) log(lvl Level, format string, args ...interface{}) {
// Fatal is equivalent to l.Critical(fmt.Sprint()) followed by a call to os.Exit(1). // Fatal is equivalent to l.Critical(fmt.Sprint()) followed by a call to os.Exit(1).
func (l *Logger) Fatal(args ...interface{}) { func (l *Logger) Fatal(args ...interface{}) {
s := fmt.Sprint(args...) l.log(CRITICAL, nil, args...)
l.log(CRITICAL, "%s", s)
os.Exit(1) os.Exit(1)
} }
// Fatalf is equivalent to l.Critical followed by a call to os.Exit(1). // Fatalf is equivalent to l.Critical followed by a call to os.Exit(1).
func (l *Logger) Fatalf(format string, args ...interface{}) { func (l *Logger) Fatalf(format string, args ...interface{}) {
l.log(CRITICAL, format, args...) l.log(CRITICAL, &format, args...)
os.Exit(1) os.Exit(1)
} }
// Panic is equivalent to l.Critical(fmt.Sprint()) followed by a call to panic(). // Panic is equivalent to l.Critical(fmt.Sprint()) followed by a call to panic().
func (l *Logger) Panic(args ...interface{}) { func (l *Logger) Panic(args ...interface{}) {
s := fmt.Sprint(args...) l.log(CRITICAL, nil, args...)
l.log(CRITICAL, "%s", s) panic(fmt.Sprint(args...))
panic(s)
} }
// Panicf is equivalent to l.Critical followed by a call to panic(). // Panicf is equivalent to l.Critical followed by a call to panic().
func (l *Logger) Panicf(format string, args ...interface{}) { func (l *Logger) Panicf(format string, args ...interface{}) {
s := fmt.Sprintf(format, args...) l.log(CRITICAL, &format, args...)
l.log(CRITICAL, "%s", s) panic(fmt.Sprintf(format, args...))
panic(s)
} }
// Critical logs a message using CRITICAL as log level. // Critical logs a message using CRITICAL as log level.
func (l *Logger) Critical(format string, args ...interface{}) { func (l *Logger) Critical(args ...interface{}) {
l.log(CRITICAL, format, args...) l.log(CRITICAL, nil, args...)
}
// Criticalf logs a message using CRITICAL as log level.
func (l *Logger) Criticalf(format string, args ...interface{}) {
l.log(CRITICAL, &format, args...)
} }
// Error logs a message using ERROR as log level. // Error logs a message using ERROR as log level.
func (l *Logger) Error(format string, args ...interface{}) { func (l *Logger) Error(args ...interface{}) {
l.log(ERROR, format, args...) l.log(ERROR, nil, args...)
} }
// Errorf logs a message using ERROR as log level. // Errorf logs a message using ERROR as log level.
func (l *Logger) Errorf(format string, args ...interface{}) { func (l *Logger) Errorf(format string, args ...interface{}) {
l.log(ERROR, format, args...) l.log(ERROR, &format, args...)
} }
// Warning logs a message using WARNING as log level. // Warning logs a message using WARNING as log level.
func (l *Logger) Warning(format string, args ...interface{}) { func (l *Logger) Warning(args ...interface{}) {
l.log(WARNING, format, args...) l.log(WARNING, nil, args...)
} }
// Warningf logs a message using WARNING as log level. // Warningf logs a message using WARNING as log level.
func (l *Logger) Warningf(format string, args ...interface{}) { func (l *Logger) Warningf(format string, args ...interface{}) {
l.log(WARNING, format, args...) l.log(WARNING, &format, args...)
} }
// Notice logs a message using NOTICE as log level. // Notice logs a message using NOTICE as log level.
func (l *Logger) Notice(format string, args ...interface{}) { func (l *Logger) Notice(args ...interface{}) {
l.log(NOTICE, format, args...) l.log(NOTICE, nil, args...)
} }
// Noticef logs a message using NOTICE as log level. // Noticef logs a message using NOTICE as log level.
func (l *Logger) Noticef(format string, args ...interface{}) { func (l *Logger) Noticef(format string, args ...interface{}) {
l.log(NOTICE, format, args...) l.log(NOTICE, &format, args...)
} }
// Info logs a message using INFO as log level. // Info logs a message using INFO as log level.
func (l *Logger) Info(format string, args ...interface{}) { func (l *Logger) Info(args ...interface{}) {
l.log(INFO, format, args...) l.log(INFO, nil, args...)
} }
// Infof logs a message using INFO as log level. // Infof logs a message using INFO as log level.
func (l *Logger) Infof(format string, args ...interface{}) { func (l *Logger) Infof(format string, args ...interface{}) {
l.log(INFO, format, args...) l.log(INFO, &format, args...)
} }
// Debug logs a message using DEBUG as log level. // Debug logs a message using DEBUG as log level.
func (l *Logger) Debug(format string, args ...interface{}) { func (l *Logger) Debug(args ...interface{}) {
l.log(DEBUG, format, args...) l.log(DEBUG, nil, args...)
} }
// Debugf logs a message using DEBUG as log level. // Debugf logs a message using DEBUG as log level.
func (l *Logger) Debugf(format string, args ...interface{}) { func (l *Logger) Debugf(format string, args ...interface{}) {
l.log(DEBUG, format, args...) l.log(DEBUG, &format, args...)
} }
func init() { func init() {

Loading…
Cancel
Save