Documentation
¶
Index ¶
- Variables
- func InnerPrint(format string, a ...interface{})
- func WrapWriter(w LevelWriter) io.Writer
- type BufioWriteCloser
- type Clock
- type Event
- type FileRotatedEvent
- type FlushWriteCloser
- type Handler
- type HandlerFunc
- type LevelWriter
- type Option
- type OptionFn
- func WithClock(c Clock) OptionFn
- func WithGzipAge(v time.Duration) OptionFn
- func WithHandler(v Handler) OptionFn
- func WithMaxAge(v time.Duration) OptionFn
- func WithMaxSize(v int64) OptionFn
- func WithRotateFullLayout(v string) OptionFn
- func WithRotateLayout(v string) OptionFn
- func WithTotalSizeCap(v int64) OptionFn
- type OptionFns
- type Rotate
- type WriterFormatter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // UTC is an object satisfying the Clock interface, which // returns the current time in UTC. UTC = clockFn(func() time.Time { return time.Now().UTC() }) // Local is an object satisfying the Clock interface, which // returns the current time in the local timezone. Local = clockFn(time.Now) )
nolint:gochecknoglobals
var GologDebug = func() bool { v := os.Getenv("GOLOG_DEBUG") v = strings.ToLower(v) switch v { case "on", "1", "yes", "true": return true default: return false } }()
GologDebug 加载后立即执行,此时还未获得设置的参数,在 startup 中获取
Functions ¶
func InnerPrint ¶
func InnerPrint(format string, a ...interface{})
func WrapWriter ¶
func WrapWriter(w LevelWriter) io.Writer
Types ¶
type BufioWriteCloser ¶
func NewBufioWriteCloser ¶
func NewBufioWriteCloser(w io.WriteCloser) *BufioWriteCloser
func (*BufioWriteCloser) Close ¶
func (b *BufioWriteCloser) Close() error
type FileRotatedEvent ¶
type FileRotatedEvent struct { PreviousFile string // previous filename CurrentFile string // current, new filename }
FileRotatedEvent is the event when file rotating occurred.
type FlushWriteCloser ¶
type FlushWriteCloser interface { io.WriteCloser Flush() error }
type Handler ¶
type Handler interface {
Handle(Event)
}
Handler defines the event handler interface.
type LevelWriter ¶
func WrapLevelWriter ¶
func WrapLevelWriter(w io.Writer) LevelWriter
type OptionFn ¶
type OptionFn func(*Rotate)
OptionFn is the option function prototype.
func WithClock ¶
WithClock creates a new Option that sets a clock that the Rotate object will use to determine the current time.
By default, Rotate.Local, which returns the current time in the local time zone, is used. If you would rather use UTC, use Rotate.UTC as the argument to this option, and pass it to the constructor.
func WithGzipAge ¶
WithGzipAge creates a new Option that sets the max age of a log file before it gets compressed in gzip.
func WithHandler ¶
WithHandler creates a new Option that specifies the Handler object that gets invoked when an event occurs. Currently, `FileRotated` event is supported.
func WithMaxAge ¶
WithMaxAge creates a new Option that sets the max age of a log file before it gets purged from the file system.
func WithMaxSize ¶
WithMaxSize set how much max size should a log file be rotated. e.g. 100*spec.MiB
func WithRotateFullLayout ¶
WithRotateFullLayout creates a layout of the final rotated file. eg. log/2006-01-02/file.log for daily rotation layout.
func WithRotateLayout ¶
WithRotateLayout creates a layout for the postfix of rotated file. eg. .2006-01-02 for daily rotation.
func WithTotalSizeCap ¶
WithTotalSizeCap 用来指定所有日志文件的总大小上限(可选), 例如设置为3GB的话,那么到了这个值,就会删除旧的日志.
type Rotate ¶
type Rotate struct {
// contains filtered or unexported fields
}
Rotate represents a log file that gets automatically rotated as you write to it.
func New ¶
New creates a new Rotate object. A logfile filename must be passed. Optional `Option` parameters may be passed.
Example ¶
package main import ( "fmt" "os" "github.com/bingoohuang/golog/pkg/rotate" "github.com/sirupsen/logrus" ) func main() { logDir, err := os.MkdirTemp("", "rotate_test") if err != nil { fmt.Println("could not create log directory ", err) return } logPath := fmt.Sprintf("%s/test.log", logDir) for i := 0; i < 2; i++ { writer, err := rotate.New(logPath) if err != nil { fmt.Println("Could not open log file ", err) return } n, err := writer.Write(logrus.InfoLevel, []byte("test")) if err != nil || n != 4 { fmt.Println("Write failed ", err, " number written ", n) return } err = writer.Close() if err != nil { fmt.Println("Close failed ", err) return } } files, err := os.ReadDir(logDir) if err != nil { fmt.Println("ReadDir failed ", err) return } for _, file := range files { info, _ := file.Info() fmt.Println(file.Name(), info.Size()) } err = os.RemoveAll(logDir) if err != nil { fmt.Println("RemoveAll failed ", err) return } }
func (*Rotate) Close ¶
Close satisfies the io.Closer interface. You must call this method if you performed any writes to the object.
func (*Rotate) CurrentFileName ¶
CurrentFileName returns the current file name that the Rotate object is writing to.
func (*Rotate) LogFile ¶
LogFile returns the current file name that the Rotate object is writing to.
func (*Rotate) Rotate ¶
Rotate forcefully rotates the log files. If the generated file name clash because file already exists, a numeric suffix of the form ".1", ".2", ".3" and so forth are appended to the end of the log file
This method can be used in conjunction with a signal handler so to emulate servers that generate new log files when they receive a SIGHUP.
type WriterFormatter ¶
type WriterFormatter struct { LevelWriter Formatter logrus.Formatter }
WriterFormatter is map for mapping a log level to an io.Writer. Multiple levels may share a writer, but multiple writers may not be used for one level.