Checking in vendor folder for ease of using go get.
This commit is contained in:
parent
7a1251853b
commit
cdb4b5a1d0
3554 changed files with 1270116 additions and 0 deletions
22
vendor/github.com/spf13/jwalterweatherman/.gitignore
generated
vendored
Normal file
22
vendor/github.com/spf13/jwalterweatherman/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
21
vendor/github.com/spf13/jwalterweatherman/LICENSE
generated
vendored
Normal file
21
vendor/github.com/spf13/jwalterweatherman/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Steve Francia
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
148
vendor/github.com/spf13/jwalterweatherman/README.md
generated
vendored
Normal file
148
vendor/github.com/spf13/jwalterweatherman/README.md
generated
vendored
Normal file
|
@ -0,0 +1,148 @@
|
|||
jWalterWeatherman
|
||||
=================
|
||||
|
||||
Seamless printing to the terminal (stdout) and logging to a io.Writer
|
||||
(file) that’s as easy to use as fmt.Println.
|
||||
|
||||

|
||||
Graphic by [JonnyEtc](http://jonnyetc.deviantart.com/art/And-That-s-Why-You-Always-Leave-a-Note-315311422)
|
||||
|
||||
JWW is primarily a wrapper around the excellent standard log library. It
|
||||
provides a few advantages over using the standard log library alone.
|
||||
|
||||
1. Ready to go out of the box.
|
||||
2. One library for both printing to the terminal and logging (to files).
|
||||
3. Really easy to log to either a temp file or a file you specify.
|
||||
|
||||
|
||||
I really wanted a very straightforward library that could seamlessly do
|
||||
the following things.
|
||||
|
||||
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
|
||||
4. Provide an easy mechanism (like fmt.Println) to print info to the user
|
||||
which can be easily logged as well
|
||||
5. Due to 2 & 3 provide easy verbose mode for output and logs
|
||||
6. Not have any unnecessary initialization cruft. Just use it.
|
||||
|
||||
# Usage
|
||||
|
||||
## Step 1. Use it
|
||||
Put calls throughout your source based on type of feedback.
|
||||
No initialization or setup needs to happen. Just start calling things.
|
||||
|
||||
Available Loggers are:
|
||||
|
||||
* TRACE
|
||||
* DEBUG
|
||||
* INFO
|
||||
* WARN
|
||||
* ERROR
|
||||
* CRITICAL
|
||||
* FATAL
|
||||
|
||||
These each are loggers based on the log standard library and follow the
|
||||
standard usage. Eg.
|
||||
|
||||
```go
|
||||
import (
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
)
|
||||
|
||||
...
|
||||
|
||||
if err != nil {
|
||||
|
||||
// This is a pretty serious error and the user should know about
|
||||
// it. It will be printed to the terminal as well as logged under the
|
||||
// default thresholds.
|
||||
|
||||
jww.ERROR.Println(err)
|
||||
}
|
||||
|
||||
if err2 != nil {
|
||||
// This error isn’t going to materially change the behavior of the
|
||||
// application, but it’s something that may not be what the user
|
||||
// expects. Under the default thresholds, Warn will be logged, but
|
||||
// not printed to the terminal.
|
||||
|
||||
jww.WARN.Println(err2)
|
||||
}
|
||||
|
||||
// Information that’s relevant to what’s happening, but not very
|
||||
// important for the user. Under the default thresholds this will be
|
||||
// discarded.
|
||||
|
||||
jww.INFO.Printf("information %q", response)
|
||||
|
||||
```
|
||||
|
||||
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
|
||||
are probably correct. Just because there are seven levels doesn’t mean
|
||||
that you should be using all 7 levels. Pick the right set for your needs.
|
||||
Remember they only have to mean something to your project.
|
||||
|
||||
## Step 2. Optionally configure JWW
|
||||
|
||||
Under the default thresholds :
|
||||
|
||||
* Debug, Trace & Info goto /dev/null
|
||||
* Warn and above is logged (when a log file/io.Writer is provided)
|
||||
* Error and above is printed to the terminal (stdout)
|
||||
|
||||
### Changing the thresholds
|
||||
|
||||
The threshold can be changed at any time, but will only affect calls that
|
||||
execute after the change was made.
|
||||
|
||||
This is very useful if your application has a verbose mode. Of course you
|
||||
can decide what verbose means to you or even have multiple levels of
|
||||
verbosity.
|
||||
|
||||
|
||||
```go
|
||||
import (
|
||||
jww "github.com/spf13/jwalterweatherman"
|
||||
)
|
||||
|
||||
if Verbose {
|
||||
jww.SetLogThreshold(jww.LevelTrace)
|
||||
jww.SetStdoutThreshold(jww.LevelInfo)
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
|
||||
### Setting a log file
|
||||
|
||||
JWW can log to any `io.Writer`:
|
||||
|
||||
|
||||
```go
|
||||
|
||||
jww.SetLogOutput(customWriter)
|
||||
|
||||
```
|
||||
|
||||
|
||||
# More information
|
||||
|
||||
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](https://gohugo.io). If you are looking
|
||||
for a static website engine that’s super fast please checkout Hugo.
|
113
vendor/github.com/spf13/jwalterweatherman/default_notepad.go
generated
vendored
Normal file
113
vendor/github.com/spf13/jwalterweatherman/default_notepad.go
generated
vendored
Normal file
|
@ -0,0 +1,113 @@
|
|||
// 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()
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// LogCountForLevel returns the number of log invocations for a given threshold.
|
||||
func LogCountForLevel(l Threshold) uint64 {
|
||||
return defaultNotepad.LogCountForLevel(l)
|
||||
}
|
||||
|
||||
// LogCountForLevelsGreaterThanorEqualTo returns the number of log invocations
|
||||
// greater than or equal to a given threshold.
|
||||
func LogCountForLevelsGreaterThanorEqualTo(threshold Threshold) uint64 {
|
||||
return defaultNotepad.LogCountForLevelsGreaterThanorEqualTo(threshold)
|
||||
}
|
||||
|
||||
// ResetLogCounters resets the invocation counters for all levels.
|
||||
func ResetLogCounters() {
|
||||
defaultNotepad.ResetLogCounters()
|
||||
}
|
102
vendor/github.com/spf13/jwalterweatherman/default_notepad_test.go
generated
vendored
Normal file
102
vendor/github.com/spf13/jwalterweatherman/default_notepad_test.go
generated
vendored
Normal file
|
@ -0,0 +1,102 @@
|
|||
// 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 (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestThresholds(t *testing.T) {
|
||||
SetStdoutThreshold(LevelError)
|
||||
require.Equal(t, StdoutThreshold(), LevelError)
|
||||
SetLogThreshold(LevelCritical)
|
||||
require.Equal(t, LogThreshold(), LevelCritical)
|
||||
require.NotEqual(t, StdoutThreshold(), LevelCritical)
|
||||
SetStdoutThreshold(LevelWarn)
|
||||
require.Equal(t, StdoutThreshold(), LevelWarn)
|
||||
}
|
||||
|
||||
func TestDefaultLogging(t *testing.T) {
|
||||
var outputBuf, logBuf bytes.Buffer
|
||||
|
||||
defaultNotepad.logHandle = &logBuf
|
||||
defaultNotepad.outHandle = &outputBuf
|
||||
|
||||
SetLogThreshold(LevelWarn)
|
||||
SetStdoutThreshold(LevelError)
|
||||
|
||||
FATAL.Println("fatal err")
|
||||
CRITICAL.Println("critical err")
|
||||
ERROR.Println("an error")
|
||||
WARN.Println("a warning")
|
||||
INFO.Println("information")
|
||||
DEBUG.Println("debugging info")
|
||||
TRACE.Println("trace")
|
||||
|
||||
require.Contains(t, logBuf.String(), "fatal err")
|
||||
require.Contains(t, logBuf.String(), "critical err")
|
||||
require.Contains(t, logBuf.String(), "an error")
|
||||
require.Contains(t, logBuf.String(), "a warning")
|
||||
require.NotContains(t, logBuf.String(), "information")
|
||||
require.NotContains(t, logBuf.String(), "debugging info")
|
||||
require.NotContains(t, logBuf.String(), "trace")
|
||||
|
||||
require.Contains(t, outputBuf.String(), "fatal err")
|
||||
require.Contains(t, outputBuf.String(), "critical err")
|
||||
require.Contains(t, outputBuf.String(), "an error")
|
||||
require.NotContains(t, outputBuf.String(), "a warning")
|
||||
require.NotContains(t, outputBuf.String(), "information")
|
||||
require.NotContains(t, outputBuf.String(), "debugging info")
|
||||
require.NotContains(t, outputBuf.String(), "trace")
|
||||
}
|
||||
|
||||
func TestLogCounter(t *testing.T) {
|
||||
defaultNotepad.logHandle = ioutil.Discard
|
||||
defaultNotepad.outHandle = ioutil.Discard
|
||||
|
||||
SetLogThreshold(LevelTrace)
|
||||
SetStdoutThreshold(LevelTrace)
|
||||
|
||||
FATAL.Println("fatal err")
|
||||
CRITICAL.Println("critical err")
|
||||
WARN.Println("a warning")
|
||||
WARN.Println("another warning")
|
||||
INFO.Println("information")
|
||||
DEBUG.Println("debugging info")
|
||||
TRACE.Println("trace")
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for j := 0; j < 10; j++ {
|
||||
ERROR.Println("error", j)
|
||||
// check for data races
|
||||
require.True(t, LogCountForLevel(LevelError) > uint64(j))
|
||||
require.True(t, LogCountForLevelsGreaterThanorEqualTo(LevelError) > uint64(j))
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
require.Equal(t, uint64(1), LogCountForLevel(LevelFatal))
|
||||
require.Equal(t, uint64(1), LogCountForLevel(LevelCritical))
|
||||
require.Equal(t, uint64(2), LogCountForLevel(LevelWarn))
|
||||
require.Equal(t, uint64(1), LogCountForLevel(LevelInfo))
|
||||
require.Equal(t, uint64(1), LogCountForLevel(LevelDebug))
|
||||
require.Equal(t, uint64(1), LogCountForLevel(LevelTrace))
|
||||
require.Equal(t, uint64(100), LogCountForLevel(LevelError))
|
||||
require.Equal(t, uint64(102), LogCountForLevelsGreaterThanorEqualTo(LevelError))
|
||||
}
|
1
vendor/github.com/spf13/jwalterweatherman/go.mod
generated
vendored
Normal file
1
vendor/github.com/spf13/jwalterweatherman/go.mod
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
module github.com/spf13/jwalterweatherman
|
55
vendor/github.com/spf13/jwalterweatherman/log_counter.go
generated
vendored
Normal file
55
vendor/github.com/spf13/jwalterweatherman/log_counter.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
// 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 (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type logCounter struct {
|
||||
counter uint64
|
||||
}
|
||||
|
||||
func (c *logCounter) incr() {
|
||||
atomic.AddUint64(&c.counter, 1)
|
||||
}
|
||||
|
||||
func (c *logCounter) resetCounter() {
|
||||
atomic.StoreUint64(&c.counter, 0)
|
||||
}
|
||||
|
||||
func (c *logCounter) getCount() uint64 {
|
||||
return atomic.LoadUint64(&c.counter)
|
||||
}
|
||||
|
||||
func (c *logCounter) Write(p []byte) (n int, err error) {
|
||||
c.incr()
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// LogCountForLevel returns the number of log invocations for a given threshold.
|
||||
func (n *Notepad) LogCountForLevel(l Threshold) uint64 {
|
||||
return n.logCounters[l].getCount()
|
||||
}
|
||||
|
||||
// LogCountForLevelsGreaterThanorEqualTo returns the number of log invocations
|
||||
// greater than or equal to a given threshold.
|
||||
func (n *Notepad) LogCountForLevelsGreaterThanorEqualTo(threshold Threshold) uint64 {
|
||||
var cnt uint64
|
||||
|
||||
for i := int(threshold); i < len(n.logCounters); i++ {
|
||||
cnt += n.LogCountForLevel(Threshold(i))
|
||||
}
|
||||
|
||||
return cnt
|
||||
}
|
||||
|
||||
// ResetLogCounters resets the invocation counters for all levels.
|
||||
func (n *Notepad) ResetLogCounters() {
|
||||
for _, np := range n.logCounters {
|
||||
np.resetCounter()
|
||||
}
|
||||
}
|
194
vendor/github.com/spf13/jwalterweatherman/notepad.go
generated
vendored
Normal file
194
vendor/github.com/spf13/jwalterweatherman/notepad.go
generated
vendored
Normal file
|
@ -0,0 +1,194 @@
|
|||
// 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"
|
||||
"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
|
||||
|
||||
// One per Threshold
|
||||
logCounters [7]*logCounter
|
||||
}
|
||||
|
||||
// NewNotepad create a new notepad.
|
||||
func NewNotepad(outThreshold Threshold, logThreshold Threshold, outHandle, logHandle io.Writer, prefix string, flags int) *Notepad {
|
||||
n := &Notepad{}
|
||||
|
||||
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)
|
||||
counter := &logCounter{}
|
||||
n.logCounters[t] = counter
|
||||
prefix := n.prefix + threshold.String() + " "
|
||||
|
||||
switch {
|
||||
case threshold >= n.logThreshold && threshold >= n.stdoutThreshold:
|
||||
*logger = log.New(io.MultiWriter(counter, logAndOut), prefix, n.flags)
|
||||
|
||||
case threshold >= n.logThreshold:
|
||||
*logger = log.New(io.MultiWriter(counter, n.logHandle), prefix, n.flags)
|
||||
|
||||
case threshold >= n.stdoutThreshold:
|
||||
*logger = log.New(io.MultiWriter(counter, n.outHandle), prefix, n.flags)
|
||||
|
||||
default:
|
||||
// counter doesn't care about prefix and flags, so don't use them
|
||||
// for performance.
|
||||
*logger = log.New(counter, "", 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
50
vendor/github.com/spf13/jwalterweatherman/notepad_test.go
generated
vendored
Normal file
50
vendor/github.com/spf13/jwalterweatherman/notepad_test.go
generated
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
// 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 (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNotepad(t *testing.T) {
|
||||
var logHandle, outHandle bytes.Buffer
|
||||
|
||||
n := NewNotepad(LevelCritical, LevelError, &outHandle, &logHandle, "TestNotePad", 0)
|
||||
|
||||
require.Equal(t, LevelCritical, n.GetStdoutThreshold())
|
||||
require.Equal(t, LevelError, n.GetLogThreshold())
|
||||
|
||||
n.DEBUG.Println("Some debug")
|
||||
n.ERROR.Println("Some error")
|
||||
n.CRITICAL.Println("Some critical error")
|
||||
|
||||
require.Contains(t, logHandle.String(), "[TestNotePad] ERROR Some error")
|
||||
require.NotContains(t, logHandle.String(), "Some debug")
|
||||
require.NotContains(t, outHandle.String(), "Some error")
|
||||
require.Contains(t, outHandle.String(), "Some critical error")
|
||||
|
||||
require.Equal(t, n.LogCountForLevel(LevelError), uint64(1))
|
||||
require.Equal(t, n.LogCountForLevel(LevelDebug), uint64(1))
|
||||
require.Equal(t, n.LogCountForLevel(LevelTrace), uint64(0))
|
||||
}
|
||||
|
||||
func TestThresholdString(t *testing.T) {
|
||||
require.Equal(t, LevelError.String(), "ERROR")
|
||||
require.Equal(t, LevelTrace.String(), "TRACE")
|
||||
}
|
||||
|
||||
func BenchmarkLogPrintOnlyToCounter(b *testing.B) {
|
||||
var logHandle, outHandle bytes.Buffer
|
||||
n := NewNotepad(LevelCritical, LevelCritical, &outHandle, &logHandle, "TestNotePad", 0)
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
n.INFO.Print("Test")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue