handler

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2021 License: MIT Imports: 11 Imported by: 21

Documentation

Overview

Package handler provide useful common log handlers. eg: file, console, multi_file, rotate_file, stream, syslog, email

Index

Examples

Constants

View Source
const (
	EveryDay rotateTime = iota
	EveryHour
	Every30Minutes
	Every15Minutes
	EveryMinute
	EverySecond // only use for tests
)

Variables

View Source
var (
	// DefaultMaxSize is the maximum size of a log file in bytes.
	DefaultMaxSize uint64 = 1024 * 1024 * 1800
	// perm and flags for create log file
	DefaultFilePerm  = 0664
	DefaultFileFlags = os.O_CREATE | os.O_WRONLY | os.O_APPEND
)

Functions

func BufferWrapper added in v0.1.1

func BufferWrapper(handler slog.FormatterWriterHandler, buffSize int) *bufferWrapper

BufferWrapper new instance

func OpenFile added in v0.1.2

func OpenFile(filepath string, flag int, perm int) (*os.File, error)

OpenFile like os.OpenFile

func QuickOpenFile added in v0.1.2

func QuickOpenFile(filepath string) (*os.File, error)

QuickOpenFile like os.OpenFile

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) Close

func (h *BufferedHandler) Close() error

Close log records

func (*BufferedHandler) Flush

func (h *BufferedHandler) Flush() error

Flush all buffers

func (*BufferedHandler) Handle

func (h *BufferedHandler) Handle(record *slog.Record) error

Handle log record

func (*BufferedHandler) Lock added in v0.1.0

func (lw *BufferedHandler) Lock()

Lock it

func (*BufferedHandler) LockEnabled added in v0.1.0

func (lw *BufferedHandler) LockEnabled() bool

LockEnabled status

func (*BufferedHandler) Unlock added in v0.1.0

func (lw *BufferedHandler) Unlock()

Unlock it

func (*BufferedHandler) UseLock added in v0.1.0

func (lw *BufferedHandler) UseLock(enable bool)

UseLock locker

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) Lock added in v0.1.0

func (lw *ConsoleHandler) Lock()

Lock it

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

func (*ConsoleHandler) Unlock added in v0.1.0

func (lw *ConsoleHandler) Unlock()

Unlock it

func (*ConsoleHandler) UseLock added in v0.1.0

func (lw *ConsoleHandler) UseLock(enable bool)

UseLock locker

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

func (*EmailHandler) Handle added in v0.1.1

func (h *EmailHandler) Handle(r *slog.Record) error

Handle an log record

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) Flush

func (h *FileHandler) Flush() error

Flush logs to disk file

func (*FileHandler) Handle

func (h *FileHandler) Handle(r *slog.Record) (err error)

Handle the log record

func (*FileHandler) Lock added in v0.1.0

func (lw *FileHandler) Lock()

Lock it

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

func (*FileHandler) Unlock added in v0.1.0

func (lw *FileHandler) Unlock()

Unlock it

func (*FileHandler) UseLock added in v0.1.0

func (lw *FileHandler) UseLock(enable bool)

UseLock locker

func (*FileHandler) Writer

func (h *FileHandler) Writer() io.Writer

Writer return *os.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) Close

func (h *GroupedHandler) Close() error

Close log handlers

func (*GroupedHandler) Flush

func (h *GroupedHandler) Flush() error

Flush log records

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) Lock added in v0.1.2

func (lw *IOWriterHandler) Lock()

Lock it

func (*IOWriterHandler) LockEnabled added in v0.1.2

func (lw *IOWriterHandler) LockEnabled() bool

LockEnabled status

func (*IOWriterHandler) Unlock added in v0.1.2

func (lw *IOWriterHandler) Unlock()

Unlock it

func (*IOWriterHandler) UseLock added in v0.1.2

func (lw *IOWriterHandler) UseLock(enable bool)

UseLock locker

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) Lock added in v0.1.1

func (lw *MultiFileHandler) Lock()

Lock it

func (*MultiFileHandler) LockEnabled added in v0.1.1

func (lw *MultiFileHandler) LockEnabled() bool

LockEnabled status

func (*MultiFileHandler) Unlock added in v0.1.1

func (lw *MultiFileHandler) Unlock()

Unlock it

func (*MultiFileHandler) UseLock added in v0.1.1

func (lw *MultiFileHandler) UseLock(enable bool)

UseLock locker

type NopFlushClose added in v0.1.1

type NopFlushClose struct{}

NopFlushClose no operation. provide empty Flush(), Close() methods

func (*NopFlushClose) Close added in v0.1.1

func (h *NopFlushClose) Close() error

Close handler

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) Lock added in v0.1.0

func (lw *RotateFileHandler) Lock()

Lock it

func (*RotateFileHandler) LockEnabled added in v0.1.0

func (lw *RotateFileHandler) LockEnabled() bool

LockEnabled status

func (*RotateFileHandler) Unlock added in v0.1.0

func (lw *RotateFileHandler) Unlock()

Unlock it

func (*RotateFileHandler) UseLock added in v0.1.0

func (lw *RotateFileHandler) UseLock(enable bool)

UseLock locker

func (*RotateFileHandler) Write

func (h *RotateFileHandler) Write(bts []byte) (n int, err error)

Write logs

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) Lock added in v0.1.0

func (lw *SimpleFileHandler) Lock()

Lock it

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) Unlock added in v0.1.0

func (lw *SimpleFileHandler) Unlock()

Unlock it

func (*SimpleFileHandler) UseLock added in v0.1.0

func (lw *SimpleFileHandler) UseLock(enable bool)

UseLock locker

func (*SimpleFileHandler) Write added in v0.1.1

func (h *SimpleFileHandler) Write(bts []byte) (n int, err error)

Write contents to *os.File

func (*SimpleFileHandler) Writer added in v0.1.0

func (h *SimpleFileHandler) Writer() io.Writer

Writer return *os.File

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

func (*SizeRotateFileHandler) Unlock added in v0.1.1

func (lw *SizeRotateFileHandler) Unlock()

Unlock it

func (*SizeRotateFileHandler) UseLock added in v0.1.1

func (lw *SizeRotateFileHandler) UseLock(enable bool)

UseLock locker

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

func (*SysLogHandler) Handle added in v0.1.0

func (h *SysLogHandler) Handle(record *slog.Record) error

Handle an record

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

func (*TimeRotateFileHandler) UseLock added in v0.1.0

func (lw *TimeRotateFileHandler) UseLock(enable bool)

UseLock locker

func (*TimeRotateFileHandler) Write added in v0.1.1

func (h *TimeRotateFileHandler) Write(bts []byte) (n int, err error)

Write logs

Jump to

Keyboard shortcuts

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