logger

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: MIT Imports: 21 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 dirrectly 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.
  • CreateTestLogger() prints log lines formatted for test runners. Use this in your tests!
  • CreateMemoryTestLogger() records logs in memory. It's useful for making assertions on the produced logs.

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

This section is empty.

Variables

View Source
var (
	HeadTracker = "head_tracker"
	FluxMonitor = "fluxmonitor"
)

Constants for service names for package specific logging configuration

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 ErrorIf

func ErrorIf(err error, optionalMsg ...string)

func ErrorIfCalling

func ErrorIfCalling(f func() error, optionalMsg ...string)

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

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

Fatalw logs a message and exits the application

func GetLogServices

func GetLogServices() []string

func Info

func Info(args ...interface{})

Info logs an info message.

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 NewErrorw

func NewErrorw(msg string, keysAndValues ...interface{}) error

Logs and returns a new error

func NewORM

func NewORM(db *gorm.DB) *orm

NewORM initializes a new ORM

func NewProductionConfig

func NewProductionConfig(lvl zapcore.Level, dir string, jsonConsole, toDisk bool) (c zap.Config)

NewProductionConfig returns a production logging config

func NewProductionEncoderConfig

func NewProductionEncoderConfig() zapcore.EncoderConfig

NewProductionEncoderConfig returns a production encoder config

func Panic

func Panic(args ...interface{})

Panic logs a panic message then panics.

func PanicIf

func PanicIf(err error)

PanicIf logs the error if present.

func Panicf

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

Panicf formats and then logs the message before panicking.

func SetLogger

func SetLogger(newLogger *Logger)

SetLogger sets the internal logger to the given input.

DEPRECATED this method is deprecated because it leads to race conditions. Instead, you should fork the logger.Default instance to create a new logger for your module. Eg: logger.Default.Named("<my-package-name>")

func Sync

func Sync() error

Sync flushes any buffered log entries.

func Trace

func Trace(args ...interface{})

Trace is a shim stand-in for when we have real trace-level logging support

func Tracef

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

Tracef is a shim stand-in for when we have real trace-level logging support

func Tracew

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

Tracew is a shim stand-in for when we have real trace-level logging support

func Warn

func Warn(args ...interface{})

Warn logs a message at the warn level.

func WarnIf

func WarnIf(err error)

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 LogConfig

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 struct {
	*zap.SugaredLogger
	Orm ORM
	// contains filtered or unexported fields
}

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
)

func CreateLogger

func CreateLogger(zl *zap.SugaredLogger) *Logger

CreateLogger dwisott

func CreateLoggerWithConfig

func CreateLoggerWithConfig(zl *zap.SugaredLogger, lvl zapcore.Level, dir string, jsonConsole bool, toDisk bool) *Logger

func CreateMemoryTestLogger

func CreateMemoryTestLogger(lvl zapcore.Level) *Logger

CreateMemoryTestLogger creates a logger that only directs output to the buffer testMemoryLog.

func CreateProductionLogger

func CreateProductionLogger(
	dir string, jsonConsole bool, lvl zapcore.Level, toDisk bool) *Logger

CreateProductionLogger returns a log config for the passed directory with the given LogLevel and customizes stdout for pretty printing.

func CreateTestLogger

func CreateTestLogger(lvl zapcore.Level) *Logger

CreateTestLogger creates a logger that directs output to PrettyConsole configured for test output, and to the buffer testMemoryLog.

func (*Logger) ErrorIf

func (l *Logger) ErrorIf(err error, optionalMsg ...string)

ErrorIf logs the error if present.

func (*Logger) ErrorIfCalling

func (l *Logger) ErrorIfCalling(f func() error, optionalMsg ...string)

ErrorIfCalling calls the given function and logs the error of it if there is.

func (*Logger) GetServiceLogLevels

func (l *Logger) GetServiceLogLevels() (map[string]string, error)

GetServiceLogLevels retrieves all service log levels from the db

func (*Logger) InitServiceLevelLogger

func (l *Logger) InitServiceLevelLogger(serviceName string, logLevel string) (*Logger, error)

InitServiceLevelLogger builds a service level logger with a given logging level & serviceName

func (*Logger) PanicIf

func (l *Logger) PanicIf(err error)

func (*Logger) ServiceLogLevel

func (l *Logger) ServiceLogLevel(serviceName string) (string, error)

ServiceLogLevel is the log level set for a specified package

func (*Logger) SetDB

func (l *Logger) SetDB(db *gorm.DB)

func (*Logger) WarnIf

func (l *Logger) WarnIf(err error)

WarnIf logs the error if present.

func (*Logger) Write

func (l *Logger) Write(b []byte) (int, error)

Write logs a message at the Info level and returns the length of the given bytes.

type MemorySink

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

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

func MemoryLogTestingOnly

func MemoryLogTestingOnly() *MemorySink

func (*MemorySink) Close

func (s *MemorySink) Close() error

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

func (*MemorySink) String

func (s *MemorySink) String() string

String returns the full log contents, as a string

func (*MemorySink) Sync

func (s *MemorySink) Sync() error

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

func (*MemorySink) Write

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

type ORM

type ORM interface {
	GetServiceLogLevel(serviceName string) (string, error)
	SetServiceLogLevel(ctx context.Context, serviceName string, level zapcore.Level) 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.

Jump to

Keyboard shortcuts

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