Documentation ¶
Overview ¶
Package handler provide useful common log handlers.
eg: file, console, multi_file, rotate_file, stream, syslog, email
Example (FileHandler) ¶
package main import ( "github.com/gookit/slog" "github.com/gookit/slog/handler" ) func main() { withLevels := handler.WithLogLevels(slog.Levels{slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel}) h1 := handler.MustFileHandler("/tmp/error.log", withLevels) withLevels = handler.WithLogLevels(slog.Levels{slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel}) h2 := handler.MustFileHandler("/tmp/info.log", withLevels) slog.PushHandler(h1) slog.PushHandler(h2) // add logs slog.Info("info message") slog.Error("error message") }
Output:
Example (RotateFileHandler) ¶
package main import ( "github.com/gookit/slog" "github.com/gookit/slog/handler" ) func main() { h1 := handler.MustRotateFile("/tmp/error.log", handler.EveryHour, handler.WithLogLevels(slog.DangerLevels)) h2 := handler.MustRotateFile("/tmp/info.log", handler.EveryHour, handler.WithLogLevels(slog.NormalLevels)) slog.PushHandler(h1) slog.PushHandler(h2) // add logs slog.Info("info message") slog.Error("error message") }
Output:
Index ¶
- Constants
- Variables
- func BufferWrapper(handler FormatterWriterHandler, buffSize int) *bufferWrapper
- func LineBuffOsFile(f *os.File, bufSize int, levels []slog.Level) slog.Handler
- func LineBuffWriter(w io.Writer, bufSize int, levels []slog.Level) slog.Handler
- func LineBufferedFile(logfile string, bufSize int, levels []slog.Level) (slog.Handler, error)
- func QuickOpenFile(filepath string) (*os.File, error)
- type Builder
- type Config
- type ConfigFn
- func WithBuffMode(buffMode string) ConfigFn
- func WithBuffSize(buffSize int) ConfigFn
- func WithLogLevels(levels slog.Levels) ConfigFn
- func WithLogfile(logfile string) ConfigFn
- func WithMaxSize(maxSize int) ConfigFn
- func WithRotateTime(rt rotatefile.RotateTime) ConfigFn
- func WithUseJSON(useJson bool) ConfigFn
- type ConsoleHandler
- type EmailHandler
- type EmailOption
- type FlushCloseHandler
- func NewBuffered(w io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler
- func NewBufferedHandler(w io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler
- func NewFlushCloseHandler(out FlushCloseWriter, levels []slog.Level) *FlushCloseHandler
- func NewFlushCloser(out FlushCloseWriter, levels []slog.Level) *FlushCloseHandler
- type FlushCloseWriter
- type FlushWriter
- type FormatterWriterHandler
- type GroupedHandler
- type IOWriterHandler
- type LevelWithFormatterdeprecated
- type LevelsWithFormatterdeprecated
- type LockWrapper
- type MultiFileHandler
- type NopFlushClose
- type RotateTimedeprecated
- type SimpleHandler
- type SyncCloseHandler
- func JSONFileHandler(logfile string, fns ...ConfigFn) (*SyncCloseHandler, error)
- func MustFileHandler(logfile string, fns ...ConfigFn) *SyncCloseHandler
- func MustRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) *SyncCloseHandler
- func MustSimpleFile(filepath string) *SyncCloseHandler
- func MustSizeRotateFile(logfile string, size int, fns ...ConfigFn) *SyncCloseHandler
- func MustTimeRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) *SyncCloseHandler
- func NewBuffFileHandler(logfile string, buffSize int, fns ...ConfigFn) (*SyncCloseHandler, error)
- func NewFileHandler(logfile string, fns ...ConfigFn) (h *SyncCloseHandler, err error)
- func NewRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
- func NewRotateFileHandler(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
- func NewSimpleFile(filepath string) (*SyncCloseHandler, error)
- func NewSimpleFileHandler(filePath string) (*SyncCloseHandler, error)
- func NewSizeRotateFile(logfile string, maxSize int, fns ...ConfigFn) (*SyncCloseHandler, error)
- func NewSizeRotateFileHandler(logfile string, maxSize int, fns ...ConfigFn) (*SyncCloseHandler, error)
- func NewSyncCloseHandler(out SyncCloseWriter, levels []slog.Level) *SyncCloseHandler
- func NewSyncCloser(out SyncCloseWriter, levels []slog.Level) *SyncCloseHandler
- func NewTimeRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
- func NewTimeRotateFileHandler(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
- type SyncCloseWriter
- type SysLogHandler
- type WriteCloserHandler
Examples ¶
Constants ¶
const ( BuffModeLine = "line" BuffModeBite = "bite" )
const ( EveryDay = rotatefile.EveryDay EveryHour = rotatefile.EveryDay Every30Minutes = rotatefile.Every30Min Every15Minutes = rotatefile.Every15Min EveryMinute = rotatefile.EveryMinute EverySecond = rotatefile.EverySecond // only use for tests )
Deprecated: Please use define constants on pkg rotatefile. e.g. rotatefile.EveryDay
Variables ¶
var ( // DefaultFilePerm perm and flags for create log file DefaultFilePerm os.FileMode = 0664 // DefaultFileFlags for create/open file DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND )
var DefaultBufferSize = 8 * 1024
DefaultBufferSize sizes the buffer associated with each log file. It's large so that log records can accumulate without the logging thread blocking on disk I/O. The flushDaemon will block instead.
Functions ¶
func BufferWrapper ¶ added in v0.1.1
func BufferWrapper(handler FormatterWriterHandler, buffSize int) *bufferWrapper
BufferWrapper new instance
func LineBuffOsFile ¶ added in v0.3.0
LineBuffOsFile handler
func LineBuffWriter ¶ added in v0.3.0
LineBuffWriter handler
func LineBufferedFile ¶ added in v0.3.0
LineBufferedFile handler
Types ¶
type Builder ¶ added in v0.3.0
Builder struct for create handler
type Config ¶ added in v0.3.0
type Config struct { // Logfile for write logs Logfile string `json:"logfile" yaml:"logfile"` // UseJSON for format logs UseJSON bool `json:"use_json" yaml:"use_json"` // BuffMode type name. allow: line, bite BuffMode string `json:"buff_mode" yaml:"buff_mode"` // BuffSize for enable buffer. set 0 to disable buffer BuffSize int `json:"buff_size" yaml:"buff_size"` // Levels for log record Levels []slog.Level `json:"levels" yaml:"levels"` // RotateTime for rotate file RotateTime rotatefile.RotateTime `json:"rotate_time" yaml:"rotate_time"` // MaxSize on rotate file by size. MaxSize uint64 `json:"max_size" yaml:"max_size"` // RenameFunc build filename for rotate file RenameFunc func(filepath string, rotateNum uint) string }
Config struct
func NewEmptyConfig ¶ added in v0.3.0
NewEmptyConfig new config instance
func (*Config) CreateHandler ¶ added in v0.3.0
func (c *Config) CreateHandler() (*SyncCloseHandler, error)
CreateHandler quick create a handler by config
func (*Config) CreateWriter ¶ added in v0.3.0
func (c *Config) CreateWriter() (output SyncCloseWriter, err error)
CreateWriter build writer by config
func (*Config) RotateWriter ¶ added in v0.3.0
func (c *Config) RotateWriter() (output SyncCloseWriter, err error)
RotateWriter build rotate writer by config
type ConfigFn ¶ added in v0.3.0
type ConfigFn func(c *Config)
ConfigFn for config some settings
func WithBuffMode ¶ added in v0.3.0
WithBuffMode setting
func WithLogLevels ¶ added in v0.3.0
WithLogLevels setting
func WithRotateTime ¶ added in v0.3.0
func WithRotateTime(rt rotatefile.RotateTime) ConfigFn
WithRotateTime setting
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) 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 slog.LevelWithFormatter // From the sender email information From EmailOption // ToAddresses email 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 `json:"smtp_host"` // eg "smtp.gmail.com" SmtpPort int `json:"smtp_port"` // eg 587 FromAddr string `json:"from_addr"` // eg "yourEmail@gmail.com" Password string `json:"password"` }
EmailOption struct
type FlushCloseHandler ¶ added in v0.3.0
type FlushCloseHandler struct { slog.LevelFormattable Output FlushCloseWriter }
FlushCloseHandler definition
func NewBuffered ¶ added in v0.1.2
func NewBuffered(w io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler
NewBuffered create new BufferedHandler
func NewBufferedHandler ¶
func NewBufferedHandler(w io.WriteCloser, bufSize int, levels ...slog.Level) *FlushCloseHandler
NewBufferedHandler create new BufferedHandler
func NewFlushCloseHandler ¶ added in v0.3.0
func NewFlushCloseHandler(out FlushCloseWriter, levels []slog.Level) *FlushCloseHandler
NewFlushCloseHandler create new FlushCloseHandler
Usage:
buf := new(bytes.Buffer) h := handler.NewFlushCloseHandler(&buf, slog.AllLevels) f, err := os.OpenFile("my.log", ...) h := handler.NewFlushCloseHandler(f, slog.AllLevels)
func NewFlushCloser ¶ added in v0.3.0
func NewFlushCloser(out FlushCloseWriter, levels []slog.Level) *FlushCloseHandler
NewFlushCloser create new FlushCloseHandler
func (*FlushCloseHandler) Close ¶ added in v0.3.0
func (h *FlushCloseHandler) Close() error
Close the handler
func (*FlushCloseHandler) Flush ¶ added in v0.3.0
func (h *FlushCloseHandler) Flush() error
Flush the handler
type FlushCloseWriter ¶ added in v0.3.0
type FlushCloseWriter interface { Flush() error // WriteCloser the output writer io.WriteCloser }
FlushCloseWriter is the interface satisfied by logging destinations.
type FlushWriter ¶ added in v0.3.0
FlushWriter is the interface satisfied by logging destinations.
type FormatterWriterHandler ¶ added in v0.3.0
type FormatterWriterHandler interface { slog.Handler // Formatter record formatter Formatter() slog.Formatter // Writer the output writer Writer() io.Writer }
FormatterWriterHandler interface
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 { slog.LevelFormattable Output io.Writer }
IOWriterHandler definition
func NewIOWriter ¶ added in v0.1.2
func NewIOWriter(out io.Writer, levels []slog.Level) *IOWriterHandler
NewIOWriter create a 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
type LevelWithFormatter
deprecated
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
Deprecated: please use slog.LevelWithFormatter instead.
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
deprecated
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
Deprecated: please use slog.LevelsWithFormatter instead.
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 LockWrapper ¶ added in v0.3.0
LockWrapper struct
func (*LockWrapper) EnableLock ¶ added in v0.3.0
func (lw *LockWrapper) EnableLock(enable bool)
EnableLock enable lock
func (*LockWrapper) LockEnabled ¶ added in v0.3.0
func (lw *LockWrapper) LockEnabled() bool
LockEnabled status
type MultiFileHandler ¶ added in v0.0.3
type MultiFileHandler struct { LockWrapper // FileDir for save log files FileDir string // FileLevels 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.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(_ *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
type NopFlushClose ¶ added in v0.1.1
type NopFlushClose struct{}
NopFlushClose no operation.
provide empty Flush(), Close() methods, useful for tests.
func (*NopFlushClose) Flush ¶ added in v0.1.1
func (h *NopFlushClose) Flush() error
Flush logs to disk
type RotateTime
deprecated
added in
v0.3.0
type RotateTime = rotatefile.RotateTime
RotateTime rotate log file by time.
EveryDay:
- "error.log.20201223"
EveryHour, Every30Minutes, EveryMinute:
- "error.log.20201223_1500"
- "error.log.20201223_1530"
- "error.log.20201223_1523"
Deprecated: please use rotatefile.RotateTime
type SimpleHandler ¶ added in v0.3.0
type SimpleHandler struct { NopFlushClose slog.LevelFormattable Output io.Writer }
SimpleHandler definition
func NewHandler ¶ added in v0.3.0
func NewHandler(out io.Writer, level slog.Level) *SimpleHandler
NewHandler create a new instance
func NewSimple ¶ added in v0.3.0
func NewSimple(out io.Writer, level slog.Level) *SimpleHandler
NewSimple create a new instance
func NewSimpleHandler ¶ added in v0.3.0
func NewSimpleHandler(out io.Writer, level slog.Level) *SimpleHandler
NewSimpleHandler create new SimpleHandler
Usage:
buf := new(bytes.Buffer) h := handler.NewSimpleHandler(&buf, slog.InfoLevel) f, err := os.OpenFile("my.log", ...) h := handler.NewSimpleHandler(f, slog.InfoLevel)
type SyncCloseHandler ¶ added in v0.3.0
type SyncCloseHandler struct { slog.LevelFormattable Output SyncCloseWriter }
SyncCloseHandler definition
func JSONFileHandler ¶
func JSONFileHandler(logfile string, fns ...ConfigFn) (*SyncCloseHandler, error)
JSONFileHandler create new FileHandler with JSON formatter
func MustFileHandler ¶ added in v0.1.0
func MustFileHandler(logfile string, fns ...ConfigFn) *SyncCloseHandler
MustFileHandler create file handler
func MustRotateFile ¶ added in v0.1.1
func MustRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) *SyncCloseHandler
MustRotateFile handler instance
func MustSimpleFile ¶ added in v0.1.1
func MustSimpleFile(filepath string) *SyncCloseHandler
MustSimpleFile new instance
func MustSizeRotateFile ¶ added in v0.1.1
func MustSizeRotateFile(logfile string, size int, fns ...ConfigFn) *SyncCloseHandler
MustSizeRotateFile instance
func MustTimeRotateFile ¶ added in v0.1.1
func MustTimeRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) *SyncCloseHandler
MustTimeRotateFile instance
func NewBuffFileHandler ¶ added in v0.3.0
func NewBuffFileHandler(logfile string, buffSize int, fns ...ConfigFn) (*SyncCloseHandler, error)
NewBuffFileHandler create file handler with buff size
func NewFileHandler ¶
func NewFileHandler(logfile string, fns ...ConfigFn) (h *SyncCloseHandler, err error)
NewFileHandler create new FileHandler
func NewRotateFile ¶ added in v0.1.1
func NewRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
NewRotateFile instance
func NewRotateFileHandler ¶ added in v0.1.0
func NewRotateFileHandler(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
NewRotateFileHandler instance It supports splitting log files by time and size
func NewSimpleFile ¶ added in v0.1.1
func NewSimpleFile(filepath string) (*SyncCloseHandler, error)
NewSimpleFile new instance
func NewSimpleFileHandler ¶ added in v0.1.0
func NewSimpleFileHandler(filePath string) (*SyncCloseHandler, error)
NewSimpleFileHandler instance
Usage:
h, err := NewSimpleFileHandler("/tmp/error.log")
custom formatter
h.SetFormatter(slog.NewJSONFormatter()) slog.PushHandler(h) slog.Info("log message")
func NewSizeRotateFile ¶ added in v0.1.1
func NewSizeRotateFile(logfile string, maxSize int, fns ...ConfigFn) (*SyncCloseHandler, error)
NewSizeRotateFile instance
func NewSizeRotateFileHandler ¶ added in v0.1.1
func NewSizeRotateFileHandler(logfile string, maxSize int, fns ...ConfigFn) (*SyncCloseHandler, error)
NewSizeRotateFileHandler instance
func NewSyncCloseHandler ¶ added in v0.3.0
func NewSyncCloseHandler(out SyncCloseWriter, levels []slog.Level) *SyncCloseHandler
NewSyncCloseHandler create new SyncCloseHandler
Usage:
f, err := os.OpenFile("my.log", ...) h := handler.NewSyncCloseHandler(f, slog.AllLevels)
func NewSyncCloser ¶ added in v0.3.0
func NewSyncCloser(out SyncCloseWriter, levels []slog.Level) *SyncCloseHandler
NewSyncCloser create new SyncCloseHandler
func NewTimeRotateFile ¶ added in v0.1.1
func NewTimeRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
NewTimeRotateFile instance
func NewTimeRotateFileHandler ¶ added in v0.1.0
func NewTimeRotateFileHandler(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
NewTimeRotateFileHandler instance
func (*SyncCloseHandler) Close ¶ added in v0.3.0
func (h *SyncCloseHandler) Close() error
Close the handler
func (*SyncCloseHandler) Flush ¶ added in v0.3.0
func (h *SyncCloseHandler) Flush() error
Flush the handler
func (*SyncCloseHandler) Handle ¶ added in v0.3.0
func (h *SyncCloseHandler) Handle(record *slog.Record) error
Handle log record
func (*SyncCloseHandler) Writer ¶ added in v0.3.0
func (h *SyncCloseHandler) Writer() io.Writer
Writer of the handler
type SyncCloseWriter ¶ added in v0.3.0
type SyncCloseWriter interface { Sync() error // WriteCloser the output writer io.WriteCloser }
SyncCloseWriter is the interface satisfied by logging destinations. such as os.File
type SysLogHandler ¶ added in v0.1.0
type SysLogHandler struct { slog.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
type WriteCloserHandler ¶ added in v0.3.0
type WriteCloserHandler struct { // LockWrapper slog.LevelFormattable Output io.WriteCloser }
WriteCloserHandler definition
func NewIOWriteCloserHandler ¶ added in v0.3.0
func NewIOWriteCloserHandler(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler
NewIOWriteCloserHandler create new WriteCloserHandler
Usage:
buf := new(bytes.Buffer) h := handler.NewIOWriteCloserHandler(&buf, slog.AllLevels) f, err := os.OpenFile("my.log", ...) h := handler.NewIOWriteCloserHandler(f, slog.AllLevels)
func NewWriteCloser ¶ added in v0.3.0
func NewWriteCloser(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler
NewWriteCloser create a new instance
func (*WriteCloserHandler) Close ¶ added in v0.3.0
func (h *WriteCloserHandler) Close() error
Close the handler
func (*WriteCloserHandler) Flush ¶ added in v0.3.0
func (h *WriteCloserHandler) Flush() error
Flush the handler