handler

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: MIT Imports: 14 Imported by: 21

README

Handlers

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

handler -> buffered -> rotated -> writer(os.File)

Built-in handlers

  • handler.ConsoleHandler Console handler
  • handler.FileHandler File handler
  • handler.StreamHandler Stream handler
  • handler.SyslogHandler Syslog handler
  • handler.EmailHandler Email handler
  • handler.FlushCloseHandler Flush and close handler

Go Docs

Docs generated by: go doc ./handler

Handler Functions
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)

type ConsoleHandler = IOWriterHandler
    func ConsoleWithLevels(levels []slog.Level) *ConsoleHandler
    func ConsoleWithMaxLevel(level slog.Level) *ConsoleHandler
    func NewConsole(levels []slog.Level) *ConsoleHandler
    func NewConsoleHandler(levels []slog.Level) *ConsoleHandler
    func NewConsoleWithLF(lf slog.LevelFormattable) *ConsoleHandler
type EmailHandler struct{ ... }
    func NewEmailHandler(from EmailOption, toAddresses []string) *EmailHandler
type EmailOption struct{ ... }

type FlushCloseHandler struct{ ... }
    func FlushCloserWithLevels(out FlushCloseWriter, levels []slog.Level) *FlushCloseHandler
    func FlushCloserWithMaxLevel(out FlushCloseWriter, maxLevel slog.Level) *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
    func NewFlushCloserWithLF(out FlushCloseWriter, lf slog.LevelFormattable) *FlushCloseHandler

type IOWriterHandler struct{ ... }
    func IOWriterWithLevels(out io.Writer, levels []slog.Level) *IOWriterHandler
    func IOWriterWithMaxLevel(out io.Writer, maxLevel slog.Level) *IOWriterHandler
    func NewIOWriter(out io.Writer, levels []slog.Level) *IOWriterHandler
    func NewIOWriterHandler(out io.Writer, levels []slog.Level) *IOWriterHandler
    func NewIOWriterWithLF(out io.Writer, lf slog.LevelFormattable) *IOWriterHandler
    func NewSimpleHandler(out io.Writer, maxLevel slog.Level) *IOWriterHandler
    func SimpleWithLevels(out io.Writer, levels []slog.Level) *IOWriterHandler


type SimpleHandler = IOWriterHandler
    func NewHandler(out io.Writer, maxLevel slog.Level) *SimpleHandler
    func NewSimple(out io.Writer, maxLevel slog.Level) *SimpleHandler

type SyncCloseHandler struct{ ... }
    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, maxLv ...slog.Level) *SyncCloseHandler
    func MustSizeRotateFile(logfile string, maxSize 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, maxLv ...slog.Level) (*SyncCloseHandler, error)
    func NewSimpleFileHandler(filePath string, maxLv ...slog.Level) (*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 NewSyncCloserWithLF(out SyncCloseWriter, lf slog.LevelFormattable) *SyncCloseHandler
    func NewTimeRotateFile(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
    func NewTimeRotateFileHandler(logfile string, rt rotatefile.RotateTime, fns ...ConfigFn) (*SyncCloseHandler, error)
    func SyncCloserWithLevels(out SyncCloseWriter, levels []slog.Level) *SyncCloseHandler
    func SyncCloserWithMaxLevel(out SyncCloseWriter, maxLevel slog.Level) *SyncCloseHandler

type SysLogHandler struct{ ... }
    func NewSysLogHandler(priority syslog.Priority, tag string) (*SysLogHandler, error)

type WriteCloserHandler struct{ ... }
    func NewWriteCloser(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler
    func NewWriteCloserHandler(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler
    func NewWriteCloserWithLF(out io.WriteCloser, lf slog.LevelFormattable) *WriteCloserHandler
    func WriteCloserWithLevels(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler
    func WriteCloserWithMaxLevel(out io.WriteCloser, maxLevel slog.Level) *WriteCloserHandler
Config Functions
type Builder struct{ ... }
    func NewBuilder() *Builder
type Config struct{ ... }
    func NewConfig(fns ...ConfigFn) *Config
    func NewEmptyConfig(fns ...ConfigFn) *Config
type ConfigFn func(c *Config)
    func WithBackupNum(n uint) ConfigFn
    func WithBackupTime(bt uint) ConfigFn
    func WithBuffMode(buffMode string) ConfigFn
    func WithBuffSize(buffSize int) ConfigFn
    func WithCompress(compress bool) ConfigFn
    func WithFilePerm(filePerm fs.FileMode) ConfigFn
    func WithLevelMode(mode slog.LevelMode) ConfigFn
    func WithLevelNames(names []string) ConfigFn
    func WithLogLevel(level slog.Level) ConfigFn
    func WithLogLevels(levels slog.Levels) ConfigFn
    func WithLogfile(logfile string) ConfigFn
    func WithMaxSize(maxSize uint64) ConfigFn
    func WithRotateMode(m rotatefile.RotateMode) ConfigFn
    func WithRotateTime(rt rotatefile.RotateTime) ConfigFn
    func WithUseJSON(useJSON bool) ConfigFn

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

Examples

Constants

View Source
const (
	BuffModeLine = "line"
	BuffModeBite = "bite"
)

the buff mode consts

View Source
const (
	// LevelModeList use level list for limit record write
	LevelModeList = slog.LevelModeList
	// LevelModeValue use max level limit log record write
	LevelModeValue = slog.LevelModeMax
)
View Source
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

View Source
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
)
View Source
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 LineBuffOsFile added in v0.3.0

func LineBuffOsFile(f *os.File, bufSize int, levels []slog.Level) slog.Handler

LineBuffOsFile handler

func LineBuffWriter added in v0.3.0

func LineBuffWriter(w io.Writer, bufSize int, levels []slog.Level) slog.Handler

LineBuffWriter handler

func LineBufferedFile added in v0.3.0

func LineBufferedFile(logfile string, bufSize int, levels []slog.Level) (slog.Handler, error)

LineBufferedFile handler

func QuickOpenFile added in v0.1.2

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

QuickOpenFile like os.OpenFile

Types

type Builder added in v0.3.0

type Builder struct {
	*Config
	Output io.Writer
}

Builder struct for create handler

func NewBuilder added in v0.3.0

func NewBuilder() *Builder

NewBuilder create

func (*Builder) Build added in v0.3.0

func (b *Builder) Build() slog.FormattableHandler

Build slog handler.

func (*Builder) With deprecated added in v0.3.0

func (b *Builder) With(fns ...ConfigFn) *Builder

With some config fn

Deprecated: please use WithConfigFn()

func (*Builder) WithBuffMode added in v0.3.3

func (b *Builder) WithBuffMode(bufMode string) *Builder

WithBuffMode setting

func (*Builder) WithBuffSize added in v0.3.3

func (b *Builder) WithBuffSize(bufSize int) *Builder

WithBuffSize setting

func (*Builder) WithCompress added in v0.3.3

func (b *Builder) WithCompress(compress bool) *Builder

WithCompress setting

func (*Builder) WithConfigFn added in v0.5.0

func (b *Builder) WithConfigFn(fns ...ConfigFn) *Builder

WithConfigFn some config fn

func (*Builder) WithLevelMode added in v0.5.0

func (b *Builder) WithLevelMode(mode slog.LevelMode) *Builder

WithLevelMode setting

func (*Builder) WithLogLevel added in v0.5.0

func (b *Builder) WithLogLevel(level slog.Level) *Builder

WithLogLevel setting

func (*Builder) WithLogLevels added in v0.3.3

func (b *Builder) WithLogLevels(levels []slog.Level) *Builder

WithLogLevels setting

func (*Builder) WithLogfile added in v0.3.3

func (b *Builder) WithLogfile(logfile string) *Builder

WithLogfile setting

func (*Builder) WithMaxSize added in v0.3.3

func (b *Builder) WithMaxSize(maxSize uint64) *Builder

WithMaxSize setting

func (*Builder) WithOutput added in v0.3.0

func (b *Builder) WithOutput(w io.Writer) *Builder

WithOutput to the builder

func (*Builder) WithRotateTime added in v0.3.3

func (b *Builder) WithRotateTime(rt rotatefile.RotateTime) *Builder

WithRotateTime setting

func (*Builder) WithUseJSON added in v0.3.3

func (b *Builder) WithUseJSON(useJSON bool) *Builder

WithUseJSON setting

type Config added in v0.3.0

type Config struct {
	// Logfile for write logs
	Logfile string `json:"logfile" yaml:"logfile"`

	// FilePerm for create log file. default rotatefile.DefaultFilePerm
	FilePerm fs.FileMode `json:"file_perm" yaml:"file_perm"`

	// LevelMode for limit log records. default LevelModeList
	LevelMode slog.LevelMode `json:"level_mode" yaml:"level_mode"`

	// Level max value. valid on LevelMode = LevelModeValue
	Level slog.Level `json:"level" yaml:"level"`

	// Levels list for write. valid on LevelMode = LevelModeList
	Levels []slog.Level `json:"levels" yaml:"levels"`

	// 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, unit is bytes. set 0 to disable buffer
	BuffSize int `json:"buff_size" yaml:"buff_size"`

	// RotateTime for rotate file, unit is seconds.
	RotateTime rotatefile.RotateTime `json:"rotate_time" yaml:"rotate_time"`

	// RotateMode for rotate file by time. default rotatefile.ModeRename
	RotateMode rotatefile.RotateMode `json:"rotate_mode" yaml:"rotate_mode"`

	// MaxSize on rotate file by size, unit is bytes.
	MaxSize uint64 `json:"max_size" yaml:"max_size"`

	// Compress determines if the rotated log files should be compressed using gzip.
	// The default is not to perform compression.
	Compress bool `json:"compress" yaml:"compress"`

	// BackupNum max number for keep old files.
	//
	// 0 is not limit, default is 20.
	BackupNum uint `json:"backup_num" yaml:"backup_num"`

	// BackupTime max time for keep old files, unit is hours.
	//
	// 0 is not limit, default is a week.
	BackupTime uint `json:"backup_time" yaml:"backup_time"`

	// RenameFunc build filename for rotate file
	RenameFunc func(filepath string, rotateNum uint) string

	// DebugMode for debug on development.
	DebugMode bool
}

Config struct

func NewConfig added in v0.3.0

func NewConfig(fns ...ConfigFn) *Config

NewConfig new config instance with some default settings.

func NewEmptyConfig added in v0.3.0

func NewEmptyConfig(fns ...ConfigFn) *Config

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

func (*Config) With added in v0.3.0

func (c *Config) With(fns ...ConfigFn) *Config

With more config settings func

func (*Config) WithConfigFn added in v0.5.0

func (c *Config) WithConfigFn(fns ...ConfigFn) *Config

WithConfigFn more config settings func

type ConfigFn added in v0.3.0

type ConfigFn func(c *Config)

ConfigFn for config some settings

func WithBackupNum added in v0.5.0

func WithBackupNum(n uint) ConfigFn

WithBackupNum setting

func WithBackupTime added in v0.5.0

func WithBackupTime(bt uint) ConfigFn

WithBackupTime setting

func WithBuffMode added in v0.3.0

func WithBuffMode(buffMode string) ConfigFn

WithBuffMode setting

func WithBuffSize added in v0.3.0

func WithBuffSize(buffSize int) ConfigFn

WithBuffSize setting

func WithCompress added in v0.3.2

func WithCompress(compress bool) ConfigFn

WithCompress setting

func WithFilePerm added in v0.5.2

func WithFilePerm(filePerm fs.FileMode) ConfigFn

WithFilePerm setting

func WithLevelMode added in v0.5.0

func WithLevelMode(mode slog.LevelMode) ConfigFn

WithLevelMode setting

func WithLevelNames added in v0.5.0

func WithLevelNames(names []string) ConfigFn

WithLevelNames set levels by level names.

func WithLogLevel added in v0.5.0

func WithLogLevel(level slog.Level) ConfigFn

WithLogLevel setting

func WithLogLevels added in v0.3.0

func WithLogLevels(levels slog.Levels) ConfigFn

WithLogLevels setting

func WithLogfile added in v0.3.0

func WithLogfile(logfile string) ConfigFn

WithLogfile setting

func WithMaxSize added in v0.3.0

func WithMaxSize(maxSize uint64) ConfigFn

WithMaxSize setting

func WithRotateMode added in v0.5.2

func WithRotateMode(m rotatefile.RotateMode) ConfigFn

WithRotateMode setting

func WithRotateTime added in v0.3.0

func WithRotateTime(rt rotatefile.RotateTime) ConfigFn

WithRotateTime setting

func WithUseJSON added in v0.3.0

func WithUseJSON(useJSON bool) ConfigFn

WithUseJSON setting

type ConsoleHandler

type ConsoleHandler = IOWriterHandler

ConsoleHandler definition

func ConsoleWithLevels added in v0.5.3

func ConsoleWithLevels(levels []slog.Level) *ConsoleHandler

ConsoleWithLevels create new ConsoleHandler and with limited log levels

func ConsoleWithMaxLevel added in v0.5.3

func ConsoleWithMaxLevel(level slog.Level) *ConsoleHandler

ConsoleWithMaxLevel create new ConsoleHandler and with max log level

func NewConsole added in v0.1.1

func NewConsole(levels []slog.Level) *ConsoleHandler

NewConsole create new ConsoleHandler, alias of NewConsoleHandler

func NewConsoleHandler

func NewConsoleHandler(levels []slog.Level) *ConsoleHandler

NewConsoleHandler create new ConsoleHandler with limited log levels

func NewConsoleWithLF added in v0.5.3

func NewConsoleWithLF(lf slog.LevelFormattable) *ConsoleHandler

NewConsoleWithLF create new ConsoleHandler and with custom slog.LevelFormattable

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

func (*EmailHandler) Handle added in v0.1.1

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

Handle a log record

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 FlushCloserWithLevels added in v0.5.3

func FlushCloserWithLevels(out FlushCloseWriter, levels []slog.Level) *FlushCloseHandler

FlushCloserWithLevels create new FlushCloseHandler, alias of NewFlushCloseHandler()

func FlushCloserWithMaxLevel added in v0.5.3

func FlushCloserWithMaxLevel(out FlushCloseWriter, maxLevel slog.Level) *FlushCloseHandler

FlushCloserWithMaxLevel create new FlushCloseHandler, with max log level

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, alias of NewFlushCloseHandler()

func NewFlushCloserWithLF added in v0.5.3

func NewFlushCloserWithLF(out FlushCloseWriter, lf slog.LevelFormattable) *FlushCloseHandler

NewFlushCloserWithLF create new FlushCloseHandler, with custom slog.LevelFormattable

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

func (*FlushCloseHandler) Handle added in v0.3.0

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

Handle log record

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

type FlushWriter interface {
	Flush() error
	// Writer the output writer
	io.Writer
}

FlushWriter is the interface satisfied by logging destinations.

type FormatWriterHandler added in v0.5.3

type FormatWriterHandler interface {
	slog.Handler
	// Formatter record formatter
	Formatter() slog.Formatter
	// Writer the output writer
	Writer() io.Writer
}

FormatWriterHandler interface

type IOWriterHandler added in v0.1.2

type IOWriterHandler struct {
	NopFlushClose
	slog.LevelFormattable
	Output io.Writer
}

IOWriterHandler definition

func IOWriterWithLevels added in v0.5.3

func IOWriterWithLevels(out io.Writer, levels []slog.Level) *IOWriterHandler

IOWriterWithLevels create a new instance and with limited log levels

func IOWriterWithMaxLevel added in v0.5.3

func IOWriterWithMaxLevel(out io.Writer, maxLevel slog.Level) *IOWriterHandler

IOWriterWithMaxLevel create new IOWriterHandler, with max log level

Usage:

	buf := new(bytes.Buffer)
	h := handler.IOWriterWithMaxLevel(buf, slog.InfoLevel)
 slog.AddHandler(h)
	slog.Info("info message")

func NewIOWriter added in v0.1.2

func NewIOWriter(out io.Writer, levels []slog.Level) *IOWriterHandler

NewIOWriter create a new instance and with limited log levels

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 NewIOWriterWithLF added in v0.5.3

func NewIOWriterWithLF(out io.Writer, lf slog.LevelFormattable) *IOWriterHandler

NewIOWriterWithLF create new IOWriterHandler, with custom slog.LevelFormattable

func NewSimpleHandler added in v0.3.0

func NewSimpleHandler(out io.Writer, maxLevel slog.Level) *IOWriterHandler

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)

func SimpleWithLevels added in v0.5.3

func SimpleWithLevels(out io.Writer, levels []slog.Level) *IOWriterHandler

SimpleWithLevels create new simple handler, with log levels

func (*IOWriterHandler) Handle added in v0.1.2

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

Handle log record

func (*IOWriterHandler) TextFormatter added in v0.5.3

func (h *IOWriterHandler) TextFormatter() *slog.TextFormatter

TextFormatter get the formatter

type LevelWithFormatter deprecated added in v0.0.3

type LevelWithFormatter = slog.LevelWithFormatter

LevelWithFormatter struct definition

- support set log formatter - only support set one log level

Deprecated: please use slog.LevelWithFormatter instead.

type LevelsWithFormatter deprecated added in v0.0.3

type LevelsWithFormatter = slog.LevelsWithFormatter

LevelsWithFormatter struct definition

- support set log formatter - support setting multi log levels

Deprecated: please use slog.LevelsWithFormatter instead.

type LockWrapper added in v0.3.0

type LockWrapper struct {
	sync.Mutex
	// contains filtered or unexported fields
}

LockWrapper struct

func (*LockWrapper) EnableLock added in v0.3.0

func (lw *LockWrapper) EnableLock(enable bool)

EnableLock enable lock

func (*LockWrapper) Lock added in v0.3.0

func (lw *LockWrapper) Lock()

Lock it

func (*LockWrapper) LockEnabled added in v0.3.0

func (lw *LockWrapper) LockEnabled() bool

LockEnabled status

func (*LockWrapper) Unlock added in v0.3.0

func (lw *LockWrapper) Unlock()

Unlock it

type NopFlushClose added in v0.1.1

type NopFlushClose struct{}

NopFlushClose no operation.

provide empty Flush(), Close() methods, useful for tests.

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 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 = IOWriterHandler

SimpleHandler definition. alias of IOWriterHandler

func NewHandler added in v0.3.0

func NewHandler(out io.Writer, maxLevel slog.Level) *SimpleHandler

NewHandler create a new instance

func NewSimple added in v0.3.0

func NewSimple(out io.Writer, maxLevel slog.Level) *SimpleHandler

NewSimple create a new instance

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, will panic on create error

func MustSimpleFile added in v0.1.1

func MustSimpleFile(filepath string, maxLv ...slog.Level) *SyncCloseHandler

MustSimpleFile new instance

func MustSizeRotateFile added in v0.1.1

func MustSizeRotateFile(logfile string, maxSize 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. alias of NewRotateFileHandler()

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, maxLv ...slog.Level) (*SyncCloseHandler, error)

NewSimpleFile new instance

func NewSimpleFileHandler added in v0.1.0

func NewSimpleFileHandler(filePath string, maxLv ...slog.Level) (*SyncCloseHandler, error)

NewSimpleFileHandler instance, default log level is InfoLevel

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, default close rotate by time.

func NewSyncCloseHandler added in v0.3.0

func NewSyncCloseHandler(out SyncCloseWriter, levels []slog.Level) *SyncCloseHandler

NewSyncCloseHandler create new SyncCloseHandler with limited log levels

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, alias of NewSyncCloseHandler()

func NewSyncCloserWithLF added in v0.5.3

func NewSyncCloserWithLF(out SyncCloseWriter, lf slog.LevelFormattable) *SyncCloseHandler

NewSyncCloserWithLF create new SyncCloseHandler, with custom slog.LevelFormattable

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, default close rotate by size

func SyncCloserWithLevels added in v0.5.3

func SyncCloserWithLevels(out SyncCloseWriter, levels []slog.Level) *SyncCloseHandler

SyncCloserWithLevels create new SyncCloseHandler, alias of NewSyncCloseHandler()

func SyncCloserWithMaxLevel added in v0.5.3

func SyncCloserWithMaxLevel(out SyncCloseWriter, maxLevel slog.Level) *SyncCloseHandler

SyncCloserWithMaxLevel create new SyncCloseHandler, with max log level

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 NewSysLog added in v0.0.3

func NewSysLog(opt *SysLogOpt) (*SysLogHandler, error)

NewSysLog handler instance with all custom options.

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

Close handler

func (*SysLogHandler) Flush added in v0.1.0

func (h *SysLogHandler) Flush() error

Flush handler

func (*SysLogHandler) Handle added in v0.1.0

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

Handle a log record

type SysLogOpt added in v0.5.5

type SysLogOpt struct {
	// Tag syslog tag
	Tag string
	// Priority syslog priority
	Priority syslog.Priority
	// Network syslog network
	Network string
	// Raddr syslog address
	Raddr string
}

SysLogOpt for syslog handler

type WriteCloserHandler added in v0.3.0

type WriteCloserHandler struct {
	slog.LevelFormattable
	Output io.WriteCloser
}

WriteCloserHandler definition

func NewWriteCloser added in v0.3.0

func NewWriteCloser(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler

NewWriteCloser create a new instance

func NewWriteCloserHandler added in v0.5.3

func NewWriteCloserHandler(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler

NewWriteCloserHandler 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 NewWriteCloserWithLF added in v0.5.3

func NewWriteCloserWithLF(out io.WriteCloser, lf slog.LevelFormattable) *WriteCloserHandler

NewWriteCloserWithLF create new WriteCloserHandler and with custom slog.LevelFormattable

func WriteCloserWithLevels added in v0.5.3

func WriteCloserWithLevels(out io.WriteCloser, levels []slog.Level) *WriteCloserHandler

WriteCloserWithLevels create a new instance and with limited log levels

func WriteCloserWithMaxLevel added in v0.5.3

func WriteCloserWithMaxLevel(out io.WriteCloser, maxLevel slog.Level) *WriteCloserHandler

WriteCloserWithMaxLevel create new WriteCloserHandler and with max log level

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

func (*WriteCloserHandler) Handle added in v0.3.0

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

Handle log record

Jump to

Keyboard shortcuts

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