Documentation ¶
Overview ¶
Package handler provide useful common log handlers. eg: file, console, multi_file, rotate_file, stream, syslog, email
Index ¶
- Constants
- Variables
- func BufferWrapper(handler slog.FormatterWriterHandler, buffSize int) *bufferWrapper
- func OpenFile(filepath string, flag int, perm int) (*os.File, error)
- func QuickOpenFile(filepath string) (*os.File, error)
- type BufferedHandler
- func (h *BufferedHandler) Close() error
- func (h *BufferedHandler) Flush() error
- func (h *BufferedHandler) Handle(record *slog.Record) error
- func (lw *BufferedHandler) Lock()
- func (lw *BufferedHandler) LockEnabled() bool
- func (lw *BufferedHandler) Unlock()
- func (lw *BufferedHandler) UseLock(enable bool)
- type ConsoleHandler
- type EmailHandler
- type EmailOption
- type FileHandler
- func (h *FileHandler) Close() error
- func (h *FileHandler) Configure(fn func(h *FileHandler)) *FileHandler
- func (h *FileHandler) Flush() error
- func (h *FileHandler) Handle(r *slog.Record) (err error)
- func (lw *FileHandler) Lock()
- func (lw *FileHandler) LockEnabled() bool
- func (h *FileHandler) ReopenFile() error
- func (lw *FileHandler) Unlock()
- func (lw *FileHandler) UseLock(enable bool)
- func (h *FileHandler) Writer() io.Writer
- type GroupedHandler
- type IOWriterHandler
- func (h *IOWriterHandler) Close() error
- func (h *IOWriterHandler) Flush() error
- func (h *IOWriterHandler) Handle(record *slog.Record) error
- func (lw *IOWriterHandler) Lock()
- func (lw *IOWriterHandler) LockEnabled() bool
- func (lw *IOWriterHandler) Unlock()
- func (lw *IOWriterHandler) UseLock(enable bool)
- type LevelWithFormatter
- type LevelsWithFormatter
- type MultiFileHandler
- func (h *MultiFileHandler) Close() error
- func (h *MultiFileHandler) Flush() error
- func (h *MultiFileHandler) Handle(record *slog.Record) error
- func (h *MultiFileHandler) IsHandling(level slog.Level) bool
- func (lw *MultiFileHandler) Lock()
- func (lw *MultiFileHandler) LockEnabled() bool
- func (lw *MultiFileHandler) Unlock()
- func (lw *MultiFileHandler) UseLock(enable bool)
- type NopFlushClose
- type RotateFileHandler
- func (h *RotateFileHandler) Close() error
- func (h *RotateFileHandler) CloseBuffer()
- func (h *RotateFileHandler) Flush() error
- func (h *RotateFileHandler) Handle(r *slog.Record) (err error)
- func (lw *RotateFileHandler) Lock()
- func (lw *RotateFileHandler) LockEnabled() bool
- func (lw *RotateFileHandler) Unlock()
- func (lw *RotateFileHandler) UseLock(enable bool)
- func (h *RotateFileHandler) Write(bts []byte) (n int, err error)
- type SimpleFileHandler
- func (h *SimpleFileHandler) Close() error
- func (h *SimpleFileHandler) Flush() error
- func (h *SimpleFileHandler) Handle(r *slog.Record) (err error)
- func (lw *SimpleFileHandler) Lock()
- func (lw *SimpleFileHandler) LockEnabled() bool
- func (h *SimpleFileHandler) ReopenFile() error
- func (lw *SimpleFileHandler) Unlock()
- func (lw *SimpleFileHandler) UseLock(enable bool)
- func (h *SimpleFileHandler) Write(bts []byte) (n int, err error)
- func (h *SimpleFileHandler) Writer() io.Writer
- type SizeRotateFileHandler
- type SysLogHandler
- type TimeRotateFileHandler
- func (h *TimeRotateFileHandler) Close() error
- func (h *TimeRotateFileHandler) CloseBuffer()
- func (h *TimeRotateFileHandler) Flush() error
- func (h *TimeRotateFileHandler) Handle(r *slog.Record) (err error)
- func (lw *TimeRotateFileHandler) Lock()
- func (lw *TimeRotateFileHandler) LockEnabled() bool
- func (lw *TimeRotateFileHandler) Unlock()
- func (lw *TimeRotateFileHandler) UseLock(enable bool)
- func (h *TimeRotateFileHandler) Write(bts []byte) (n int, err error)
Examples ¶
Constants ¶
const ( EveryDay rotateTime = iota EveryHour Every30Minutes Every15Minutes EveryMinute EverySecond // only use for tests )
Variables ¶
Functions ¶
func BufferWrapper ¶ added in v0.1.1
func BufferWrapper(handler slog.FormatterWriterHandler, buffSize int) *bufferWrapper
BufferWrapper new instance
Types ¶
type BufferedHandler ¶
type BufferedHandler struct { LevelsWithFormatter // contains filtered or unexported fields }
BufferedHandler definition
func NewBuffered ¶ added in v0.1.2
func NewBuffered(cWriter io.WriteCloser, bufSize int) *BufferedHandler
NewBuffered create new BufferedHandler
func NewBufferedHandler ¶
func NewBufferedHandler(cWriter io.WriteCloser, bufSize int) *BufferedHandler
NewBufferedHandler create new BufferedHandler
func (*BufferedHandler) Handle ¶
func (h *BufferedHandler) Handle(record *slog.Record) error
Handle log record
func (*BufferedHandler) LockEnabled ¶ added in v0.1.0
func (lw *BufferedHandler) LockEnabled() bool
LockEnabled status
type ConsoleHandler ¶
type ConsoleHandler struct {
IOWriterHandler
}
ConsoleHandler definition
func NewConsole ¶ added in v0.1.1
func NewConsole(levels []slog.Level) *ConsoleHandler
NewConsole create new ConsoleHandler
func NewConsoleHandler ¶
func NewConsoleHandler(levels []slog.Level) *ConsoleHandler
NewConsoleHandler create new ConsoleHandler
func (*ConsoleHandler) LockEnabled ¶ added in v0.1.0
func (lw *ConsoleHandler) LockEnabled() bool
LockEnabled status
func (*ConsoleHandler) TextFormatter ¶ added in v0.0.5
func (h *ConsoleHandler) TextFormatter() *slog.TextFormatter
TextFormatter get the formatter
type EmailHandler ¶ added in v0.0.5
type EmailHandler struct { // NopFlushClose provide empty Flush(), Close() methods NopFlushClose // LevelWithFormatter support level and formatter LevelWithFormatter // From the sender email information From EmailOption // ToAddresses list ToAddresses []string }
EmailHandler struct
func NewEmailHandler ¶ added in v0.1.1
func NewEmailHandler(from EmailOption, toAddresses []string) *EmailHandler
NewEmailHandler instance
type EmailOption ¶ added in v0.1.1
type EmailOption struct { SmtpHost string // eg "smtp.gmail.com" SmtpPort string // eg "587" FromAddr string // eg "yourEmail@gmail.com" Password string }
EmailOption struct
type FileHandler ¶
type FileHandler struct { // LevelsWithFormatter support limit log levels and formatter LevelsWithFormatter // NoBuffer on write log records NoBuffer bool // BuffSize for enable buffer BuffSize int // contains filtered or unexported fields }
FileHandler definition
Example ¶
package main import ( "github.com/gookit/slog" "github.com/gookit/slog/handler" ) func main() { h1 := handler.MustFileHandler("/tmp/error.log", true) h1.Levels = slog.Levels{slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel} h2 := handler.MustFileHandler("/tmp/info.log", true) h1.Levels = slog.Levels{slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel} slog.PushHandler(h1) slog.PushHandler(h2) // add logs slog.Info("info message") slog.Error("error message") }
Output:
func JSONFileHandler ¶
func JSONFileHandler(logfile string) (*FileHandler, error)
JSONFileHandler create new FileHandler with JSON formatter
func MustFileHandler ¶ added in v0.1.0
func MustFileHandler(logfile string, useJSON bool) *FileHandler
MustFileHandler create file handler
func NewFileHandler ¶
func NewFileHandler(logfile string, useJSON bool) (*FileHandler, error)
NewFileHandler create new FileHandler
func (*FileHandler) Close ¶
func (h *FileHandler) Close() error
Close handler, will be flush logs to file, then close file
func (*FileHandler) Configure ¶
func (h *FileHandler) Configure(fn func(h *FileHandler)) *FileHandler
Configure the handler
func (*FileHandler) Handle ¶
func (h *FileHandler) Handle(r *slog.Record) (err error)
Handle the log record
func (*FileHandler) LockEnabled ¶ added in v0.1.0
func (lw *FileHandler) LockEnabled() bool
LockEnabled status
func (*FileHandler) ReopenFile ¶ added in v0.1.0
func (h *FileHandler) ReopenFile() error
ReopenFile the log file
type GroupedHandler ¶
type GroupedHandler struct { // Levels for log message Levels []slog.Level // IgnoreErr on handling messages IgnoreErr bool // contains filtered or unexported fields }
GroupedHandler definition
func NewGroupedHandler ¶
func NewGroupedHandler(handlers []slog.Handler) *GroupedHandler
NewGroupedHandler create new GroupedHandler
func (*GroupedHandler) Handle ¶
func (h *GroupedHandler) Handle(record *slog.Record) (err error)
Handle log record
func (*GroupedHandler) IsHandling ¶
func (h *GroupedHandler) IsHandling(level slog.Level) bool
IsHandling Check if the current level can be handling
type IOWriterHandler ¶ added in v0.1.2
type IOWriterHandler struct { LevelsWithFormatter // Output io.WriteCloser Output io.Writer // contains filtered or unexported fields }
IOWriterHandler definition
func NewIOWriter ¶ added in v0.1.2
func NewIOWriter(out io.Writer, levels []slog.Level) *IOWriterHandler
NewIOWriter create an new instance
func NewIOWriterHandler ¶ added in v0.1.2
func NewIOWriterHandler(out io.Writer, levels []slog.Level) *IOWriterHandler
NewIOWriterHandler create new IOWriterHandler Usage:
buf := new(bytes.Buffer) h := handler.NewIOWriterHandler(&buf, slog.AllLevels) f, err := os.OpenFile("my.log", ...) h := handler.NewIOWriterHandler(f, slog.AllLevels)
func (*IOWriterHandler) Close ¶ added in v0.1.2
func (h *IOWriterHandler) Close() error
Close the handler
func (*IOWriterHandler) Flush ¶ added in v0.1.2
func (h *IOWriterHandler) Flush() error
Flush the handler
func (*IOWriterHandler) Handle ¶ added in v0.1.2
func (h *IOWriterHandler) Handle(record *slog.Record) error
Handle log record
func (*IOWriterHandler) LockEnabled ¶ added in v0.1.2
func (lw *IOWriterHandler) LockEnabled() bool
LockEnabled status
type LevelWithFormatter ¶ added in v0.0.3
type LevelWithFormatter struct { slog.Formattable // Level for log message. if current level <= Level will log message Level slog.Level }
LevelWithFormatter struct definition
- support set log formatter - only support set one log level
func (*LevelWithFormatter) IsHandling ¶ added in v0.0.3
func (h *LevelWithFormatter) IsHandling(level slog.Level) bool
IsHandling Check if the current level can be handling
type LevelsWithFormatter ¶ added in v0.0.3
type LevelsWithFormatter struct { slog.Formattable // Levels for log message Levels []slog.Level }
LevelsWithFormatter struct definition
- support set log formatter - support setting multi log levels
func (*LevelsWithFormatter) IsHandling ¶ added in v0.0.3
func (h *LevelsWithFormatter) IsHandling(level slog.Level) bool
IsHandling Check if the current level can be handling
type MultiFileHandler ¶ added in v0.0.3
type MultiFileHandler struct { // FileDir for save log files FileDir string // Files can use multi file for record level logs. eg: // "error.log": []slog.Level{slog.Warn, slog.Error}, // "info.log": []slog.Level{slog.Trace, slog.Info, slog.Notice} // FileLevels map[string][]slog.Level FileLevels map[string]slog.Levels // NoBuffer on write log records NoBuffer bool // BuffSize for enable buffer BuffSize int // file contents max size MaxSize uint64 // contains filtered or unexported fields }
MultiFileHandler definition
func NewMultiFileHandler ¶ added in v0.0.3
func NewMultiFileHandler() *MultiFileHandler
NewMultiFileHandler instance
func (*MultiFileHandler) Close ¶ added in v0.0.5
func (h *MultiFileHandler) Close() error
func (*MultiFileHandler) Flush ¶ added in v0.0.5
func (h *MultiFileHandler) Flush() error
func (*MultiFileHandler) Handle ¶ added in v0.0.5
func (h *MultiFileHandler) Handle(record *slog.Record) error
func (*MultiFileHandler) IsHandling ¶ added in v0.0.5
func (h *MultiFileHandler) IsHandling(level slog.Level) bool
IsHandling Check if the current level can be handling
func (*MultiFileHandler) LockEnabled ¶ added in v0.1.1
func (lw *MultiFileHandler) LockEnabled() bool
LockEnabled status
type NopFlushClose ¶ added in v0.1.1
type NopFlushClose struct{}
NopFlushClose no operation. provide empty Flush(), Close() methods
func (*NopFlushClose) Flush ¶ added in v0.1.1
func (h *NopFlushClose) Flush() error
Flush logs to disk
type RotateFileHandler ¶
type RotateFileHandler struct { // LevelsWithFormatter support limit log levels and formatter LevelsWithFormatter // for clear log files MaxFileCount int // The number of files should be kept MaxKeepTime int // Time to wait until old logs are purged. // file contents max size MaxSize uint64 // RenameFunc build filename for rotate file RenameFunc func(fpath string, rotateNum uint) string // contains filtered or unexported fields }
RotateFileHandler struct definition It also supports splitting log files by time and size
func MustRotateFile ¶ added in v0.1.1
func MustRotateFile(logfile string, rt rotateTime) *RotateFileHandler
MustRotateFile instance
func NewRotateFile ¶ added in v0.1.1
func NewRotateFile(logfile string, rt rotateTime) (*RotateFileHandler, error)
NewRotateFile instance
func NewRotateFileHandler ¶ added in v0.1.0
func NewRotateFileHandler(logfile string, rt rotateTime) (*RotateFileHandler, error)
NewRotateFileHandler instance
func (*RotateFileHandler) Close ¶ added in v0.1.1
func (h *RotateFileHandler) Close() error
Close handler, will be flush logs to file, then close file
func (*RotateFileHandler) CloseBuffer ¶ added in v0.1.1
func (h *RotateFileHandler) CloseBuffer()
CloseBuffer for write logs
func (*RotateFileHandler) Flush ¶ added in v0.1.1
func (h *RotateFileHandler) Flush() error
Flush logs to disk file
func (*RotateFileHandler) Handle ¶ added in v0.1.0
func (h *RotateFileHandler) Handle(r *slog.Record) (err error)
Handle the log record
func (*RotateFileHandler) LockEnabled ¶ added in v0.1.0
func (lw *RotateFileHandler) LockEnabled() bool
LockEnabled status
type SimpleFileHandler ¶ added in v0.1.0
type SimpleFileHandler struct { // LevelWithFormatter support level and formatter LevelWithFormatter // contains filtered or unexported fields }
SimpleFileHandler struct - no buffer, will direct write logs to file.
func MustSimpleFile ¶ added in v0.1.1
func MustSimpleFile(filepath string) *SimpleFileHandler
MustSimpleFile new instance
func NewSimpleFile ¶ added in v0.1.1
func NewSimpleFile(filepath string) (*SimpleFileHandler, error)
NewSimpleFileHandler new instance
func NewSimpleFileHandler ¶ added in v0.1.0
func NewSimpleFileHandler(filepath string) (*SimpleFileHandler, error)
NewSimpleFileHandler instance
Usage:
h, err := NewSimpleFileHandler("", DefaultFileFlags)
custom file flags
h, err := NewSimpleFileHandler("", os.O_CREATE | os.O_WRONLY | os.O_APPEND)
custom formatter
h.SetFormatter(slog.NewJSONFormatter()) slog.PushHandler(h) slog.Info("log message")
func (*SimpleFileHandler) Close ¶ added in v0.1.0
func (h *SimpleFileHandler) Close() error
Close handler, will be flush logs to file, then close file
func (*SimpleFileHandler) Flush ¶ added in v0.1.0
func (h *SimpleFileHandler) Flush() error
Flush logs to disk file
func (*SimpleFileHandler) Handle ¶ added in v0.1.0
func (h *SimpleFileHandler) Handle(r *slog.Record) (err error)
Handle the log record
func (*SimpleFileHandler) LockEnabled ¶ added in v0.1.0
func (lw *SimpleFileHandler) LockEnabled() bool
LockEnabled status
func (*SimpleFileHandler) ReopenFile ¶ added in v0.1.0
func (h *SimpleFileHandler) ReopenFile() error
ReopenFile the log file
func (*SimpleFileHandler) UseLock ¶ added in v0.1.0
func (lw *SimpleFileHandler) UseLock(enable bool)
UseLock locker
type SizeRotateFileHandler ¶ added in v0.1.1
type SizeRotateFileHandler struct { FileHandler // file contents max size MaxSize uint64 // RenameFunc build filename for rotate file RenameFunc func(fpath string, rotateNum uint) string // contains filtered or unexported fields }
SizeRotateFileHandler struct definition
func MustSizeRotateFile ¶ added in v0.1.1
func MustSizeRotateFile(logfile string, maxSize uint64) *SizeRotateFileHandler
MustSizeRotateFile instance
func NewSizeRotateFile ¶ added in v0.1.1
func NewSizeRotateFile(logfile string, maxSize uint64) (*SizeRotateFileHandler, error)
NewSizeRotateFile instance
func NewSizeRotateFileHandler ¶ added in v0.1.1
func NewSizeRotateFileHandler(logfile string, maxSize uint64) (*SizeRotateFileHandler, error)
NewSizeRotateFileHandler instance
func (*SizeRotateFileHandler) Handle ¶ added in v0.1.1
func (h *SizeRotateFileHandler) Handle(r *slog.Record) (err error)
Handle the log record
func (*SizeRotateFileHandler) Lock ¶ added in v0.1.1
func (lw *SizeRotateFileHandler) Lock()
Lock it
func (*SizeRotateFileHandler) LockEnabled ¶ added in v0.1.1
func (lw *SizeRotateFileHandler) LockEnabled() bool
LockEnabled status
type SysLogHandler ¶ added in v0.1.0
type SysLogHandler struct { LevelWithFormatter // contains filtered or unexported fields }
SysLogHandler struct
func NewSysLogHandler ¶ added in v0.1.0
func NewSysLogHandler(priority syslog.Priority, tag string) (*SysLogHandler, error)
NewSysLogHandler instance
func (*SysLogHandler) Close ¶ added in v0.1.0
func (h *SysLogHandler) Close() error
func (*SysLogHandler) Flush ¶ added in v0.1.0
func (h *SysLogHandler) Flush() error
type TimeRotateFileHandler ¶ added in v0.1.0
type TimeRotateFileHandler struct { // LevelsWithFormatter support limit log levels and formatter LevelsWithFormatter // contains filtered or unexported fields }
TimeRotateFileHandler struct refer http://hg.python.org/cpython/file/2.7/Lib/logging/handlers.py refer https://github.com/flike/golog/blob/master/filehandler.go TODO Support custom rotate time. Cut log files on the whole point
func MustTimeRotateFile ¶ added in v0.1.1
func MustTimeRotateFile(logfile string, rt rotateTime) *TimeRotateFileHandler
MustTimeRotateFile instance
func NewTimeRotateFile ¶ added in v0.1.1
func NewTimeRotateFile(logfile string, rt rotateTime) (*TimeRotateFileHandler, error)
NewTimeRotateFile instance
func NewTimeRotateFileHandler ¶ added in v0.1.0
func NewTimeRotateFileHandler(logfile string, rt rotateTime) (*TimeRotateFileHandler, error)
NewTimeRotateFileHandler instance
func (*TimeRotateFileHandler) Close ¶ added in v0.1.0
func (h *TimeRotateFileHandler) Close() error
Close handler, will be flush logs to file, then close file
func (*TimeRotateFileHandler) CloseBuffer ¶ added in v0.1.1
func (h *TimeRotateFileHandler) CloseBuffer()
CloseBuffer for write logs
func (*TimeRotateFileHandler) Flush ¶ added in v0.1.0
func (h *TimeRotateFileHandler) Flush() error
Flush logs to disk file
func (*TimeRotateFileHandler) Handle ¶ added in v0.1.0
func (h *TimeRotateFileHandler) Handle(r *slog.Record) (err error)
Handle the log record
func (*TimeRotateFileHandler) Lock ¶ added in v0.1.0
func (lw *TimeRotateFileHandler) Lock()
Lock it
func (*TimeRotateFileHandler) LockEnabled ¶ added in v0.1.0
func (lw *TimeRotateFileHandler) LockEnabled() bool
LockEnabled status
func (*TimeRotateFileHandler) Unlock ¶ added in v0.1.0
func (lw *TimeRotateFileHandler) Unlock()
Unlock it