log4go

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2021 License: GPL-3.0 Imports: 16 Imported by: 5

README

log4go

The simple but powerful log for go.

TODO

  • base logger
  • console writer
  • file writer
  • kafka writer
  • net writer

ENV

The go version shall >= 1.16

Writers

  • support output the caller`s file and lines
  • support level filter
  • simply use, pls ref xxx_test.go
FileWriter

Filename regex support: %Y %M %D %H %m, prefix must be %

KafkaWriter

Can writer to kafka easily, with es_index you can also transfer data to ES easily. If you want more fields can set them by the field msg.extra_fields.

Documentation

Index

Constants

View Source
const (
	WriterNameConsole = "console_writer"
	WriterNameFile    = "file_writer"
	WriterNameKafka   = "kafka_writer"
)
View Source
const (
	LevelFlagEmergency = "EMERGENCY"
	LevelFlagAlert     = "ALERT"
	LevelFlagCritical  = "CRITICAL"
	LevelFlagError     = "ERROR"
	LevelFlagWarning   = "WARNING" // compatible WARN
	LevelFlagWarn      = "WARN"
	LevelFlagNotice    = "NOTICE"
	LevelFlagInfo      = "INFO"
	LevelFlagDebug     = "DEBUG"
)

LevelFlag log level flags

View Source
const (
	EMERGENCY = iota // Emergency: system is unusable
	ALERT            // Alert: action must be taken immediately
	CRITICAL         // Critical: critical conditions
	ERROR            // Error: error conditions
	WARNING          // Warning: warning conditions
	NOTICE           // Notice: normal but significant condition
	INFO             // Informational: informational messages
	DEBUG            // Debug: debug-level messages
)

RFC5424 log message levels. ref: https://tools.ietf.org/html/draft-ietf-syslog-protocol-23

Variables

LevelFlags level Flags set

View Source
var GlobalLevel = DEBUG

GlobalLevel global level

Functions

func Alert

func Alert(fmt string, args ...interface{})

Alert level alert

func Close

func Close()

Close close logger

func Critical

func Critical(fmt string, args ...interface{})

Critical level critical

func Debug

func Debug(fmt string, args ...interface{})

Debug level debug

func Emergency

func Emergency(fmt string, args ...interface{})

Emergency level emergency

func Error

func Error(fmt string, args ...interface{})

Error level error

func Info

func Info(fmt string, args ...interface{})

Info level info

func Notice

func Notice(fmt string, args ...interface{})

Notice level notice

func Register

func Register(w Writer)

Register register writer

func SetLayout

func SetLayout(layout string)

SetLayout set the logger time layout, should call before logger real use

func SetLevel

func SetLevel(lvl int)

SetLevel set the logger level, should call before logger real use

func SetLog

func SetLog(config []byte) (err error)

SetLog setup log with config []byte

func SetLogWithConf

func SetLogWithConf(file string) (err error)

SetLogWithConf setup log with config file

func SetupLog

func SetupLog(lc LogConfig) (err error)

SetupLog setup log

func Warn

func Warn(fmt string, args ...interface{})

Warn level warn

func WithFullPath

func WithFullPath(show bool)

WithFullPath set the logger with full path, should call before logger real use

func WithFuncName

func WithFuncName(show bool)

WithFuncName set the logger with func name, should call before logger real use

Types

type ConsoleWriter

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

ConsoleWriter console writer define

func NewConsoleWriter

func NewConsoleWriter() *ConsoleWriter

NewConsoleWriter create new console writer

func NewConsoleWriterWithOptions

func NewConsoleWriterWithOptions(options ConsoleWriterOptions) *ConsoleWriter

NewConsoleWriterWithOptions create new console writer with level

func (*ConsoleWriter) Init

func (w *ConsoleWriter) Init() error

Init console init without implement

func (*ConsoleWriter) SetColor

func (w *ConsoleWriter) SetColor(c bool)

SetColor console output color control

func (*ConsoleWriter) SetFullColor

func (w *ConsoleWriter) SetFullColor(c bool)

SetFullColor console output full line color control

func (*ConsoleWriter) Write

func (w *ConsoleWriter) Write(r *Record) error

Write console write

type ConsoleWriterOptions

type ConsoleWriterOptions struct {
	Enable    bool   `json:"enable" mapstructure:"enable"`
	Color     bool   `json:"color" mapstructure:"color"`
	FullColor bool   `json:"full_color" mapstructure:"full_color"`
	Level     string `json:"level" mapstructure:"level"`
}

ConsoleWriterOptions color field options

type FileWriter

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

FileWriter file writer for log record deal

func NewFileWriter

func NewFileWriter() *FileWriter

NewFileWriter create new file writer

func NewFileWriterWithOptions

func NewFileWriterWithOptions(options FileWriterOptions) *FileWriter

NewFileWriterWithOptions create new file writer with options

func (*FileWriter) Flush

func (w *FileWriter) Flush() error

Flush writes any buffered data to file

func (*FileWriter) Init

func (w *FileWriter) Init() error

Init file writer init

func (*FileWriter) Rotate

func (w *FileWriter) Rotate() error

Rotate file writer rotate

func (*FileWriter) SetPathPattern

func (w *FileWriter) SetPathPattern(pattern string) error

SetPathPattern for file writer

func (*FileWriter) Write

func (w *FileWriter) Write(r *Record) error

Write file write

type FileWriterOptions

type FileWriterOptions struct {
	Level    string `json:"level" mapstructure:"level"`
	Filename string `json:"filename" mapstructure:"filename"`
	Enable   bool   `json:"enable" mapstructure:"enable"`

	Rotate bool `json:"rotate" mapstructure:"rotate"`
	// Rotate daily
	Daily bool `json:"daily" mapstructure:"daily"`
	// Rotate hourly
	Hourly bool `json:"hourly" mapstructure:"hourly"`
	// Rotate minutely
	Minutely bool `json:"minutely" mapstructure:"minutely"`

	MaxDays    int `json:"max_days" mapstructure:"max_days"`
	MaxHours   int `json:"max_hours" mapstructure:"max_hours"`
	MaxMinutes int `json:"max_minutes" mapstructure:"max_minutes"`
}

FileWriterOptions file writer options

type Flusher

type Flusher interface {
	Flush() error
}

Flusher record flusher

type KafKaMSGFields

type KafKaMSGFields struct {
	ESIndex   string `json:"es_index" mapstructure:"es_index"` // optional, init field, can set if want send data to es
	Level     string `json:"level"`                            // dynamic, set by logger, mark the record level
	File      string `json:"file"`                             // source code file:line_number
	Message   string `json:"message"`                          // required, dynamic
	ServerIP  string `json:"server_ip"`                        // required, init field, set by app
	Timestamp string `json:"timestamp"`                        // required, dynamic, set by logger
	Now       int64  `json:"now"`                              // choice

	ExtraFields map[string]interface{} `json:"extra_fields" mapstructure:"extra_fields"` // extra fields will be added
}

KafKaMSGFields kafka msg fields

type KafKaWriter

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

KafKaWriter kafka writer

func NewKafKaWriter

func NewKafKaWriter(options KafKaWriterOptions) *KafKaWriter

NewKafKaWriter new kafka writer

func (*KafKaWriter) Init

func (k *KafKaWriter) Init() error

Init service for Record

func (*KafKaWriter) Start

func (k *KafKaWriter) Start() (err error)

Start start the kafka writer

func (*KafKaWriter) Stop

func (k *KafKaWriter) Stop()

Stop stop the kafka writer

func (*KafKaWriter) Write

func (k *KafKaWriter) Write(r *Record) error

Write service for Record

type KafKaWriterOptions

type KafKaWriterOptions struct {
	Enable                  bool `json:"enable" mapstructure:"enable"`
	Debug                   bool `json:"debug" mapstructure:"debug"`                     // if true, will output the send msg
	SpecifyVersion          bool `json:"specify_version" mapstructure:"specify_version"` // if use the input version, default false
	ProducerReturnSuccesses bool `json:"producer_return_successes" mapstructure:"producer_return_successes"`
	BufferSize              int  `json:"buffer_size" mapstructure:"buffer_size"`

	Level      string `json:"level" mapstructure:"level"`
	VersionStr string `json:"version" mapstructure:"version"` // used to specify the kafka version, ex: 0.10.0.1 or 1.1.1

	Key string `json:"key" mapstructure:"key"` // kafka producer key, choice field

	ProducerTopic   string        `json:"producer_topic" mapstructure:"producer_topic"`
	ProducerTimeout time.Duration `json:"producer_timeout" mapstructure:"producer_timeout"`
	Brokers         []string      `json:"brokers" mapstructure:"brokers"`

	MSG KafKaMSGFields `json:"msg"`
}

KafKaWriterOptions kafka writer options

type LogConfig

type LogConfig struct {
	Level         string               `json:"level" mapstructure:"level"`
	Debug         bool                 `json:"debug" mapstructure:"debug"` // output log info or not for log4go
	FullPath      bool                 `json:"full_path" mapstructure:"full_path"`
	ConsoleWriter ConsoleWriterOptions `json:"console_writer" mapstructure:"console_writer"`
	FileWriter    FileWriterOptions    `json:"file_writer" mapstructure:"file_writer"`
	KafKaWriter   KafKaWriterOptions   `json:"kafka_writer" mapstructure:"kafka_writer"`
}

LogConfig log config

type Logger

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

Logger logger define

func NewLogger

func NewLogger() *Logger

NewLogger create the logger

func (*Logger) Alert

func (l *Logger) Alert(fmt string, args ...interface{})

Alert level alert

func (*Logger) Close

func (l *Logger) Close()

Close close logger

func (*Logger) Critical

func (l *Logger) Critical(fmt string, args ...interface{})

Critical level critical

func (*Logger) Debug

func (l *Logger) Debug(fmt string, args ...interface{})

Debug level debug

func (*Logger) Emergency

func (l *Logger) Emergency(fmt string, args ...interface{})

Emergency level emergency

func (*Logger) Error

func (l *Logger) Error(fmt string, args ...interface{})

Error level error

func (*Logger) Info

func (l *Logger) Info(fmt string, args ...interface{})

Info level info

func (*Logger) Notice

func (l *Logger) Notice(fmt string, args ...interface{})

Notice level notice

func (*Logger) Register

func (l *Logger) Register(w Writer)

Register register writer the writer should be register once for writers by kind

func (*Logger) SetLayout

func (l *Logger) SetLayout(layout string)

SetLayout set the logger time layout

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl int)

SetLevel set the logger level

func (*Logger) Warn

func (l *Logger) Warn(fmt string, args ...interface{})

Warn level warn

func (*Logger) WithFullPath

func (l *Logger) WithFullPath(show bool)

WithFullPath set the logger with full path

func (*Logger) WithFuncName

func (l *Logger) WithFuncName(show bool)

WithFuncName set the logger with func name

type Record

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

Record log record

func (*Record) String

func (r *Record) String() string

type Rotater

type Rotater interface {
	Rotate() error
	SetPathPattern(string) error
}

Rotater record rotater

type Writer

type Writer interface {
	Init() error
	Write(*Record) error
}

Writer record writer

Jump to

Keyboard shortcuts

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