logger

package
v1.10.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2021 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package logger exports multiple type loggers:

  • the Logger type is a wrapper over uber/zap#SugaredLogger with added conditional utilities.
  • the Default instance is exported and used in all top-level functions (similar to godoc.org/log). You can use this directly but the recommended way is to create a new Logger for your module using Named(), eg: logger.Default.Named("<my-package-name>")
  • ProductionLogger() builds a Logger which stores logs on disk.
  • TestLogger() prints log lines formatted for test runners. Use this in your tests!

Package logger is used to store details of events in the node. Events can be categorized by Trace, Debug, Info, Error, Fatal, and Panic.

Index

Constants

View Source
const (
	HeadTracker = "head_tracker"
	FluxMonitor = "fluxmonitor"
	Keeper      = "keeper"
)

Constants for service names for package specific logging configuration

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug logs a debug message.

func Debugf

func Debugf(format string, values ...interface{})

Debugf formats and then logs the message.

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

Debugw logs a debug message and any additional given information.

func Error

func Error(args ...interface{})

Error logs an error message.

func ErrorIfCalling added in v1.10.16

func ErrorIfCalling(f func() error)

func Errorf

func Errorf(format string, values ...interface{})

Errorf logs a message at the error level using Sprintf.

func Errorw

func Errorw(msg string, keysAndValues ...interface{})

Errorw logs an error message, any additional given information, and includes stack trace.

func Fatal

func Fatal(args ...interface{})

Fatal logs a fatal message then exits the application.

func Fatalf

func Fatalf(format string, values ...interface{})

Fatalf logs a message at the fatal level using Sprintf.

func Fatalw added in v1.10.16

func Fatalw(msg string, keysAndValues ...interface{})

Fatalw logs a message and exits the application

func GetLogServices added in v1.10.16

func GetLogServices() []string

func Infof

func Infof(format string, values ...interface{})

Infof formats and then logs the message.

func Infow

func Infow(msg string, keysAndValues ...interface{})

Infow logs an info message and any additional given information.

func InitColor added in v1.10.17

func InitColor(c bool)

InitColor explicitly sets the global color.NoColor option. Not safe for concurrent use. Only to be called from init().

func InitLogger added in v1.10.17

func InitLogger(newLogger Logger)

InitLogger sets the Default logger to newLogger. Not safe for concurrent use, so must be called from init() or the main goroutine during initialization.

You probably don't want to use this. Instead, you should fork the logger.Default instance to create a new logger: Eg: logger.Default.Named("<my-service-name>")

func NewOCRWrapper added in v1.10.17

func NewOCRWrapper(l Logger, trace bool, saveError func(string)) ocrtypes.Logger

func NewORM added in v1.10.16

func NewORM(db *gorm.DB) *orm

NewORM initializes a new ORM

func Panicf

func Panicf(format string, values ...interface{})

Panicf formats and then logs the message before panicking.

func Sync

func Sync() error

Sync flushes any buffered log entries.

func Warn

func Warn(args ...interface{})

Warn logs a message at the warn level.

func Warnf

func Warnf(format string, values ...interface{})

Warnf formats and then logs the message as Warn.

func Warnw

func Warnw(msg string, keysAndValues ...interface{})

Warnw logs a debug message and any additional given information.

Types

type Config added in v1.10.17

type Config interface {
	RootDir() string
	JSONConsole() bool
	LogToDisk() bool
	LogLevel() zapcore.Level
}

type GormWrapper added in v1.10.17

type GormWrapper struct {
	Logger
	// contains filtered or unexported fields
}

func NewGormWrapper added in v1.10.17

func NewGormWrapper(logger Logger, logAllQueries bool, slowThreshold time.Duration) *GormWrapper

func (*GormWrapper) Error added in v1.10.17

func (o *GormWrapper) Error(ctx context.Context, s string, i ...interface{})

func (*GormWrapper) Info added in v1.10.17

func (o *GormWrapper) Info(ctx context.Context, s string, i ...interface{})

func (*GormWrapper) LogAllQueries added in v1.10.17

func (o *GormWrapper) LogAllQueries(b bool)

func (*GormWrapper) LogMode added in v1.10.17

Noop

func (*GormWrapper) Trace added in v1.10.17

func (o *GormWrapper) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

This is called at the end of every gorm v2 query. We always log the sql queries for errors and slow queries (warns). Need to set LOG_SQL=true to enable all queries.

func (*GormWrapper) Warn added in v1.10.17

func (o *GormWrapper) Warn(ctx context.Context, s string, i ...interface{})

type LogConfig added in v1.10.16

type LogConfig struct {
	ID          int64  `gorm:"primary_key"`
	ServiceName string `gorm:"not null"`
	LogLevel    string `gorm:"not null"`
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

LogConfig stores key value pairs for configuring package specific logging

type Logger

type Logger interface {
	// With creates a new Logger with the given arguments
	With(args ...interface{}) Logger
	// Named creates a new Logger sub-scoped with name.
	// Names are inherited and dot-separated.
	//   a := l.Named("a") // logger=a
	//   b := a.Named("b") // logger=a.b
	Named(name string) Logger

	// NewRootLogger creates a new root Logger with an independent log level
	// unaffected by upstream calls to SetLogLevel.
	NewRootLogger(lvl zapcore.Level) (Logger, error)

	// SetLogLevel changes the log level for this and all connected Loggers.
	SetLogLevel(zapcore.Level)

	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Debugf(format string, values ...interface{})
	Infof(format string, values ...interface{})
	Warnf(format string, values ...interface{})
	Errorf(format string, values ...interface{})
	Fatalf(format string, values ...interface{})

	Debugw(msg string, keysAndValues ...interface{})
	Infow(msg string, keysAndValues ...interface{})
	Warnw(msg string, keysAndValues ...interface{})
	Errorw(msg string, keysAndValues ...interface{})
	Fatalw(msg string, keysAndValues ...interface{})
	Panicw(msg string, keysAndValues ...interface{})

	// WarnIf logs the error if present.
	WarnIf(err error, msg string)
	// ErrorIf logs the error if present.
	ErrorIf(err error, msg string)
	PanicIf(err error, msg string)

	// ErrorIfCalling calls fn and logs any returned error along with func name.
	ErrorIfCalling(fn func() error)

	// Sync flushes any buffered log entries.
	// Some insignificant errors are suppressed.
	Sync() error
	// contains filtered or unexported methods
}

Logger is the main interface of this package. It implements uber/zap's SugaredLogger interface and adds conditional logging helpers.

var (
	// Default logger for use throughout the project.
	// All the package-level functions are calling Default.
	Default Logger
)
var NullLogger Logger

func NewNullLogger added in v1.10.17

func NewNullLogger() Logger

func ProductionLogger added in v1.10.17

func ProductionLogger(c Config) Logger

ProductionLogger returns a custom logger for the config's root directory and LogLevel, with pretty printing for stdout. If LOG_TO_DISK is false, the logger will only log to stdout.

func TestLogger added in v1.10.17

func TestLogger(t T) Logger

TestLogger creates a logger that directs output to PrettyConsole configured for test output, and to the buffer testMemoryLog. t is optional. Log level is derived from the LOG_LEVEL env var.

type MemorySink added in v1.10.16

type MemorySink struct {
	// contains filtered or unexported fields
}

MemorySink implements zap.Sink by writing all messages to a buffer.

func MemoryLogTestingOnly added in v1.10.16

func MemoryLogTestingOnly() *MemorySink

func (*MemorySink) Close added in v1.10.16

func (s *MemorySink) Close() error

Close is a dummy method to satisfy the zap.Sink interface

func (*MemorySink) Reset added in v1.10.17

func (s *MemorySink) Reset()

func (*MemorySink) String added in v1.10.16

func (s *MemorySink) String() string

String returns the full log contents, as a string

func (*MemorySink) Sync added in v1.10.16

func (s *MemorySink) Sync() error

Sync is a dummy method to satisfy the zap.Sink interface

func (*MemorySink) Write added in v1.10.16

func (s *MemorySink) Write(p []byte) (n int, err error)

type ORM added in v1.10.16

type ORM interface {
	GetServiceLogLevel(serviceName string) (level string, ok bool)
	SetServiceLogLevel(ctx context.Context, serviceName string, level string) error
}

type PrettyConsole

type PrettyConsole struct {
	zap.Sink
}

PrettyConsole wraps a Sink (Writer, Syncer, Closer), usually stdout, and formats the incoming json bytes with colors and white space for readability before passing on to the underlying Writer in Sink.

func (PrettyConsole) Write

func (pc PrettyConsole) Write(b []byte) (int, error)

Write reformats the incoming json bytes with colors, newlines and whitespace for better readability in console.

type T added in v1.10.17

type T interface {
	Name() string
	Fatal(...interface{})
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL