mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
updating github.com/spf13/jwalterweatherman to v1.1.0
This commit is contained in:
parent
1b401bdab7
commit
a6e9835daa
4
go.mod
4
go.mod
@ -132,7 +132,7 @@ require (
|
||||
github.com/spf13/afero v1.2.2
|
||||
github.com/spf13/cast v0.0.0-20160730092037-e31f36ffc91a // indirect
|
||||
github.com/spf13/cobra v0.0.4
|
||||
github.com/spf13/jwalterweatherman v0.0.0-20160311093646-33c24e77fb80 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.3
|
||||
github.com/spf13/viper v1.3.2
|
||||
github.com/storageos/go-api v0.0.0-20180912212459-343b3eff91fc
|
||||
@ -392,7 +392,7 @@ replace (
|
||||
github.com/spf13/afero => github.com/spf13/afero v1.2.2
|
||||
github.com/spf13/cast => github.com/spf13/cast v0.0.0-20160730092037-e31f36ffc91a
|
||||
github.com/spf13/cobra => github.com/spf13/cobra v0.0.4
|
||||
github.com/spf13/jwalterweatherman => github.com/spf13/jwalterweatherman v0.0.0-20160311093646-33c24e77fb80
|
||||
github.com/spf13/jwalterweatherman => github.com/spf13/jwalterweatherman v1.1.0
|
||||
github.com/spf13/pflag => github.com/spf13/pflag v1.0.1
|
||||
github.com/spf13/viper => github.com/spf13/viper v0.0.0-20160820190039-7fb2782df3d8
|
||||
github.com/storageos/go-api => github.com/storageos/go-api v0.0.0-20180912212459-343b3eff91fc
|
||||
|
4
go.sum
4
go.sum
@ -373,8 +373,8 @@ github.com/spf13/cast v0.0.0-20160730092037-e31f36ffc91a h1:tPI5RnYZJhcXj0LhJ9pi
|
||||
github.com/spf13/cast v0.0.0-20160730092037-e31f36ffc91a/go.mod h1:r2rcYCSwa1IExKTDiTfzaxqT2FNHs8hODu4LnUfgKEg=
|
||||
github.com/spf13/cobra v0.0.4 h1:S0tLZ3VOKl2Te0hpq8+ke0eSJPfCnNTPiDlsfwi1/NE=
|
||||
github.com/spf13/cobra v0.0.4/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
|
||||
github.com/spf13/jwalterweatherman v0.0.0-20160311093646-33c24e77fb80 h1:evyGXhHMrxKBDkdlSPv9HMWV2o53o+Ibhm28BGc0450=
|
||||
github.com/spf13/jwalterweatherman v0.0.0-20160311093646-33c24e77fb80/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
|
||||
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
|
||||
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
|
||||
github.com/spf13/pflag v1.0.1 h1:aCvUg6QPl3ibpQUxyLkrEkCHtPqYJL4x9AuhqVqFis4=
|
||||
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
|
||||
github.com/spf13/viper v0.0.0-20160820190039-7fb2782df3d8 h1:hh6dlnX5EheZdLFxYsRZt7d2z6lOnP8qVEksE/7UrhM=
|
||||
|
2
vendor/github.com/spf13/jwalterweatherman/.gitignore
generated
vendored
2
vendor/github.com/spf13/jwalterweatherman/.gitignore
generated
vendored
@ -20,3 +20,5 @@ _cgo_export.*
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.bench
|
||||
go.sum
|
6
vendor/github.com/spf13/jwalterweatherman/BUILD
generated
vendored
6
vendor/github.com/spf13/jwalterweatherman/BUILD
generated
vendored
@ -2,7 +2,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["thatswhyyoualwaysleaveanote.go"],
|
||||
srcs = [
|
||||
"default_notepad.go",
|
||||
"log_counter.go",
|
||||
"notepad.go",
|
||||
],
|
||||
importmap = "k8s.io/kubernetes/vendor/github.com/spf13/jwalterweatherman",
|
||||
importpath = "github.com/spf13/jwalterweatherman",
|
||||
visibility = ["//visibility:public"],
|
||||
|
37
vendor/github.com/spf13/jwalterweatherman/README.md
generated
vendored
37
vendor/github.com/spf13/jwalterweatherman/README.md
generated
vendored
@ -18,7 +18,7 @@ provides a few advantages over using the standard log library alone.
|
||||
I really wanted a very straightforward library that could seamlessly do
|
||||
the following things.
|
||||
|
||||
1. Replace all the println, printf, etc statements thought my code with
|
||||
1. Replace all the println, printf, etc statements thoughout my code with
|
||||
something more useful
|
||||
2. Allow the user to easily control what levels are printed to stdout
|
||||
3. Allow the user to easily control what levels are logged
|
||||
@ -44,7 +44,7 @@ Available Loggers are:
|
||||
* FATAL
|
||||
|
||||
These each are loggers based on the log standard library and follow the
|
||||
standard usage. Eg..
|
||||
standard usage. Eg.
|
||||
|
||||
```go
|
||||
import (
|
||||
@ -79,6 +79,13 @@ standard usage. Eg..
|
||||
|
||||
```
|
||||
|
||||
NOTE: You can also use the library in a non-global setting by creating an instance of a Notebook:
|
||||
|
||||
```go
|
||||
notepad = jww.NewNotepad(jww.LevelInfo, jww.LevelTrace, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
|
||||
notepad.WARN.Println("Some warning"")
|
||||
```
|
||||
|
||||
_Why 7 levels?_
|
||||
|
||||
Maybe you think that 7 levels are too much for any application... and you
|
||||
@ -118,35 +125,15 @@ verbosity.
|
||||
Note that JWW's own internal output uses log levels as well, so set the log
|
||||
level before making any other calls if you want to see what it's up to.
|
||||
|
||||
### Using a temp log file
|
||||
|
||||
JWW conveniently creates a temporary file and sets the log Handle to
|
||||
a io.Writer created for it. You should call this early in your application
|
||||
initialization routine as it will only log calls made after it is executed.
|
||||
When this option is used, the library will fmt.Println where to find the
|
||||
log file.
|
||||
|
||||
```go
|
||||
import (
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
)
|
||||
|
||||
jww.UseTempLogFile("YourAppName")
|
||||
|
||||
```
|
||||
|
||||
### Setting a log file
|
||||
|
||||
JWW can log to any file you provide a path to (provided it’s writable).
|
||||
Will only append to this file.
|
||||
JWW can log to any `io.Writer`:
|
||||
|
||||
|
||||
```go
|
||||
import (
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
)
|
||||
|
||||
jww.SetLogFile("/path/to/logfile")
|
||||
jww.SetLogOutput(customWriter)
|
||||
|
||||
```
|
||||
|
||||
@ -157,5 +144,5 @@ This is an early release. I’ve been using it for a while and this is the
|
||||
third interface I’ve tried. I like this one pretty well, but no guarantees
|
||||
that it won’t change a bit.
|
||||
|
||||
I wrote this for use in [hugo](http://hugo.spf13.com). If you are looking
|
||||
I wrote this for use in [hugo](https://gohugo.io). If you are looking
|
||||
for a static website engine that’s super fast please checkout Hugo.
|
||||
|
111
vendor/github.com/spf13/jwalterweatherman/default_notepad.go
generated
vendored
Normal file
111
vendor/github.com/spf13/jwalterweatherman/default_notepad.go
generated
vendored
Normal file
@ -0,0 +1,111 @@
|
||||
// Copyright © 2016 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package jwalterweatherman
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
TRACE *log.Logger
|
||||
DEBUG *log.Logger
|
||||
INFO *log.Logger
|
||||
WARN *log.Logger
|
||||
ERROR *log.Logger
|
||||
CRITICAL *log.Logger
|
||||
FATAL *log.Logger
|
||||
|
||||
LOG *log.Logger
|
||||
FEEDBACK *Feedback
|
||||
|
||||
defaultNotepad *Notepad
|
||||
)
|
||||
|
||||
func reloadDefaultNotepad() {
|
||||
TRACE = defaultNotepad.TRACE
|
||||
DEBUG = defaultNotepad.DEBUG
|
||||
INFO = defaultNotepad.INFO
|
||||
WARN = defaultNotepad.WARN
|
||||
ERROR = defaultNotepad.ERROR
|
||||
CRITICAL = defaultNotepad.CRITICAL
|
||||
FATAL = defaultNotepad.FATAL
|
||||
|
||||
LOG = defaultNotepad.LOG
|
||||
FEEDBACK = defaultNotepad.FEEDBACK
|
||||
}
|
||||
|
||||
func init() {
|
||||
defaultNotepad = NewNotepad(LevelError, LevelWarn, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// SetLogThreshold set the log threshold for the default notepad. Trace by default.
|
||||
func SetLogThreshold(threshold Threshold) {
|
||||
defaultNotepad.SetLogThreshold(threshold)
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// SetLogOutput set the log output for the default notepad. Discarded by default.
|
||||
func SetLogOutput(handle io.Writer) {
|
||||
defaultNotepad.SetLogOutput(handle)
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// SetStdoutThreshold set the standard output threshold for the default notepad.
|
||||
// Info by default.
|
||||
func SetStdoutThreshold(threshold Threshold) {
|
||||
defaultNotepad.SetStdoutThreshold(threshold)
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// SetStdoutOutput set the stdout output for the default notepad. Default is stdout.
|
||||
func SetStdoutOutput(handle io.Writer) {
|
||||
defaultNotepad.outHandle = handle
|
||||
defaultNotepad.init()
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// SetPrefix set the prefix for the default logger. Empty by default.
|
||||
func SetPrefix(prefix string) {
|
||||
defaultNotepad.SetPrefix(prefix)
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// SetFlags set the flags for the default logger. "log.Ldate | log.Ltime" by default.
|
||||
func SetFlags(flags int) {
|
||||
defaultNotepad.SetFlags(flags)
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// SetLogListeners configures the default logger with one or more log listeners.
|
||||
func SetLogListeners(l ...LogListener) {
|
||||
defaultNotepad.logListeners = l
|
||||
defaultNotepad.init()
|
||||
reloadDefaultNotepad()
|
||||
}
|
||||
|
||||
// Level returns the current global log threshold.
|
||||
func LogThreshold() Threshold {
|
||||
return defaultNotepad.logThreshold
|
||||
}
|
||||
|
||||
// Level returns the current global output threshold.
|
||||
func StdoutThreshold() Threshold {
|
||||
return defaultNotepad.stdoutThreshold
|
||||
}
|
||||
|
||||
// GetStdoutThreshold returns the defined Treshold for the log logger.
|
||||
func GetLogThreshold() Threshold {
|
||||
return defaultNotepad.GetLogThreshold()
|
||||
}
|
||||
|
||||
// GetStdoutThreshold returns the Treshold for the stdout logger.
|
||||
func GetStdoutThreshold() Threshold {
|
||||
return defaultNotepad.GetStdoutThreshold()
|
||||
}
|
7
vendor/github.com/spf13/jwalterweatherman/go.mod
generated
vendored
Normal file
7
vendor/github.com/spf13/jwalterweatherman/go.mod
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/spf13/jwalterweatherman
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.2.2
|
||||
)
|
46
vendor/github.com/spf13/jwalterweatherman/log_counter.go
generated
vendored
Normal file
46
vendor/github.com/spf13/jwalterweatherman/log_counter.go
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright © 2016 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package jwalterweatherman
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Counter is an io.Writer that increments a counter on Write.
|
||||
type Counter struct {
|
||||
count uint64
|
||||
}
|
||||
|
||||
func (c *Counter) incr() {
|
||||
atomic.AddUint64(&c.count, 1)
|
||||
}
|
||||
|
||||
// Reset resets the counter.
|
||||
func (c *Counter) Reset() {
|
||||
atomic.StoreUint64(&c.count, 0)
|
||||
}
|
||||
|
||||
// Count returns the current count.
|
||||
func (c *Counter) Count() uint64 {
|
||||
return atomic.LoadUint64(&c.count)
|
||||
}
|
||||
|
||||
func (c *Counter) Write(p []byte) (n int, err error) {
|
||||
c.incr()
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// LogCounter creates a LogListener that counts log statements >= the given threshold.
|
||||
func LogCounter(counter *Counter, t1 Threshold) LogListener {
|
||||
return func(t2 Threshold) io.Writer {
|
||||
if t2 < t1 {
|
||||
// Not interested in this threshold.
|
||||
return nil
|
||||
}
|
||||
return counter
|
||||
}
|
||||
}
|
225
vendor/github.com/spf13/jwalterweatherman/notepad.go
generated
vendored
Normal file
225
vendor/github.com/spf13/jwalterweatherman/notepad.go
generated
vendored
Normal file
@ -0,0 +1,225 @@
|
||||
// Copyright © 2016 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package jwalterweatherman
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Threshold int
|
||||
|
||||
func (t Threshold) String() string {
|
||||
return prefixes[t]
|
||||
}
|
||||
|
||||
const (
|
||||
LevelTrace Threshold = iota
|
||||
LevelDebug
|
||||
LevelInfo
|
||||
LevelWarn
|
||||
LevelError
|
||||
LevelCritical
|
||||
LevelFatal
|
||||
)
|
||||
|
||||
var prefixes map[Threshold]string = map[Threshold]string{
|
||||
LevelTrace: "TRACE",
|
||||
LevelDebug: "DEBUG",
|
||||
LevelInfo: "INFO",
|
||||
LevelWarn: "WARN",
|
||||
LevelError: "ERROR",
|
||||
LevelCritical: "CRITICAL",
|
||||
LevelFatal: "FATAL",
|
||||
}
|
||||
|
||||
// Notepad is where you leave a note!
|
||||
type Notepad struct {
|
||||
TRACE *log.Logger
|
||||
DEBUG *log.Logger
|
||||
INFO *log.Logger
|
||||
WARN *log.Logger
|
||||
ERROR *log.Logger
|
||||
CRITICAL *log.Logger
|
||||
FATAL *log.Logger
|
||||
|
||||
LOG *log.Logger
|
||||
FEEDBACK *Feedback
|
||||
|
||||
loggers [7]**log.Logger
|
||||
logHandle io.Writer
|
||||
outHandle io.Writer
|
||||
logThreshold Threshold
|
||||
stdoutThreshold Threshold
|
||||
prefix string
|
||||
flags int
|
||||
|
||||
logListeners []LogListener
|
||||
}
|
||||
|
||||
// A LogListener can ble supplied to a Notepad to listen on log writes for a given
|
||||
// threshold. This can be used to capture log events in unit tests and similar.
|
||||
// Note that this function will be invoked once for each log threshold. If
|
||||
// the given threshold is not of interest to you, return nil.
|
||||
// Note that these listeners will receive log events for a given threshold, even
|
||||
// if the current configuration says not to log it. That way you can count ERRORs even
|
||||
// if you don't print them to the console.
|
||||
type LogListener func(t Threshold) io.Writer
|
||||
|
||||
// NewNotepad creates a new Notepad.
|
||||
func NewNotepad(
|
||||
outThreshold Threshold,
|
||||
logThreshold Threshold,
|
||||
outHandle, logHandle io.Writer,
|
||||
prefix string, flags int,
|
||||
logListeners ...LogListener,
|
||||
) *Notepad {
|
||||
|
||||
n := &Notepad{logListeners: logListeners}
|
||||
|
||||
n.loggers = [7]**log.Logger{&n.TRACE, &n.DEBUG, &n.INFO, &n.WARN, &n.ERROR, &n.CRITICAL, &n.FATAL}
|
||||
n.outHandle = outHandle
|
||||
n.logHandle = logHandle
|
||||
n.stdoutThreshold = outThreshold
|
||||
n.logThreshold = logThreshold
|
||||
|
||||
if len(prefix) != 0 {
|
||||
n.prefix = "[" + prefix + "] "
|
||||
} else {
|
||||
n.prefix = ""
|
||||
}
|
||||
|
||||
n.flags = flags
|
||||
|
||||
n.LOG = log.New(n.logHandle,
|
||||
"LOG: ",
|
||||
n.flags)
|
||||
n.FEEDBACK = &Feedback{out: log.New(outHandle, "", 0), log: n.LOG}
|
||||
|
||||
n.init()
|
||||
return n
|
||||
}
|
||||
|
||||
// init creates the loggers for each level depending on the notepad thresholds.
|
||||
func (n *Notepad) init() {
|
||||
logAndOut := io.MultiWriter(n.outHandle, n.logHandle)
|
||||
|
||||
for t, logger := range n.loggers {
|
||||
threshold := Threshold(t)
|
||||
prefix := n.prefix + threshold.String() + " "
|
||||
|
||||
switch {
|
||||
case threshold >= n.logThreshold && threshold >= n.stdoutThreshold:
|
||||
*logger = log.New(n.createLogWriters(threshold, logAndOut), prefix, n.flags)
|
||||
|
||||
case threshold >= n.logThreshold:
|
||||
*logger = log.New(n.createLogWriters(threshold, n.logHandle), prefix, n.flags)
|
||||
|
||||
case threshold >= n.stdoutThreshold:
|
||||
*logger = log.New(n.createLogWriters(threshold, n.outHandle), prefix, n.flags)
|
||||
|
||||
default:
|
||||
*logger = log.New(n.createLogWriters(threshold, ioutil.Discard), prefix, n.flags)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notepad) createLogWriters(t Threshold, handle io.Writer) io.Writer {
|
||||
if len(n.logListeners) == 0 {
|
||||
return handle
|
||||
}
|
||||
writers := []io.Writer{handle}
|
||||
for _, l := range n.logListeners {
|
||||
w := l(t)
|
||||
if w != nil {
|
||||
writers = append(writers, w)
|
||||
}
|
||||
}
|
||||
|
||||
if len(writers) == 1 {
|
||||
return handle
|
||||
}
|
||||
|
||||
return io.MultiWriter(writers...)
|
||||
}
|
||||
|
||||
// SetLogThreshold changes the threshold above which messages are written to the
|
||||
// log file.
|
||||
func (n *Notepad) SetLogThreshold(threshold Threshold) {
|
||||
n.logThreshold = threshold
|
||||
n.init()
|
||||
}
|
||||
|
||||
// SetLogOutput changes the file where log messages are written.
|
||||
func (n *Notepad) SetLogOutput(handle io.Writer) {
|
||||
n.logHandle = handle
|
||||
n.init()
|
||||
}
|
||||
|
||||
// GetStdoutThreshold returns the defined Treshold for the log logger.
|
||||
func (n *Notepad) GetLogThreshold() Threshold {
|
||||
return n.logThreshold
|
||||
}
|
||||
|
||||
// SetStdoutThreshold changes the threshold above which messages are written to the
|
||||
// standard output.
|
||||
func (n *Notepad) SetStdoutThreshold(threshold Threshold) {
|
||||
n.stdoutThreshold = threshold
|
||||
n.init()
|
||||
}
|
||||
|
||||
// GetStdoutThreshold returns the Treshold for the stdout logger.
|
||||
func (n *Notepad) GetStdoutThreshold() Threshold {
|
||||
return n.stdoutThreshold
|
||||
}
|
||||
|
||||
// SetPrefix changes the prefix used by the notepad. Prefixes are displayed between
|
||||
// brackets at the beginning of the line. An empty prefix won't be displayed at all.
|
||||
func (n *Notepad) SetPrefix(prefix string) {
|
||||
if len(prefix) != 0 {
|
||||
n.prefix = "[" + prefix + "] "
|
||||
} else {
|
||||
n.prefix = ""
|
||||
}
|
||||
n.init()
|
||||
}
|
||||
|
||||
// SetFlags choose which flags the logger will display (after prefix and message
|
||||
// level). See the package log for more informations on this.
|
||||
func (n *Notepad) SetFlags(flags int) {
|
||||
n.flags = flags
|
||||
n.init()
|
||||
}
|
||||
|
||||
// Feedback writes plainly to the outHandle while
|
||||
// logging with the standard extra information (date, file, etc).
|
||||
type Feedback struct {
|
||||
out *log.Logger
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
func (fb *Feedback) Println(v ...interface{}) {
|
||||
fb.output(fmt.Sprintln(v...))
|
||||
}
|
||||
|
||||
func (fb *Feedback) Printf(format string, v ...interface{}) {
|
||||
fb.output(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (fb *Feedback) Print(v ...interface{}) {
|
||||
fb.output(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func (fb *Feedback) output(s string) {
|
||||
if fb.out != nil {
|
||||
fb.out.Output(2, s)
|
||||
}
|
||||
if fb.log != nil {
|
||||
fb.log.Output(2, s)
|
||||
}
|
||||
}
|
256
vendor/github.com/spf13/jwalterweatherman/thatswhyyoualwaysleaveanote.go
generated
vendored
256
vendor/github.com/spf13/jwalterweatherman/thatswhyyoualwaysleaveanote.go
generated
vendored
@ -1,256 +0,0 @@
|
||||
// Copyright © 2016 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package jwalterweatherman
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Level describes the chosen log level between
|
||||
// debug and critical.
|
||||
type Level int
|
||||
|
||||
type NotePad struct {
|
||||
Handle io.Writer
|
||||
Level Level
|
||||
Prefix string
|
||||
Logger **log.Logger
|
||||
counter uint64
|
||||
}
|
||||
|
||||
func (n *NotePad) incr() {
|
||||
atomic.AddUint64(&n.counter, 1)
|
||||
}
|
||||
|
||||
func (n *NotePad) resetCounter() {
|
||||
atomic.StoreUint64(&n.counter, 0)
|
||||
}
|
||||
|
||||
func (n *NotePad) getCount() uint64 {
|
||||
return atomic.LoadUint64(&n.counter)
|
||||
}
|
||||
|
||||
type countingWriter struct {
|
||||
incrFunc func()
|
||||
}
|
||||
|
||||
func (cw *countingWriter) Write(p []byte) (n int, err error) {
|
||||
cw.incrFunc()
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Feedback is special. It writes plainly to the output while
|
||||
// logging with the standard extra information (date, file, etc)
|
||||
// Only Println and Printf are currently provided for this
|
||||
type Feedback struct{}
|
||||
|
||||
const (
|
||||
LevelTrace Level = iota
|
||||
LevelDebug
|
||||
LevelInfo
|
||||
LevelWarn
|
||||
LevelError
|
||||
LevelCritical
|
||||
LevelFatal
|
||||
DefaultLogThreshold = LevelWarn
|
||||
DefaultStdoutThreshold = LevelError
|
||||
)
|
||||
|
||||
var (
|
||||
TRACE *log.Logger
|
||||
DEBUG *log.Logger
|
||||
INFO *log.Logger
|
||||
WARN *log.Logger
|
||||
ERROR *log.Logger
|
||||
CRITICAL *log.Logger
|
||||
FATAL *log.Logger
|
||||
LOG *log.Logger
|
||||
FEEDBACK Feedback
|
||||
LogHandle io.Writer = ioutil.Discard
|
||||
OutHandle io.Writer = os.Stdout
|
||||
BothHandle io.Writer = io.MultiWriter(LogHandle, OutHandle)
|
||||
NotePads []*NotePad = []*NotePad{trace, debug, info, warn, err, critical, fatal}
|
||||
|
||||
trace *NotePad = &NotePad{Level: LevelTrace, Handle: os.Stdout, Logger: &TRACE, Prefix: "TRACE: "}
|
||||
debug *NotePad = &NotePad{Level: LevelDebug, Handle: os.Stdout, Logger: &DEBUG, Prefix: "DEBUG: "}
|
||||
info *NotePad = &NotePad{Level: LevelInfo, Handle: os.Stdout, Logger: &INFO, Prefix: "INFO: "}
|
||||
warn *NotePad = &NotePad{Level: LevelWarn, Handle: os.Stdout, Logger: &WARN, Prefix: "WARN: "}
|
||||
err *NotePad = &NotePad{Level: LevelError, Handle: os.Stdout, Logger: &ERROR, Prefix: "ERROR: "}
|
||||
critical *NotePad = &NotePad{Level: LevelCritical, Handle: os.Stdout, Logger: &CRITICAL, Prefix: "CRITICAL: "}
|
||||
fatal *NotePad = &NotePad{Level: LevelFatal, Handle: os.Stdout, Logger: &FATAL, Prefix: "FATAL: "}
|
||||
logThreshold Level = DefaultLogThreshold
|
||||
outputThreshold Level = DefaultStdoutThreshold
|
||||
)
|
||||
|
||||
const (
|
||||
DATE = log.Ldate
|
||||
TIME = log.Ltime
|
||||
SFILE = log.Lshortfile
|
||||
LFILE = log.Llongfile
|
||||
MSEC = log.Lmicroseconds
|
||||
)
|
||||
|
||||
var logFlags = DATE | TIME | SFILE
|
||||
|
||||
func init() {
|
||||
SetStdoutThreshold(DefaultStdoutThreshold)
|
||||
}
|
||||
|
||||
// initialize will setup the jWalterWeatherman standard approach of providing the user
|
||||
// some feedback and logging a potentially different amount based on independent log and output thresholds.
|
||||
// By default the output has a lower threshold than logged
|
||||
// Don't use if you have manually set the Handles of the different levels as it will overwrite them.
|
||||
func initialize() {
|
||||
BothHandle = io.MultiWriter(LogHandle, OutHandle)
|
||||
|
||||
for _, n := range NotePads {
|
||||
if n.Level < outputThreshold && n.Level < logThreshold {
|
||||
n.Handle = ioutil.Discard
|
||||
} else if n.Level >= outputThreshold && n.Level >= logThreshold {
|
||||
n.Handle = BothHandle
|
||||
} else if n.Level >= outputThreshold && n.Level < logThreshold {
|
||||
n.Handle = OutHandle
|
||||
} else {
|
||||
n.Handle = LogHandle
|
||||
}
|
||||
}
|
||||
|
||||
for _, n := range NotePads {
|
||||
n.Handle = io.MultiWriter(n.Handle, &countingWriter{n.incr})
|
||||
*n.Logger = log.New(n.Handle, n.Prefix, logFlags)
|
||||
}
|
||||
|
||||
LOG = log.New(LogHandle,
|
||||
"LOG: ",
|
||||
logFlags)
|
||||
}
|
||||
|
||||
// Set the log Flags (Available flag: DATE, TIME, SFILE, LFILE and MSEC)
|
||||
func SetLogFlag(flags int) {
|
||||
logFlags = flags
|
||||
initialize()
|
||||
}
|
||||
|
||||
// Level returns the current global log threshold.
|
||||
func LogThreshold() Level {
|
||||
return logThreshold
|
||||
}
|
||||
|
||||
// Level returns the current global output threshold.
|
||||
func StdoutThreshold() Level {
|
||||
return outputThreshold
|
||||
}
|
||||
|
||||
// Ensures that the level provided is within the bounds of available levels
|
||||
func levelCheck(level Level) Level {
|
||||
switch {
|
||||
case level <= LevelTrace:
|
||||
return LevelTrace
|
||||
case level >= LevelFatal:
|
||||
return LevelFatal
|
||||
default:
|
||||
return level
|
||||
}
|
||||
}
|
||||
|
||||
// Establishes a threshold where anything matching or above will be logged
|
||||
func SetLogThreshold(level Level) {
|
||||
logThreshold = levelCheck(level)
|
||||
initialize()
|
||||
}
|
||||
|
||||
// Establishes a threshold where anything matching or above will be output
|
||||
func SetStdoutThreshold(level Level) {
|
||||
outputThreshold = levelCheck(level)
|
||||
initialize()
|
||||
}
|
||||
|
||||
// Conveniently Sets the Log Handle to a io.writer created for the file behind the given filepath
|
||||
// Will only append to this file
|
||||
func SetLogFile(path string) {
|
||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
CRITICAL.Println("Failed to open log file:", path, err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
INFO.Println("Logging to", file.Name())
|
||||
|
||||
LogHandle = file
|
||||
initialize()
|
||||
}
|
||||
|
||||
// Conveniently Creates a temporary file and sets the Log Handle to a io.writer created for it
|
||||
func UseTempLogFile(prefix string) {
|
||||
file, err := ioutil.TempFile(os.TempDir(), prefix)
|
||||
if err != nil {
|
||||
CRITICAL.Println(err)
|
||||
}
|
||||
|
||||
INFO.Println("Logging to", file.Name())
|
||||
|
||||
LogHandle = file
|
||||
initialize()
|
||||
}
|
||||
|
||||
// LogCountForLevel returns the number of log invocations for a given level.
|
||||
func LogCountForLevel(l Level) uint64 {
|
||||
for _, np := range NotePads {
|
||||
if np.Level == l {
|
||||
return np.getCount()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// LogCountForLevelsGreaterThanorEqualTo returns the number of log invocations
|
||||
// greater than or equal to a given level threshold.
|
||||
func LogCountForLevelsGreaterThanorEqualTo(threshold Level) uint64 {
|
||||
var cnt uint64
|
||||
for _, np := range NotePads {
|
||||
if np.Level >= threshold {
|
||||
cnt += np.getCount()
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
// ResetLogCounters resets the invocation counters for all levels.
|
||||
func ResetLogCounters() {
|
||||
for _, np := range NotePads {
|
||||
np.resetCounter()
|
||||
}
|
||||
}
|
||||
|
||||
// Disables logging for the entire JWW system
|
||||
func DiscardLogging() {
|
||||
LogHandle = ioutil.Discard
|
||||
initialize()
|
||||
}
|
||||
|
||||
// Feedback is special. It writes plainly to the output while
|
||||
// logging with the standard extra information (date, file, etc)
|
||||
// Only Println and Printf are currently provided for this
|
||||
func (fb *Feedback) Println(v ...interface{}) {
|
||||
s := fmt.Sprintln(v...)
|
||||
fmt.Print(s)
|
||||
LOG.Output(2, s)
|
||||
}
|
||||
|
||||
// Feedback is special. It writes plainly to the output while
|
||||
// logging with the standard extra information (date, file, etc)
|
||||
// Only Println and Printf are currently provided for this
|
||||
func (fb *Feedback) Printf(format string, v ...interface{}) {
|
||||
s := fmt.Sprintf(format, v...)
|
||||
fmt.Print(s)
|
||||
LOG.Output(2, s)
|
||||
}
|
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
@ -742,7 +742,7 @@ github.com/spf13/cast
|
||||
# github.com/spf13/cobra v0.0.4 => github.com/spf13/cobra v0.0.4
|
||||
github.com/spf13/cobra
|
||||
github.com/spf13/cobra/doc
|
||||
# github.com/spf13/jwalterweatherman v0.0.0-20160311093646-33c24e77fb80 => github.com/spf13/jwalterweatherman v0.0.0-20160311093646-33c24e77fb80
|
||||
# github.com/spf13/jwalterweatherman v1.1.0 => github.com/spf13/jwalterweatherman v1.1.0
|
||||
github.com/spf13/jwalterweatherman
|
||||
# github.com/spf13/pflag v1.0.3 => github.com/spf13/pflag v1.0.1
|
||||
github.com/spf13/pflag
|
||||
|
Loading…
Reference in New Issue
Block a user