Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
all: Remove seelog logger.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrick committed Jun 20, 2017
1 parent 016915c commit ce4b77d
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 548 deletions.
43 changes: 1 addition & 42 deletions blockchain/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
package blockchain

import (
"errors"
"io"

"github.com/btcsuite/btclog"
)

Expand All @@ -23,50 +20,12 @@ func init() {
}

// 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() {
log = btclog.Disabled
}

// 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) {
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
// generate expensive-to-create data for a detailed log level and avoid doing
// the work if the data isn't printed.
type logClosure func() string

func (c logClosure) String() string {
return c()
}

func newLogClosure(c func() string) logClosure {
return logClosure(c)
}
43 changes: 1 addition & 42 deletions blockchain/stake/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
package stake

import (
"errors"
"io"

"github.com/btcsuite/btclog"
)

Expand All @@ -23,50 +20,12 @@ func init() {
}

// 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() {
log = btclog.Disabled
}

// 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) {
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
// generate expensive-to-create data for a detailed log level and avoid doing
// the work if the data isn't printed.
type logClosure func() string

func (c logClosure) String() string {
return c()
}

func newLogClosure(c func() string) logClosure {
return logClosure(c)
}
12 changes: 6 additions & 6 deletions cmd/addblock/addblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func realMain() error {
cfg = tcfg

// Setup logging.
backendLogger := btclog.NewDefaultBackendLogger()
defer backendLogger.Flush()
log = btclog.NewSubsystemLogger(backendLogger, "")
database.UseLogger(btclog.NewSubsystemLogger(backendLogger, "BCDB: "))
blockchain.UseLogger(btclog.NewSubsystemLogger(backendLogger, "CHAN: "))
indexers.UseLogger(btclog.NewSubsystemLogger(backendLogger, "INDX: "))
backendLogger := btclog.NewBackend(os.Stdout)
defer os.Stdout.Sync()
log = backendLogger.Logger("MAIN")
database.UseLogger(backendLogger.Logger("BCDB"))
blockchain.UseLogger(backendLogger.Logger("CHAN"))
indexers.UseLogger(backendLogger.Logger("INDX"))

// Load the block database.
db, err := loadBlockDB()
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ func loadConfig() (*config, []string, error) {
os.Exit(0)
}

// Initialize logging at the default logging level.
initSeelogLogger(filepath.Join(cfg.LogDir, defaultLogFilename))
setLogLevels(defaultLogLevel)
// Initialize log rotation. After log rotation has been initialized, the
// logger variables may be used.
initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename))

// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
Expand Down
12 changes: 0 additions & 12 deletions connmgr/connmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
package connmgr

import (
"bytes"
"errors"
"io"
"net"
"sync/atomic"
"testing"
"time"

"github.com/btcsuite/btclog"
)

func init() {
Expand Down Expand Up @@ -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
// expected.
func TestStartStop(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions database/cmd/dbtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func loadBlockDB() (database.DB, error) {
// around the fact that deferred functions do not run when os.Exit() is called.
func realMain() error {
// Setup logging.
backendLogger := btclog.NewDefaultBackendLogger()
defer backendLogger.Flush()
log = btclog.NewSubsystemLogger(backendLogger, "")
dbLog := btclog.NewSubsystemLogger(backendLogger, "BCDB: ")
dbLog.SetLevel(btclog.DebugLvl)
backendLogger := btclog.NewBackend(os.Stdout)
defer os.Stdout.Sync()
log = backendLogger.Logger("MAIN")
dbLog := backendLogger.Logger("BCDB")
dbLog.SetLevel(btclog.LevelDebug)
database.UseLogger(dbLog)

// Setup the parser options and commands.
Expand Down
8 changes: 4 additions & 4 deletions database/ffldb/blockio.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
}
for ; wc.curFileNum > oldBlockFileNum; wc.curFileNum-- {
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)
return
}
Expand All @@ -692,7 +692,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
obf, err := s.openWriteFileFunc(wc.curFileNum)
if err != nil {
wc.curFile.Unlock()
_ = log.Warnf("ROLLBACK: %v", err)
log.Warnf("ROLLBACK: %v", err)
return
}
wc.curFile.file = obf
Expand All @@ -701,7 +701,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
// Truncate the to the provided rollback offset.
if err := wc.curFile.file.Truncate(int64(oldBlockOffset)); err != nil {
wc.curFile.Unlock()
_ = log.Warnf("ROLLBACK: Failed to truncate file %d: %v",
log.Warnf("ROLLBACK: Failed to truncate file %d: %v",
wc.curFileNum, err)
return
}
Expand All @@ -710,7 +710,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) {
err := wc.curFile.file.Sync()
wc.curFile.Unlock()
if err != nil {
_ = log.Warnf("ROLLBACK: Failed to sync file %d: %v",
log.Warnf("ROLLBACK: Failed to sync file %d: %v",
wc.curFileNum, err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion database/ffldb/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func reconcileDB(pdb *db, create bool) (database.DB, error) {
str := fmt.Sprintf("metadata claims file %d, offset %d, but "+
"block data is at file %d, offset %d", curFileNum,
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)
}

Expand Down
30 changes: 1 addition & 29 deletions database/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
package database

import (
"errors"
"io"

"github.com/btcsuite/btclog"
)

Expand All @@ -23,14 +20,12 @@ func init() {
}

// 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() {
log = btclog.Disabled
}

// 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) {
log = logger

Expand All @@ -41,26 +36,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
}
68 changes: 0 additions & 68 deletions database/log_test.go

This file was deleted.

6 changes: 5 additions & 1 deletion dcrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func dcrdMain(serverChan chan<- *server) error {
return err
}
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
// triggered either from an OS signal such as SIGINT (Ctrl+C) or from
Expand Down
Loading

0 comments on commit ce4b77d

Please sign in to comment.