all: Remove seelog logger.
The btclog package has been changed to defining its own logging interface (rather than seelog's) and provides a default implementation for callers to use. There are two primary advantages to the new logger implementation. First, all log messages are created before the call returns. Compared to seelog, this prevents data races when mutable variables are logged. Second, the new logger does not implement any kind of artifical rate limiting (what seelog refers to as "adaptive logging"). Log messages are outputted as soon as possible and the application will appear to perform much better when watching standard output. Because log rotation is not a feature of the btclog logging implementation, it is handled by the main package by importing a file rotation package that provides an io.Reader interface for creating output to a rotating file output. The rotator has been configured with the same defaults that btcd previously used in the seelog config (10MB file limits with maximum of 3 rolls) but now compresses newly created roll files. Due to the high compressibility of log text, the compressed files typically reduce to around 15-30% of the original 10MB file.
This commit is contained in:
parent
1bdb713285
commit
a6965d493f
20 changed files with 126 additions and 529 deletions
|
@ -5,9 +5,6 @@
|
||||||
package blockchain
|
package blockchain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btclog"
|
"github.com/btcsuite/btclog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,37 +19,12 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableLog disables all library log output. Logging output is disabled
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
// by default until either UseLogger or SetLogWriter are called.
|
// by default until UseLogger is called.
|
||||||
func DisableLog() {
|
func DisableLog() {
|
||||||
log = btclog.Disabled
|
log = btclog.Disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// UseLogger uses a specified Logger to output package logging info.
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
// This should be used in preference to SetLogWriter if the caller is also
|
|
||||||
// using btclog.
|
|
||||||
func UseLogger(logger btclog.Logger) {
|
func UseLogger(logger btclog.Logger) {
|
||||||
log = logger
|
log = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogWriter uses a specified io.Writer to output package logging info.
|
|
||||||
// This allows a caller to direct package logging output without needing a
|
|
||||||
// dependency on seelog. If the caller is also using btclog, UseLogger should
|
|
||||||
// be used instead.
|
|
||||||
func SetLogWriter(w io.Writer, level string) error {
|
|
||||||
if w == nil {
|
|
||||||
return errors.New("nil writer")
|
|
||||||
}
|
|
||||||
|
|
||||||
lvl, ok := btclog.LogLevelFromString(level)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("invalid log level")
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := btclog.NewLoggerFromWriter(w, lvl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
UseLogger(l)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
6
btcd.go
6
btcd.go
|
@ -39,7 +39,11 @@ func btcdMain(serverChan chan<- *server) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cfg = tcfg
|
cfg = tcfg
|
||||||
defer backendLog.Flush()
|
defer func() {
|
||||||
|
if logRotator != nil {
|
||||||
|
logRotator.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Get a channel that will be closed when a shutdown signal has been
|
// Get a channel that will be closed when a shutdown signal has been
|
||||||
// triggered either from an OS signal such as SIGINT (Ctrl+C) or from
|
// triggered either from an OS signal such as SIGINT (Ctrl+C) or from
|
||||||
|
|
|
@ -69,12 +69,12 @@ func realMain() error {
|
||||||
cfg = tcfg
|
cfg = tcfg
|
||||||
|
|
||||||
// Setup logging.
|
// Setup logging.
|
||||||
backendLogger := btclog.NewDefaultBackendLogger()
|
backendLogger := btclog.NewBackend(os.Stdout)
|
||||||
defer backendLogger.Flush()
|
defer os.Stdout.Sync()
|
||||||
log = btclog.NewSubsystemLogger(backendLogger, "")
|
log = backendLogger.Logger("MAIN")
|
||||||
database.UseLogger(btclog.NewSubsystemLogger(backendLogger, "BCDB: "))
|
database.UseLogger(backendLogger.Logger("BCDB"))
|
||||||
blockchain.UseLogger(btclog.NewSubsystemLogger(backendLogger, "CHAN: "))
|
blockchain.UseLogger(backendLogger.Logger("CHAN"))
|
||||||
indexers.UseLogger(btclog.NewSubsystemLogger(backendLogger, "INDX: "))
|
indexers.UseLogger(backendLogger.Logger("INDX"))
|
||||||
|
|
||||||
// Load the block database.
|
// Load the block database.
|
||||||
db, err := loadBlockDB()
|
db, err := loadBlockDB()
|
||||||
|
|
|
@ -579,9 +579,9 @@ func loadConfig() (*config, []string, error) {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize logging at the default logging level.
|
// Initialize log rotation. After log rotation has been initialized, the
|
||||||
initSeelogLogger(filepath.Join(cfg.LogDir, defaultLogFilename))
|
// logger variables may be used.
|
||||||
setLogLevels(defaultLogLevel)
|
initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename))
|
||||||
|
|
||||||
// Parse, validate, and set debug log level(s).
|
// Parse, validate, and set debug log level(s).
|
||||||
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
|
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
|
||||||
|
|
|
@ -5,15 +5,12 @@
|
||||||
package connmgr
|
package connmgr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btclog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -85,15 +82,6 @@ func TestNewConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestUseLogger tests that a logger can be passed to UseLogger
|
|
||||||
func TestUseLogger(t *testing.T) {
|
|
||||||
l, err := btclog.NewLoggerFromWriter(bytes.NewBuffer(nil), btclog.InfoLvl)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
UseLogger(l)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestStartStop tests that the connection manager starts and stops as
|
// TestStartStop tests that the connection manager starts and stops as
|
||||||
// expected.
|
// expected.
|
||||||
func TestStartStop(t *testing.T) {
|
func TestStartStop(t *testing.T) {
|
||||||
|
|
|
@ -61,11 +61,11 @@ func loadBlockDB() (database.DB, error) {
|
||||||
// around the fact that deferred functions do not run when os.Exit() is called.
|
// around the fact that deferred functions do not run when os.Exit() is called.
|
||||||
func realMain() error {
|
func realMain() error {
|
||||||
// Setup logging.
|
// Setup logging.
|
||||||
backendLogger := btclog.NewDefaultBackendLogger()
|
backendLogger := btclog.NewBackend(os.Stdout)
|
||||||
defer backendLogger.Flush()
|
defer os.Stdout.Sync()
|
||||||
log = btclog.NewSubsystemLogger(backendLogger, "")
|
log = backendLogger.Logger("MAIN")
|
||||||
dbLog := btclog.NewSubsystemLogger(backendLogger, "BCDB: ")
|
dbLog := backendLogger.Logger("BCDB")
|
||||||
dbLog.SetLevel(btclog.DebugLvl)
|
dbLog.SetLevel(btclog.LevelDebug)
|
||||||
database.UseLogger(dbLog)
|
database.UseLogger(dbLog)
|
||||||
|
|
||||||
// Setup the parser options and commands.
|
// Setup the parser options and commands.
|
||||||
|
|
|
@ -676,7 +676,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
|
||||||
}
|
}
|
||||||
for ; wc.curFileNum > oldBlockFileNum; wc.curFileNum-- {
|
for ; wc.curFileNum > oldBlockFileNum; wc.curFileNum-- {
|
||||||
if err := s.deleteFileFunc(wc.curFileNum); err != nil {
|
if err := s.deleteFileFunc(wc.curFileNum); err != nil {
|
||||||
_ = log.Warnf("ROLLBACK: Failed to delete block file "+
|
log.Warnf("ROLLBACK: Failed to delete block file "+
|
||||||
"number %d: %v", wc.curFileNum, err)
|
"number %d: %v", wc.curFileNum, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -688,7 +688,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
|
||||||
obf, err := s.openWriteFileFunc(wc.curFileNum)
|
obf, err := s.openWriteFileFunc(wc.curFileNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wc.curFile.Unlock()
|
wc.curFile.Unlock()
|
||||||
_ = log.Warnf("ROLLBACK: %v", err)
|
log.Warnf("ROLLBACK: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
wc.curFile.file = obf
|
wc.curFile.file = obf
|
||||||
|
@ -697,7 +697,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
|
||||||
// Truncate the to the provided rollback offset.
|
// Truncate the to the provided rollback offset.
|
||||||
if err := wc.curFile.file.Truncate(int64(oldBlockOffset)); err != nil {
|
if err := wc.curFile.file.Truncate(int64(oldBlockOffset)); err != nil {
|
||||||
wc.curFile.Unlock()
|
wc.curFile.Unlock()
|
||||||
_ = log.Warnf("ROLLBACK: Failed to truncate file %d: %v",
|
log.Warnf("ROLLBACK: Failed to truncate file %d: %v",
|
||||||
wc.curFileNum, err)
|
wc.curFileNum, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -706,7 +706,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
|
||||||
err := wc.curFile.file.Sync()
|
err := wc.curFile.file.Sync()
|
||||||
wc.curFile.Unlock()
|
wc.curFile.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = log.Warnf("ROLLBACK: Failed to sync file %d: %v",
|
log.Warnf("ROLLBACK: Failed to sync file %d: %v",
|
||||||
wc.curFileNum, err)
|
wc.curFileNum, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,7 +109,7 @@ func reconcileDB(pdb *db, create bool) (database.DB, error) {
|
||||||
str := fmt.Sprintf("metadata claims file %d, offset %d, but "+
|
str := fmt.Sprintf("metadata claims file %d, offset %d, but "+
|
||||||
"block data is at file %d, offset %d", curFileNum,
|
"block data is at file %d, offset %d", curFileNum,
|
||||||
curOffset, wc.curFileNum, wc.curOffset)
|
curOffset, wc.curFileNum, wc.curOffset)
|
||||||
_ = log.Warnf("***Database corruption detected***: %v", str)
|
log.Warnf("***Database corruption detected***: %v", str)
|
||||||
return nil, makeDbErr(database.ErrCorruption, str, nil)
|
return nil, makeDbErr(database.ErrCorruption, str, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,6 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btclog"
|
"github.com/btcsuite/btclog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,14 +19,12 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableLog disables all library log output. Logging output is disabled
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
// by default until either UseLogger or SetLogWriter are called.
|
// by default until UseLogger is called.
|
||||||
func DisableLog() {
|
func DisableLog() {
|
||||||
log = btclog.Disabled
|
log = btclog.Disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// UseLogger uses a specified Logger to output package logging info.
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
// This should be used in preference to SetLogWriter if the caller is also
|
|
||||||
// using btclog.
|
|
||||||
func UseLogger(logger btclog.Logger) {
|
func UseLogger(logger btclog.Logger) {
|
||||||
log = logger
|
log = logger
|
||||||
|
|
||||||
|
@ -40,26 +35,3 @@ func UseLogger(logger btclog.Logger) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogWriter uses a specified io.Writer to output package logging info.
|
|
||||||
// This allows a caller to direct package logging output without needing a
|
|
||||||
// dependency on seelog. If the caller is also using btclog, UseLogger should
|
|
||||||
// be used instead.
|
|
||||||
func SetLogWriter(w io.Writer, level string) error {
|
|
||||||
if w == nil {
|
|
||||||
return errors.New("nil writer")
|
|
||||||
}
|
|
||||||
|
|
||||||
lvl, ok := btclog.LogLevelFromString(level)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("invalid log level")
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := btclog.NewLoggerFromWriter(w, lvl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
UseLogger(l)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,67 +0,0 @@
|
||||||
// Copyright (c) 2015-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package database_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/database"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestSetLogWriter ensures the
|
|
||||||
func TestSetLogWriter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
w io.Writer
|
|
||||||
level string
|
|
||||||
expected error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "nil writer",
|
|
||||||
w: nil,
|
|
||||||
level: "trace",
|
|
||||||
expected: errors.New("nil writer"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid log level",
|
|
||||||
w: os.Stdout,
|
|
||||||
level: "wrong",
|
|
||||||
expected: errors.New("invalid log level"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "use off level",
|
|
||||||
w: os.Stdout,
|
|
||||||
level: "off",
|
|
||||||
expected: errors.New("min level can't be greater than max. Got min: 6, max: 5"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "pass",
|
|
||||||
w: os.Stdout,
|
|
||||||
level: "debug",
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
err := database.SetLogWriter(test.w, test.level)
|
|
||||||
if err != nil {
|
|
||||||
if err.Error() != test.expected.Error() {
|
|
||||||
t.Errorf("SetLogWriter #%d (%s) wrong result\n"+
|
|
||||||
"got: %v\nwant: %v", i, test.name, err,
|
|
||||||
test.expected)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if test.expected != nil {
|
|
||||||
t.Errorf("SetLogWriter #%d (%s) wrong result\n"+
|
|
||||||
"got: %v\nwant: %v", i, test.name, err,
|
|
||||||
test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
14
glide.lock
generated
14
glide.lock
generated
|
@ -1,10 +1,10 @@
|
||||||
hash: 4a7317ba20a3d8dd1ea253b86d86231d97cf85c16ca368471a50e8c42771daf0
|
hash: 9e111ebb6989c8b23db0ef5c0523e8dde1ccaa964fa7a42faccbb7a6a60b2860
|
||||||
updated: 2017-05-08T23:05:30.5569509-05:00
|
updated: 2017-06-19T16:45:22.5258215-04:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/btcsuite/btclog
|
- name: github.com/btcsuite/btclog
|
||||||
version: 73889fb79bd687870312b6e40effcecffbd57d30
|
version: 96c2a91a67da03552a5e6554fe3ccbfbc7f860be
|
||||||
- name: github.com/btcsuite/btcrpcclient
|
- name: github.com/btcsuite/btcrpcclient
|
||||||
version: abcdfb702a7ca67e4a32c10be380c63216bd69fe
|
version: 45b9cb481d2aead4e80aab32d7aa86db386430ab
|
||||||
- name: github.com/btcsuite/btcutil
|
- name: github.com/btcsuite/btcutil
|
||||||
version: dcd4997b0664bcfd6ef48e4ae9da8396e08b1cd9
|
version: dcd4997b0664bcfd6ef48e4ae9da8396e08b1cd9
|
||||||
subpackages:
|
subpackages:
|
||||||
|
@ -30,8 +30,6 @@ imports:
|
||||||
- leveldb/storage
|
- leveldb/storage
|
||||||
- leveldb/table
|
- leveldb/table
|
||||||
- leveldb/util
|
- leveldb/util
|
||||||
- name: github.com/btcsuite/seelog
|
|
||||||
version: 313961b101eb55f65ae0f03ddd4e322731763b6c
|
|
||||||
- name: github.com/btcsuite/snappy-go
|
- name: github.com/btcsuite/snappy-go
|
||||||
version: 0bdef8d067237991ddaa1bb6072a740bc40601ba
|
version: 0bdef8d067237991ddaa1bb6072a740bc40601ba
|
||||||
- name: github.com/btcsuite/websocket
|
- name: github.com/btcsuite/websocket
|
||||||
|
@ -50,6 +48,10 @@ imports:
|
||||||
- spew
|
- spew
|
||||||
- name: github.com/jessevdk/go-flags
|
- name: github.com/jessevdk/go-flags
|
||||||
version: 1679536dcc895411a9f5848d9a0250be7856448c
|
version: 1679536dcc895411a9f5848d9a0250be7856448c
|
||||||
|
- name: github.com/jrick/logrotate
|
||||||
|
version: 4ed05ed86ef17d10ff99cce77481e0fcf6f2c7b0
|
||||||
|
subpackages:
|
||||||
|
- rotator
|
||||||
- name: golang.org/x/crypto
|
- name: golang.org/x/crypto
|
||||||
version: 122d919ec1efcfb58483215da23f815853e24b81
|
version: 122d919ec1efcfb58483215da23f815853e24b81
|
||||||
subpackages:
|
subpackages:
|
||||||
|
|
|
@ -21,7 +21,6 @@ import:
|
||||||
- leveldb/iterator
|
- leveldb/iterator
|
||||||
- leveldb/opt
|
- leveldb/opt
|
||||||
- leveldb/util
|
- leveldb/util
|
||||||
- package: github.com/btcsuite/seelog
|
|
||||||
- package: github.com/btcsuite/websocket
|
- package: github.com/btcsuite/websocket
|
||||||
- package: github.com/btcsuite/winsvc
|
- package: github.com/btcsuite/winsvc
|
||||||
subpackages:
|
subpackages:
|
||||||
|
@ -33,3 +32,4 @@ import:
|
||||||
- spew
|
- spew
|
||||||
- package: github.com/jessevdk/go-flags
|
- package: github.com/jessevdk/go-flags
|
||||||
version: 1679536dcc895411a9f5848d9a0250be7856448c
|
version: 1679536dcc895411a9f5848d9a0250be7856448c
|
||||||
|
- package: github.com/jrick/logrotate
|
||||||
|
|
202
log.go
202
log.go
|
@ -1,4 +1,5 @@
|
||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
// Copyright (c) 2013-2017 The btcsuite developers
|
||||||
|
// Copyright (c) 2017 The Decred developers
|
||||||
// Use of this source code is governed by an ISC
|
// Use of this source code is governed by an ISC
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
@ -6,7 +7,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/addrmgr"
|
"github.com/btcsuite/btcd/addrmgr"
|
||||||
"github.com/btcsuite/btcd/blockchain"
|
"github.com/btcsuite/btcd/blockchain"
|
||||||
|
@ -19,32 +22,72 @@ import (
|
||||||
"github.com/btcsuite/btcd/peer"
|
"github.com/btcsuite/btcd/peer"
|
||||||
"github.com/btcsuite/btcd/txscript"
|
"github.com/btcsuite/btcd/txscript"
|
||||||
"github.com/btcsuite/btclog"
|
"github.com/btcsuite/btclog"
|
||||||
"github.com/btcsuite/seelog"
|
"github.com/jrick/logrotate/rotator"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Loggers per subsystem. Note that backendLog is a seelog logger that all of
|
// logWriter implements an io.Writer that outputs to both standard output and
|
||||||
// the subsystem loggers route their messages to. When adding new subsystems,
|
// the write-end pipe of an initialized log rotator.
|
||||||
// add a reference here, to the subsystemLoggers map, and the useLogger
|
type logWriter struct{}
|
||||||
// function.
|
|
||||||
|
func (logWriter) Write(p []byte) (n int, err error) {
|
||||||
|
os.Stdout.Write(p)
|
||||||
|
logRotatorPipe.Write(p)
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loggers per subsystem. A single backend logger is created and all subsytem
|
||||||
|
// loggers created from it will write to the backend. When adding new
|
||||||
|
// subsystems, add the subsystem logger variable here and to the
|
||||||
|
// subsystemLoggers map.
|
||||||
|
//
|
||||||
|
// Loggers can not be used before the log rotator has been initialized with a
|
||||||
|
// log file. This must be performed early during application startup by calling
|
||||||
|
// initLogRotator.
|
||||||
var (
|
var (
|
||||||
backendLog = seelog.Disabled
|
// backendLog is the logging backend used to create all subsystem loggers.
|
||||||
adxrLog = btclog.Disabled
|
// The backend must not be used before the log rotator has been initialized,
|
||||||
amgrLog = btclog.Disabled
|
// or data races and/or nil pointer dereferences will occur.
|
||||||
cmgrLog = btclog.Disabled
|
backendLog = btclog.NewBackend(logWriter{})
|
||||||
bcdbLog = btclog.Disabled
|
|
||||||
bmgrLog = btclog.Disabled
|
// logRotator is one of the logging outputs. It should be closed on
|
||||||
btcdLog = btclog.Disabled
|
// application shutdown.
|
||||||
chanLog = btclog.Disabled
|
logRotator *rotator.Rotator
|
||||||
discLog = btclog.Disabled
|
|
||||||
indxLog = btclog.Disabled
|
// logRotatorPipe is the write-end pipe for writing to the log rotator. It
|
||||||
minrLog = btclog.Disabled
|
// is written to by the Write method of the logWriter type.
|
||||||
peerLog = btclog.Disabled
|
logRotatorPipe *io.PipeWriter
|
||||||
rpcsLog = btclog.Disabled
|
|
||||||
scrpLog = btclog.Disabled
|
adxrLog = backendLog.Logger("ADXR")
|
||||||
srvrLog = btclog.Disabled
|
amgrLog = backendLog.Logger("AMGR")
|
||||||
txmpLog = btclog.Disabled
|
cmgrLog = backendLog.Logger("CMGR")
|
||||||
|
bcdbLog = backendLog.Logger("BCDB")
|
||||||
|
bmgrLog = backendLog.Logger("BMGR")
|
||||||
|
btcdLog = backendLog.Logger("BTCD")
|
||||||
|
chanLog = backendLog.Logger("CHAN")
|
||||||
|
discLog = backendLog.Logger("DISC")
|
||||||
|
indxLog = backendLog.Logger("INDX")
|
||||||
|
minrLog = backendLog.Logger("MINR")
|
||||||
|
peerLog = backendLog.Logger("PEER")
|
||||||
|
rpcsLog = backendLog.Logger("RPCS")
|
||||||
|
scrpLog = backendLog.Logger("SCRP")
|
||||||
|
srvrLog = backendLog.Logger("SRVR")
|
||||||
|
txmpLog = backendLog.Logger("TXMP")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Initialize package-global logger variables.
|
||||||
|
func init() {
|
||||||
|
addrmgr.UseLogger(amgrLog)
|
||||||
|
connmgr.UseLogger(cmgrLog)
|
||||||
|
database.UseLogger(bcdbLog)
|
||||||
|
blockchain.UseLogger(chanLog)
|
||||||
|
indexers.UseLogger(indxLog)
|
||||||
|
mining.UseLogger(minrLog)
|
||||||
|
cpuminer.UseLogger(minrLog)
|
||||||
|
peer.UseLogger(peerLog)
|
||||||
|
txscript.UseLogger(scrpLog)
|
||||||
|
mempool.UseLogger(txmpLog)
|
||||||
|
}
|
||||||
|
|
||||||
// subsystemLoggers maps each subsystem identifier to its associated logger.
|
// subsystemLoggers maps each subsystem identifier to its associated logger.
|
||||||
var subsystemLoggers = map[string]btclog.Logger{
|
var subsystemLoggers = map[string]btclog.Logger{
|
||||||
"ADXR": adxrLog,
|
"ADXR": adxrLog,
|
||||||
|
@ -64,95 +107,27 @@ var subsystemLoggers = map[string]btclog.Logger{
|
||||||
"TXMP": txmpLog,
|
"TXMP": txmpLog,
|
||||||
}
|
}
|
||||||
|
|
||||||
// useLogger updates the logger references for subsystemID to logger. Invalid
|
// initLogRotator initializes the logging rotater to write logs to logFile and
|
||||||
// subsystems are ignored.
|
// create roll files in the same directory. It must be called before the
|
||||||
func useLogger(subsystemID string, logger btclog.Logger) {
|
// package-global log rotater variables are used.
|
||||||
if _, ok := subsystemLoggers[subsystemID]; !ok {
|
func initLogRotator(logFile string) {
|
||||||
return
|
logDir, _ := filepath.Split(logFile)
|
||||||
}
|
err := os.MkdirAll(logDir, 0700)
|
||||||
subsystemLoggers[subsystemID] = logger
|
|
||||||
|
|
||||||
switch subsystemID {
|
|
||||||
case "ADXR":
|
|
||||||
adxrLog = logger
|
|
||||||
|
|
||||||
case "AMGR":
|
|
||||||
amgrLog = logger
|
|
||||||
addrmgr.UseLogger(logger)
|
|
||||||
|
|
||||||
case "CMGR":
|
|
||||||
cmgrLog = logger
|
|
||||||
connmgr.UseLogger(logger)
|
|
||||||
|
|
||||||
case "BCDB":
|
|
||||||
bcdbLog = logger
|
|
||||||
database.UseLogger(logger)
|
|
||||||
|
|
||||||
case "BMGR":
|
|
||||||
bmgrLog = logger
|
|
||||||
|
|
||||||
case "BTCD":
|
|
||||||
btcdLog = logger
|
|
||||||
|
|
||||||
case "CHAN":
|
|
||||||
chanLog = logger
|
|
||||||
blockchain.UseLogger(logger)
|
|
||||||
|
|
||||||
case "DISC":
|
|
||||||
discLog = logger
|
|
||||||
|
|
||||||
case "INDX":
|
|
||||||
indxLog = logger
|
|
||||||
indexers.UseLogger(logger)
|
|
||||||
|
|
||||||
case "MINR":
|
|
||||||
minrLog = logger
|
|
||||||
mining.UseLogger(logger)
|
|
||||||
cpuminer.UseLogger(logger)
|
|
||||||
|
|
||||||
case "PEER":
|
|
||||||
peerLog = logger
|
|
||||||
peer.UseLogger(logger)
|
|
||||||
|
|
||||||
case "RPCS":
|
|
||||||
rpcsLog = logger
|
|
||||||
|
|
||||||
case "SCRP":
|
|
||||||
scrpLog = logger
|
|
||||||
txscript.UseLogger(logger)
|
|
||||||
|
|
||||||
case "SRVR":
|
|
||||||
srvrLog = logger
|
|
||||||
|
|
||||||
case "TXMP":
|
|
||||||
txmpLog = logger
|
|
||||||
mempool.UseLogger(logger)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// initSeelogLogger initializes a new seelog logger that is used as the backend
|
|
||||||
// for all logging subsystems.
|
|
||||||
func initSeelogLogger(logFile string) {
|
|
||||||
config := `
|
|
||||||
<seelog type="adaptive" mininterval="2000000" maxinterval="100000000"
|
|
||||||
critmsgcount="500" minlevel="trace">
|
|
||||||
<outputs formatid="all">
|
|
||||||
<console />
|
|
||||||
<rollingfile type="size" filename="%s" maxsize="10485760" maxrolls="3" />
|
|
||||||
</outputs>
|
|
||||||
<formats>
|
|
||||||
<format id="all" format="%%Time %%Date [%%LEV] %%Msg%%n" />
|
|
||||||
</formats>
|
|
||||||
</seelog>`
|
|
||||||
config = fmt.Sprintf(config, logFile)
|
|
||||||
|
|
||||||
logger, err := seelog.LoggerFromConfigAsString(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to create logger: %v", err)
|
fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
pr, pw := io.Pipe()
|
||||||
|
r, err := rotator.New(pr, logFile, 10*1024, false, 3)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
backendLog = logger
|
go r.Run()
|
||||||
|
|
||||||
|
logRotator = r
|
||||||
|
logRotatorPipe = pw
|
||||||
}
|
}
|
||||||
|
|
||||||
// setLogLevel sets the logging level for provided subsystem. Invalid
|
// setLogLevel sets the logging level for provided subsystem. Invalid
|
||||||
|
@ -165,17 +140,8 @@ func setLogLevel(subsystemID string, logLevel string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default to info if the log level is invalid.
|
// Defaults to info if the log level is invalid.
|
||||||
level, ok := btclog.LogLevelFromString(logLevel)
|
level, _ := btclog.LevelFromString(logLevel)
|
||||||
if !ok {
|
|
||||||
level = btclog.InfoLvl
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create new logger for the subsystem if needed.
|
|
||||||
if logger == btclog.Disabled {
|
|
||||||
logger = btclog.NewSubsystemLogger(backendLog, subsystemID+": ")
|
|
||||||
useLogger(subsystemID, logger)
|
|
||||||
}
|
|
||||||
logger.SetLevel(level)
|
logger.SetLevel(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,6 @@
|
||||||
package cpuminer
|
package cpuminer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btclog"
|
"github.com/btcsuite/btclog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,37 +19,12 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableLog disables all library log output. Logging output is disabled
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
// by default until either UseLogger or SetLogWriter are called.
|
// by default until UseLogger is called.
|
||||||
func DisableLog() {
|
func DisableLog() {
|
||||||
log = btclog.Disabled
|
log = btclog.Disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// UseLogger uses a specified Logger to output package logging info.
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
// This should be used in preference to SetLogWriter if the caller is also
|
|
||||||
// using btclog.
|
|
||||||
func UseLogger(logger btclog.Logger) {
|
func UseLogger(logger btclog.Logger) {
|
||||||
log = logger
|
log = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogWriter uses a specified io.Writer to output package logging info.
|
|
||||||
// This allows a caller to direct package logging output without needing a
|
|
||||||
// dependency on seelog. If the caller is also using btclog, UseLogger should
|
|
||||||
// be used instead.
|
|
||||||
func SetLogWriter(w io.Writer, level string) error {
|
|
||||||
if w == nil {
|
|
||||||
return errors.New("nil writer")
|
|
||||||
}
|
|
||||||
|
|
||||||
lvl, ok := btclog.LogLevelFromString(level)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("invalid log level")
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := btclog.NewLoggerFromWriter(w, lvl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
UseLogger(l)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,9 +5,6 @@
|
||||||
package mining
|
package mining
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btclog"
|
"github.com/btcsuite/btclog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,37 +19,12 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableLog disables all library log output. Logging output is disabled
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
// by default until either UseLogger or SetLogWriter are called.
|
// by default until UseLogger is called.
|
||||||
func DisableLog() {
|
func DisableLog() {
|
||||||
log = btclog.Disabled
|
log = btclog.Disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// UseLogger uses a specified Logger to output package logging info.
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
// This should be used in preference to SetLogWriter if the caller is also
|
|
||||||
// using btclog.
|
|
||||||
func UseLogger(logger btclog.Logger) {
|
func UseLogger(logger btclog.Logger) {
|
||||||
log = logger
|
log = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogWriter uses a specified io.Writer to output package logging info.
|
|
||||||
// This allows a caller to direct package logging output without needing a
|
|
||||||
// dependency on seelog. If the caller is also using btclog, UseLogger should
|
|
||||||
// be used instead.
|
|
||||||
func SetLogWriter(w io.Writer, level string) error {
|
|
||||||
if w == nil {
|
|
||||||
return errors.New("nil writer")
|
|
||||||
}
|
|
||||||
|
|
||||||
lvl, ok := btclog.LogLevelFromString(level)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("invalid log level")
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := btclog.NewLoggerFromWriter(w, lvl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
UseLogger(l)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
29
peer/log.go
29
peer/log.go
|
@ -5,9 +5,7 @@
|
||||||
package peer
|
package peer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -34,41 +32,16 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableLog disables all library log output. Logging output is disabled
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
// by default until either UseLogger or SetLogWriter are called.
|
// by default until UseLogger is called.
|
||||||
func DisableLog() {
|
func DisableLog() {
|
||||||
log = btclog.Disabled
|
log = btclog.Disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// UseLogger uses a specified Logger to output package logging info.
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
// This should be used in preference to SetLogWriter if the caller is also
|
|
||||||
// using btclog.
|
|
||||||
func UseLogger(logger btclog.Logger) {
|
func UseLogger(logger btclog.Logger) {
|
||||||
log = logger
|
log = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogWriter uses a specified io.Writer to output package logging info.
|
|
||||||
// This allows a caller to direct package logging output without needing a
|
|
||||||
// dependency on seelog. If the caller is also using btclog, UseLogger should
|
|
||||||
// be used instead.
|
|
||||||
func SetLogWriter(w io.Writer, level string) error {
|
|
||||||
if w == nil {
|
|
||||||
return errors.New("nil writer")
|
|
||||||
}
|
|
||||||
|
|
||||||
lvl, ok := btclog.LogLevelFromString(level)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("invalid log level")
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := btclog.NewLoggerFromWriter(w, lvl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
UseLogger(l)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogClosure is a closure that can be printed with %v to be used to
|
// LogClosure is a closure that can be printed with %v to be used to
|
||||||
// generate expensive-to-create data for a detailed log level and avoid doing
|
// generate expensive-to-create data for a detailed log level and avoid doing
|
||||||
// the work if the data isn't printed.
|
// the work if the data isn't printed.
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
// Copyright (c) 2015 The btcsuite developers Use of this source code is
|
|
||||||
// governed by an ISC license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package peer_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/peer"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSetLogWriter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
w io.Writer
|
|
||||||
level string
|
|
||||||
expected error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "nil writer",
|
|
||||||
w: nil,
|
|
||||||
level: "trace",
|
|
||||||
expected: errors.New("nil writer"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid log level",
|
|
||||||
w: bytes.NewBuffer(nil),
|
|
||||||
level: "wrong",
|
|
||||||
expected: errors.New("invalid log level"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "use off level",
|
|
||||||
w: bytes.NewBuffer(nil),
|
|
||||||
level: "off",
|
|
||||||
expected: errors.New("min level can't be greater than max. Got min: 6, max: 5"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "pass",
|
|
||||||
w: bytes.NewBuffer(nil),
|
|
||||||
level: "debug",
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
err := peer.SetLogWriter(test.w, test.level)
|
|
||||||
if err != nil {
|
|
||||||
if err.Error() != test.expected.Error() {
|
|
||||||
t.Errorf("SetLogWriter #%d (%s) wrong result\n"+
|
|
||||||
"got: %v\nwant: %v", i, test.name, err,
|
|
||||||
test.expected)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if test.expected != nil {
|
|
||||||
t.Errorf("SetLogWriter #%d (%s) wrong result\n"+
|
|
||||||
"got: %v\nwant: %v", i, test.name, err,
|
|
||||||
test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2039,11 +2039,11 @@ func handleGetBlockTemplateProposal(s *rpcServer, request *btcjson.TemplateReque
|
||||||
isOrphan, err := s.server.blockManager.ProcessBlock(block, flags)
|
isOrphan, err := s.server.blockManager.ProcessBlock(block, flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(blockchain.RuleError); !ok {
|
if _, ok := err.(blockchain.RuleError); !ok {
|
||||||
err := rpcsLog.Errorf("Failed to process block "+
|
errStr := fmt.Sprintf("Failed to process block proposal: %v", err)
|
||||||
"proposal: %v", err)
|
rpcsLog.Error(errStr)
|
||||||
return nil, &btcjson.RPCError{
|
return nil, &btcjson.RPCError{
|
||||||
Code: btcjson.ErrRPCVerify,
|
Code: btcjson.ErrRPCVerify,
|
||||||
Message: err.Error(),
|
Message: errStr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,6 @@
|
||||||
package txscript
|
package txscript
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btclog"
|
"github.com/btcsuite/btclog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,41 +19,16 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableLog disables all library log output. Logging output is disabled
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
// by default until either UseLogger or SetLogWriter are called.
|
// by default until UseLogger is called.
|
||||||
func DisableLog() {
|
func DisableLog() {
|
||||||
log = btclog.Disabled
|
log = btclog.Disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// UseLogger uses a specified Logger to output package logging info.
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
// This should be used in preference to SetLogWriter if the caller is also
|
|
||||||
// using btclog.
|
|
||||||
func UseLogger(logger btclog.Logger) {
|
func UseLogger(logger btclog.Logger) {
|
||||||
log = logger
|
log = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogWriter uses a specified io.Writer to output package logging info.
|
|
||||||
// This allows a caller to direct package logging output without needing a
|
|
||||||
// dependency on seelog. If the caller is also using btclog, UseLogger should
|
|
||||||
// be used instead.
|
|
||||||
func SetLogWriter(w io.Writer, level string) error {
|
|
||||||
if w == nil {
|
|
||||||
return errors.New("nil writer")
|
|
||||||
}
|
|
||||||
|
|
||||||
lvl, ok := btclog.LogLevelFromString(level)
|
|
||||||
if !ok {
|
|
||||||
return errors.New("invalid log level")
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := btclog.NewLoggerFromWriter(w, lvl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
UseLogger(l)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogClosure is a closure that can be printed with %v to be used to
|
// LogClosure is a closure that can be printed with %v to be used to
|
||||||
// generate expensive-to-create data for a detailed log level and avoid doing
|
// generate expensive-to-create data for a detailed log level and avoid doing
|
||||||
// the work if the data isn't printed.
|
// the work if the data isn't printed.
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package txscript
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSetLogWriter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
w io.Writer
|
|
||||||
level string
|
|
||||||
expected error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "nil writer",
|
|
||||||
w: nil,
|
|
||||||
level: "trace",
|
|
||||||
expected: errors.New("nil writer"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid log level",
|
|
||||||
w: os.Stdout,
|
|
||||||
level: "wrong",
|
|
||||||
expected: errors.New("invalid log level"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "use off level",
|
|
||||||
w: os.Stdout,
|
|
||||||
level: "off",
|
|
||||||
expected: errors.New("min level can't be greater than max. Got min: 6, max: 5"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "pass",
|
|
||||||
w: os.Stdout,
|
|
||||||
level: "debug",
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
err := SetLogWriter(test.w, test.level)
|
|
||||||
if err != nil {
|
|
||||||
if err.Error() != test.expected.Error() {
|
|
||||||
t.Errorf("SetLogWriter #%d (%s) wrong result\n"+
|
|
||||||
"got: %v\nwant: %v", i, test.name, err,
|
|
||||||
test.expected)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if test.expected != nil {
|
|
||||||
t.Errorf("SetLogWriter #%d (%s) wrong result\n"+
|
|
||||||
"got: %v\nwant: %v", i, test.name, err,
|
|
||||||
test.expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue