revendor with lgr 0.6.1

This commit is contained in:
Umputun
2019-03-31 16:21:32 -05:00
parent 29dc368ea8
commit 45ae2e6f13
6 changed files with 51 additions and 30 deletions
+2 -2
View File
@@ -62,10 +62,10 @@ func main() {
func setupLog(dbg bool) {
if dbg {
log.Setup(log.Debug, log.CallerFile, log.Msec, log.LevelBraces)
log.Setup(log.Debug, log.CallerFile, log.Msec)
return
}
log.Setup(log.Msec, log.LevelBraces, log.CallerPkg)
log.Setup(log.Msec, log.CallerPkg)
}
// getDump reads runtime stack and returns as a string
+1 -1
View File
@@ -15,7 +15,7 @@ require (
github.com/go-chi/render v1.0.0
github.com/go-pkgz/auth v0.5.0
github.com/go-pkgz/lcw v0.2.0
github.com/go-pkgz/lgr v0.6.0
github.com/go-pkgz/lgr v0.6.1
github.com/go-pkgz/mongo v1.1.2
github.com/go-pkgz/repeater v1.1.1
github.com/go-pkgz/rest v1.4.0
+2
View File
@@ -33,6 +33,8 @@ github.com/go-pkgz/lcw v0.2.0/go.mod h1:k+PY1CkCMTLXILtFoJOyK65Qqi9rkoTYunFH1vE/
github.com/go-pkgz/lgr v0.2.2/go.mod h1:hBM1NM/SoYdlrykgdgJWGrZ/TM/XaZIjRbJfx7NkMm8=
github.com/go-pkgz/lgr v0.6.0 h1:Z9FRhfSyuASiF05iXRj+clTAAl8+1EfLR7SeyLIaGKg=
github.com/go-pkgz/lgr v0.6.0/go.mod h1:hBM1NM/SoYdlrykgdgJWGrZ/TM/XaZIjRbJfx7NkMm8=
github.com/go-pkgz/lgr v0.6.1 h1:poohUbv/iguoQ6bzJ5j/Ubl1VcsjU+rzTbbxsKbkGXk=
github.com/go-pkgz/lgr v0.6.1/go.mod h1:hBM1NM/SoYdlrykgdgJWGrZ/TM/XaZIjRbJfx7NkMm8=
github.com/go-pkgz/mongo v1.0.0/go.mod h1:R9si/F2aJsjz4MUxhzuppIHY8yLV3YCeuCpgcI50cu4=
github.com/go-pkgz/mongo v1.1.2 h1:2Vqn3CWQJkkx4gxxDiQUitAW2FN/CH26lKHkipmpKcc=
github.com/go-pkgz/mongo v1.1.2/go.mod h1:0NkWnzpiUxoL5fYZuttCtJrpC67oNDidfYxcdPqHTf0=
+3 -3
View File
@@ -7,14 +7,14 @@
## usage
```go
l := lgr.New(lgr.Debug, lgr.CallerFile) // allow debug and caller file info
l := lgr.New(lgr.Msec, lgr.Debug, lgr.CallerFile, lgr.CallerFunc) // allow debug and caller info, timestamp with milliseconds
l.Logf("INFO some important message, %v", err)
l.Logf("DEBUG some less important message, %v", err)
```
output looks like this:
```
2018/01/07 13:02:34.000 INFO {svc/handler.go:101 h.MyFunc1} some important message, can't open file`
2018/01/07 13:02:34.000 INFO {svc/handler.go:101 h.MyFunc1} some important message, can't open file myfile.xyz
2018/01/07 13:02:34.015 DEBUG {svc/handler.go:155 h.MyFunc2} some less important message, file is too small`
```
@@ -25,7 +25,7 @@ _Without `lgr.Caller*` it will drop `{caller}` part_
### interfaces and default loggers
- `lgr` package provides a single interface `lgr.L` with a single method `Logf(format string, args ...interface{})`. Function wrapper `lgr.Func` allows to make `lgr.L` from a function directly.
- Default logger functionality can be used without `lgr.New`, but just `lgr.Printf`
- Default logger functionality can be used without `lgr.New` (see "global logger")
- Two predefined loggers available: `lgr.NoOp` (do-nothing logger) and `lgr.Std` (passing directly to stdlib log)
### options
+42 -23
View File
@@ -1,3 +1,11 @@
// Package lgr provides a simple logger with some extras. Primary way to log is Logf method.
// The logger's output can be customized in 2 ways:
// - by passing formatting template, i.e. lgr.New(lgr.Format(lgr.Short))
// - by setting individual formatting flags, i.e. lgr.New(lgr.Msec, lgr.CallerFunc)
// Leveled output works for messages based on level prefix, i.e. Logf("INFO some message") means INFO level.
// Debug and trace levels can be filtered based on lgr.Trace and lgr.Debug options.
// ERROR, FATAL and PANIC levels send to err as well. Both FATAL and PANIC also print stack trace and terminate caller application with os.Exit(1)
package lgr
import (
@@ -30,11 +38,11 @@ type Logger struct {
stdout, stderr io.Writer // destination writes for out and err
dbg bool // allows reporting for DEBUG level
trace bool // allows reporting for TRACE and DEBUG levels
callerFile bool // reports caller file, i.e. /go/src/github.com/go-pkgz/lgr/logger.go
callerFunc bool // reports caller function name, i.e. foo/bar.myFunc
callerFile bool // reports caller file with line number, i.e. foo/bar.go:89
callerFunc bool // reports caller function name, i.e. bar.myFunc
callerPkg bool // reports caller package name
levelBraces bool // encloses level with [], i.e. [INFO]
callerDepth int // how many stack frames to skip
callerDepth int // how many stack frames to skip, relative to the real (reported) frame
format string // layout template
// internal use
@@ -51,6 +59,7 @@ type Logger struct {
type nowFn func() time.Time
type panicFn func()
// layout holds all parts to construct the final message with template
type layout struct {
DT time.Time
Level string
@@ -61,8 +70,8 @@ type layout struct {
CallerLine int
}
// New makes new leveled logger. Accepts dbg flag turing on info about the caller and allowing DEBUG messages.
// Two writers can be passed optionally - first for out and second for err
// New makes new leveled logger. By default writes to stdout/stderr.
// default format: 2018/01/07 13:02:34.123 DEBUG some message 123
func New(options ...Option) *Logger {
res := Logger{
@@ -101,9 +110,9 @@ func New(options ...Option) *Logger {
}
// Logf implements L interface to output with printf style.
// Each line prefixed with ts, level and optionally (dbg mode only) by caller info.
// DEBUG and TRACE filtered out by dbg and trace flags.
// ERROR and FATAL also send the same line to err writer.
// FATAL adds runtime stack and os.exit(1), like panic.
// FATAL and PANIC adds runtime stack and os.exit(1), like panic.
func (l *Logger) Logf(format string, args ...interface{}) {
// to align call depth between (*Logger).Logf() and, for example, Printf()
l.logf(format, args...)
@@ -120,7 +129,7 @@ func (l *Logger) logf(format string, args ...interface{}) {
}
ci := callerInfo{}
if l.callerOn { // optimization to avlod expensive caller evaluation if not in template
if l.callerOn { // optimization to avoid expensive caller evaluation if caller info not in the template
ci = l.reportCaller(l.callerDepth)
}
@@ -142,7 +151,7 @@ func (l *Logger) logf(format string, args ...interface{}) {
buf.WriteString("\n")
data := buf.Bytes()
if l.levelBracesOn {
if l.levelBracesOn { // rearrange space in short levels
data = bytes.Replace(data, []byte("[WARN ]"), []byte("[WARN] "), 1)
data = bytes.Replace(data, []byte("[INFO ]"), []byte("[INFO] "), 1)
}
@@ -150,14 +159,17 @@ func (l *Logger) logf(format string, args ...interface{}) {
l.lock.Lock()
_, _ = l.stdout.Write(data)
// write to err as well for high levels
// write to err as well for high levels, exit(1) on fatal and panic and dump stack on panic level
switch lv {
case "PANIC", "FATAL":
case "ERROR":
_, _ = l.stderr.Write(data)
case "FATAL":
_, _ = l.stderr.Write(data)
l.fatal()
case "PANIC":
_, _ = l.stderr.Write(data)
_, _ = l.stderr.Write(getDump())
l.fatal()
case "ERROR":
_, _ = l.stderr.Write(data)
}
l.lock.Unlock()
@@ -218,9 +230,15 @@ func (l *Logger) reportCaller(calldepth int) (res callerInfo) {
return res
}
// make template from options flag
// make template from option flags
func (l *Logger) templateFromOptions() (res string) {
const (
// escape { and } from templates to allow "{some/blah}" output for caller
openCallerBrace = `{{"{"}}`
closeCallerBrace = `{{"}"}}`
)
orElse := func(flag bool, value string, elseValue string) string {
if flag {
return value
@@ -244,7 +262,7 @@ func (l *Logger) templateFromOptions() (res string) {
if v := orElse(l.callerPkg, `{{.CallerPkg}}`, ""); v != "" {
callerParts = append(callerParts, v)
}
parts = append(parts, "("+strings.Join(callerParts, " ")+")")
parts = append(parts, openCallerBrace+strings.Join(callerParts, " ")+closeCallerBrace)
}
parts = append(parts, "{{.Message}}")
return strings.Join(parts, " ")
@@ -291,14 +309,14 @@ func getDump() []byte {
// Option func type
type Option func(l *Logger)
// Out sets out writer
// Out sets out writer, stdout by default
func Out(w io.Writer) Option {
return func(l *Logger) {
l.stdout = w
}
}
// Err sets error writer
// Err sets error writer, stderr by default
func Err(w io.Writer) Option {
return func(l *Logger) {
l.stderr = w
@@ -316,40 +334,41 @@ func Trace(l *Logger) {
l.trace = true
}
// CallerDepth sets number of stack frame skipped for caller reporting
// CallerDepth sets number of stack frame skipped for caller reporting, 0 by default
func CallerDepth(n int) Option {
return func(l *Logger) {
l.callerDepth = n
}
}
// Format sets output layout, overwrites all options for individual parts, i.e. Caller*, Msec and LevelBraces
func Format(f string) Option {
return func(l *Logger) {
l.format = f
}
}
// CallerFunc adds caller info with function name
// CallerFunc adds caller info with function name. Ignored if Format option used.
func CallerFunc(l *Logger) {
l.callerFunc = true
}
// CallerPkg adds caller's package name
// CallerPkg adds caller's package name. Ignored if Format option used.
func CallerPkg(l *Logger) {
l.callerPkg = true
}
// LevelBraces adds [] to level
// LevelBraces surrounds level with [], i.e. [INFO]. Ignored if Format option used.
func LevelBraces(l *Logger) {
l.levelBraces = true
}
// CallerFile adds caller info with file, and line number
// CallerFile adds caller info with file, and line number. Ignored if Format option used.
func CallerFile(l *Logger) {
l.callerFile = true
}
// Msec adds .msec to timestamp
// Msec adds .msec to timestamp. Ignored if Format option used.
func Msec(l *Logger) {
l.msec = true
}
+1 -1
View File
@@ -39,7 +39,7 @@ github.com/go-pkgz/auth/logger
github.com/go-pkgz/auth/middleware
# github.com/go-pkgz/lcw v0.2.0
github.com/go-pkgz/lcw
# github.com/go-pkgz/lgr v0.6.0
# github.com/go-pkgz/lgr v0.6.1
github.com/go-pkgz/lgr
# github.com/go-pkgz/mongo v1.1.2
github.com/go-pkgz/mongo