logx

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2022 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// InfoLevel logs everything
	InfoLevel uint32 = iota
	// ErrorLevel includes errors, slows, stacks
	ErrorLevel
	// SevereLevel only log severe messages
	SevereLevel
)

Variables

View Source
var (
	// ErrLogPathNotSet is an error that indicates the log path is not set.
	ErrLogPathNotSet = errors.New("log path must be set")
	// ErrLogServiceNameNotSet is an error that indicates that the service name is not set.
	ErrLogServiceNameNotSet = errors.New("log service name must be set")
)
View Source
var ErrLogFileClosed = errors.New("error: log file closed")

ErrLogFileClosed is an error that indicates the log file is already closed.

Functions

func Alert

func Alert(v string)

Alert alerts v in alert level, and the message is written to error log.

func Close

func Close() error

Close closes the logging.

func CollectSysLog

func CollectSysLog()

CollectSysLog redirects system log into logx info

func Disable

func Disable()

Disable disables the logging.

func DisableStat

func DisableStat()

DisableStat disables the stat logs.

func Error

func Error(v ...interface{})

Error writes v into error log.

func ErrorStack

func ErrorStack(v ...interface{})

ErrorStack writes v along with call stack into error log.

func ErrorStackf

func ErrorStackf(format string, v ...interface{})

ErrorStackf writes v along with call stack in format into error log.

func Errorf

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

Errorf writes v with format into error log.

func Errorv

func Errorv(v interface{})

Errorv writes v into error log with json content. No call stack attached, because not elegant to pack the messages.

func Errorw

func Errorw(msg string, fields ...LogField)

Errorw writes msg along with fields into error log.

func Info

func Info(v ...interface{})

Info writes v into access log.

func Infof

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

Infof writes v with format into access log.

func Infov

func Infov(v interface{})

Infov writes v into access log with json content.

func Infow

func Infow(msg string, fields ...LogField)

Infow writes msg along with fields into access log.

func Must

func Must(err error)

Must checks if err is nil, otherwise logs the error and exits.

func MustSetup

func MustSetup(c LogConf)

MustSetup sets up logging with given config c. It exits on error.

func SetLevel

func SetLevel(level uint32)

SetLevel sets the logging level. It can be used to suppress some logs.

func SetUp

func SetUp(c LogConf) error

SetUp sets up the logx. If already set up, just return nil. we allow SetUp to be called multiple times, because for example we need to allow different service frameworks to initialize logx respectively. the same logic for SetUp

func SetWriter

func SetWriter(w Writer)

SetWriter sets the logging writer. It can be used to customize the logging. Call Reset before calling SetWriter again.

func Severe

func Severe(v ...interface{})

Severe writes v into severe log.

func Severef

func Severef(format string, v ...interface{})

Severef writes v with format into severe log.

func Slow

func Slow(v ...interface{})

Slow writes v into slow log.

func Slowf

func Slowf(format string, v ...interface{})

Slowf writes v with format into slow log.

func Slowv

func Slowv(v interface{})

Slowv writes v into slow log with json content.

func Sloww

func Sloww(msg string, fields ...LogField)

Sloww writes msg along with fields into slow log.

func Stat

func Stat(v ...interface{})

Stat writes v into stat log.

func Statf

func Statf(format string, v ...interface{})

Statf writes v with format into stat log.

func WithColor

func WithColor(text string, colour color.Color) string

WithColor is a helper function to add color to a string, only in plain encoding.

func WithColorPadding

func WithColorPadding(text string, colour color.Color) string

WithColorPadding is a helper function to add color to a string with leading and trailing spaces, only in plain encoding.

Types

type DailyRotateRule

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

A DailyRotateRule is a rule to daily rotate the log files.

func (*DailyRotateRule) BackupFileName

func (r *DailyRotateRule) BackupFileName() string

BackupFileName returns the backup filename on rotating.

func (*DailyRotateRule) MarkRotated

func (r *DailyRotateRule) MarkRotated()

MarkRotated marks the rotated time of r to be the current time.

func (*DailyRotateRule) OutdatedFiles

func (r *DailyRotateRule) OutdatedFiles() []string

OutdatedFiles returns the files that exceeded the keeping days.

func (*DailyRotateRule) ShallRotate

func (r *DailyRotateRule) ShallRotate() bool

ShallRotate checks if the file should be rotated.

type LessLogger

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

A LessLogger is a logger that control to log once during the given duration.

func NewLessLogger

func NewLessLogger(milliseconds int) *LessLogger

NewLessLogger returns a LessLogger.

func (*LessLogger) Error

func (logger *LessLogger) Error(v ...interface{})

Error logs v into error log or discard it if more than once in the given duration.

func (*LessLogger) Errorf

func (logger *LessLogger) Errorf(format string, v ...interface{})

Errorf logs v with format into error log or discard it if more than once in the given duration.

type LogConf

type LogConf struct {
	ServiceName         string `json:",optional"`
	Mode                string `json:",default=console,options=[console,file,volume]"`
	Encoding            string `json:",default=json,options=[json,plain]"`
	TimeFormat          string `json:",optional"`
	Path                string `json:",default=logs"`
	Level               string `json:",default=info,options=[info,error,severe]"`
	Compress            bool   `json:",optional"`
	KeepDays            int    `json:",optional"`
	StackCooldownMillis int    `json:",default=100"`
}

A LogConf is a logging config.

type LogField

type LogField struct {
	Key   string
	Value interface{}
}

LogField is a key-value pair that will be added to the log entry.

func Field

func Field(key string, value interface{}) LogField

Field returns a LogField for the given key and value.

type LogOption

type LogOption func(options *logOptions)

LogOption defines the method to customize the logging.

func WithCooldownMillis

func WithCooldownMillis(millis int) LogOption

WithCooldownMillis customizes logging on writing call stack interval.

func WithGzip

func WithGzip() LogOption

WithGzip customizes logging to automatically gzip the log files.

func WithKeepDays

func WithKeepDays(days int) LogOption

WithKeepDays customizes logging to keep logs with days.

type Logger

type Logger interface {
	// Error logs a message at error level.
	Error(...interface{})
	// Errorf logs a message at error level.
	Errorf(string, ...interface{})
	// Errorv logs a message at error level.
	Errorv(interface{})
	// Errorw logs a message at error level.
	Errorw(string, ...LogField)
	// Info logs a message at info level.
	Info(...interface{})
	// Infof logs a message at info level.
	Infof(string, ...interface{})
	// Infov logs a message at info level.
	Infov(interface{})
	// Infow logs a message at info level.
	Infow(string, ...LogField)
	// Slow logs a message at slow level.
	Slow(...interface{})
	// Slowf logs a message at slow level.
	Slowf(string, ...interface{})
	// Slowv logs a message at slow level.
	Slowv(interface{})
	// Sloww logs a message at slow level.
	Sloww(string, ...LogField)
	// WithContext returns a new logger with the given context.
	WithContext(context.Context) Logger
	// WithDuration returns a new logger with the given duration.
	WithDuration(time.Duration) Logger
}

A Logger represents a logger.

func WithContext

func WithContext(ctx context.Context) Logger

WithContext sets ctx to log, for keeping tracing information.

func WithDuration

func WithDuration(d time.Duration) Logger

WithDuration returns a Logger which logs the given duration.

type RotateLogger

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

A RotateLogger is a Logger that can rotate log files with given rules.

func NewLogger

func NewLogger(filename string, rule RotateRule, compress bool) (*RotateLogger, error)

NewLogger returns a RotateLogger with given filename and rule, etc.

func (*RotateLogger) Close

func (l *RotateLogger) Close() error

Close closes l.

func (*RotateLogger) Write

func (l *RotateLogger) Write(data []byte) (int, error)

type RotateRule

type RotateRule interface {
	BackupFileName() string
	MarkRotated()
	OutdatedFiles() []string
	ShallRotate() bool
}

A RotateRule interface is used to define the log rotating rules.

func DefaultRotateRule

func DefaultRotateRule(filename, delimiter string, days int, gzip bool) RotateRule

DefaultRotateRule is a default log rotating rule, currently DailyRotateRule.

type Writer

type Writer interface {
	Alert(v interface{})
	Close() error
	Error(v interface{}, fields ...LogField)
	Info(v interface{}, fields ...LogField)
	Severe(v interface{})
	Slow(v interface{}, fields ...LogField)
	Stack(v interface{})
	Stat(v interface{}, fields ...LogField)
}

func NewWriter

func NewWriter(w io.Writer) Writer

NewWriter creates a new Writer with the given io.Writer.

func Reset

func Reset() Writer

Reset clears the writer and resets the log level.

Jump to

Keyboard shortcuts

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