Documentation ¶
Index ¶
- Variables
- func Caller(skip int) (file string, line int)
- func NewFileWriter(path string) (*os.File, error)
- func SetInternalLogger(l *Logger)
- func StartFastTimer()
- func StopFastTimer()
- type BufferedFileWriter
- type BufferedFileWriterOption
- type ByteFormatPart
- type BytesFormatPart
- type ConcurrentFileWriter
- type ConsoleWriter
- type DateFormatPart
- type DiscardWriter
- type FastTimer
- type FormatPart
- type Formatter
- type FullSourceFormatPart
- type Handler
- type Level
- type LevelFormatPart
- type Logger
- func (l *Logger) AddHandler(h *Handler)
- func (l *Logger) Close()
- func (l *Logger) Crit(args ...interface{})
- func (l *Logger) Critf(msg string, args ...interface{})
- func (l *Logger) Debug(args ...interface{})
- func (l *Logger) Debugf(msg string, args ...interface{})
- func (l *Logger) Error(args ...interface{})
- func (l *Logger) Errorf(msg string, args ...interface{})
- func (l *Logger) GetMinLevel() Level
- func (l *Logger) Info(args ...interface{})
- func (l *Logger) Infof(msg string, args ...interface{})
- func (l *Logger) IsEnabledFor(level Level) bool
- func (l *Logger) Log(lv Level, file string, line int, msg string, args ...interface{})
- func (l *Logger) Warn(args ...interface{})
- func (l *Logger) Warnf(msg string, args ...interface{})
- type MessageFormatPart
- type Record
- type RotateDuration
- type RotatingFileWriter
- type SourceFormatPart
- type TimeFormatPart
- type TimedRotatingFileWriter
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultFormatter is the default formatter DefaultFormatter = ParseFormat("[%l %D %T %s] %m") // TimedRotatingFormatter is a formatter for TimedRotatingFileWriter TimedRotatingFormatter = ParseFormat("[%l %T %s] %m") )
Functions ¶
func Caller ¶
Caller caches the result for runtime.Caller(). Inspired by https://zhuanlan.zhihu.com/p/403417640
func NewFileWriter ¶
NewFileWriter creates a FileWriter by its path.
func SetInternalLogger ¶
func SetInternalLogger(l *Logger)
SetInternalLogger sets a logger as the internalLogger which is used to log internal errors. The logger and its handlers will be marked as internal, so do not reuse them. The internalLogger may discard its own errors to prevent recursive log.
Types ¶
type BufferedFileWriter ¶
type BufferedFileWriter struct {
// contains filtered or unexported fields
}
A BufferedFileWriter is a buffered file writer. The written bytes will be flushed to the log file every 0.1 second, or when reaching the buffer capacity (4 MB).
func NewBufferedFileWriter ¶
func NewBufferedFileWriter(path string, options ...BufferedFileWriterOption) (*BufferedFileWriter, error)
NewBufferedFileWriter creates a new BufferedFileWriter.
func (*BufferedFileWriter) Close ¶
func (w *BufferedFileWriter) Close() error
Close flushes the buffer, then closes the file writer.
type BufferedFileWriterOption ¶
type BufferedFileWriterOption func(*bufferedFileWriter)
func BufferSize ¶
func BufferSize(size uint32) BufferedFileWriterOption
BufferSize sets the buffer size.
type ByteFormatPart ¶
type ByteFormatPart struct {
// contains filtered or unexported fields
}
ByteFormatPart is a FormatPart containing a byte.
type BytesFormatPart ¶
type BytesFormatPart struct {
// contains filtered or unexported fields
}
BytesFormatPart is a FormatPart containing a byte slice.
type ConcurrentFileWriter ¶
type ConcurrentFileWriter struct {
// contains filtered or unexported fields
}
func NewConcurrentFileWriter ¶
func NewConcurrentFileWriter(path string, options ...BufferedFileWriterOption) (*ConcurrentFileWriter, error)
NewBufferedFileWriter creates a new BufferedFileWriter.
func (*ConcurrentFileWriter) Close ¶
func (w *ConcurrentFileWriter) Close() (err error)
Close flushes the buffer, then closes the file writer.
type ConsoleWriter ¶
A ConsoleWriter is a writer which should not be actually closed.
func NewConsoleWriter ¶
func NewConsoleWriter(f *os.File) *ConsoleWriter
NewConsoleWriter creates a new ConsoleWriter.
func NewStderrWriter ¶
func NewStderrWriter() *ConsoleWriter
NewStderrWriter creates a new stderr writer.
func NewStdoutWriter ¶
func NewStdoutWriter() *ConsoleWriter
NewStdoutWriter creates a new stdout writer.
type DateFormatPart ¶
type DateFormatPart struct{}
DateFormatPart is a FormatPart of the date placeholder.
type DiscardWriter ¶
DiscardWriter is a WriteCloser which write everything to devNull
func NewDiscardWriter ¶
func NewDiscardWriter() *DiscardWriter
NewDiscardWriter creates a new ConsoleWriter.
type FastTimer ¶
type FastTimer struct {
// contains filtered or unexported fields
}
FastTimer is not thread-safe for performance reason, but all the threads will notice its changes in a few milliseconds.
type FormatPart ¶
FormatPart is an interface containing the Format() method.
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
A Formatter containing a sequence of FormatParts.
func ParseFormat ¶
ParseFormat parses a format string into a formatter.
type FullSourceFormatPart ¶
type FullSourceFormatPart struct{}
FullSourceFormatPart is a FormatPart of the full source code placeholder.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
A Handler is a leveled log handler with a formatter and several writers.
func NewHandler ¶
NewHandler creates a new Handler of the given level with the formatter. Records with the lower level than the handler will be ignored.
func (*Handler) AddWriter ¶
func (h *Handler) AddWriter(w io.WriteCloser)
AddWriter adds a writer to the Handler. The Write() method of the writer should be thread-safe.
func (*Handler) Close ¶
func (h *Handler) Close()
Close closes all its writers. It's safe to call this method more than once, but it's unsafe to call its writers' Close() more than once.
func (*Handler) Handle ¶
Handle formats a record using its formatter, then writes the formatted result to all of its writers. Returns true if it can handle the record. The errors during writing will be logged by the internalLogger. It's not thread-safe, concurrent record may be written in a random order through different writers. But two records won't be mixed in a single line.
type LevelFormatPart ¶
type LevelFormatPart struct{}
LevelFormatPart is a FormatPart of the level placeholder.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger is a leveled logger with several handlers.
func NewLogger ¶
NewLogger creates a new Logger of the given level. Messages with lower level than the logger will be ignored.
func NewLoggerWithWriter ¶
func NewLoggerWithWriter(w io.WriteCloser) *Logger
NewLoggerWithWriter creates an info level logger with a writer.
func NewStderrLogger ¶
func NewStderrLogger() *Logger
NewStderrLogger creates a logger with a stderr writer.
func NewStdoutLogger ¶
func NewStdoutLogger() *Logger
NewStdoutLogger creates a logger with a stdout writer.
func (*Logger) AddHandler ¶
AddHandler adds a Handler to the Logger.
func (*Logger) Close ¶
func (l *Logger) Close()
Close closes its handlers. It's safe to call this method more than once.
func (*Logger) Crit ¶
func (l *Logger) Crit(args ...interface{})
Crit logs a critical level message. It uses fmt.Fprint() to format args.
func (*Logger) Critf ¶
Critf logs a critical level message. It uses fmt.Fprintf() to format msg and args.
func (*Logger) Debug ¶
func (l *Logger) Debug(args ...interface{})
Debug logs a debug level message. It uses fmt.Fprint() to format args.
func (*Logger) Debugf ¶
Debugf logs a debug level message. It uses fmt.Fprintf() to format msg and args.
func (*Logger) Error ¶
func (l *Logger) Error(args ...interface{})
Error logs an error level message. It uses fmt.Fprint() to format args.
func (*Logger) Errorf ¶
Errorf logs a error level message. It uses fmt.Fprintf() to format msg and args.
func (*Logger) GetMinLevel ¶
GetMinLevel returns its minLevel. Records lower than its minLevel will be ignored.
func (*Logger) Info ¶
func (l *Logger) Info(args ...interface{})
Info logs a info level message. It uses fmt.Fprint() to format args.
func (*Logger) Infof ¶
Infof logs a info level message. It uses fmt.Fprintf() to format msg and args.
func (*Logger) IsEnabledFor ¶
IsEnabledFor returns whether it's enabled for the level.
func (*Logger) Log ¶
Log logs a message with context. A logger should check the message level before call its Log(). The line param should be uint32. It's not thread-safe, concurrent messages may be written in a random order through different handlers or writers. But two messages won't be mixed in a single line.
type MessageFormatPart ¶
type MessageFormatPart struct{}
MessageFormatPart is a FormatPart of the message placeholder.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
A Record is an item which contains required context for the logger.
type RotateDuration ¶
type RotateDuration uint8
RotateDuration specifies rotate duration type, should be either RotateByDate or RotateByHour.
const ( // RotateByDate set the log file to be rotated each day. RotateByDate RotateDuration = iota // RotateByHour set the log file to be rotated each hour. RotateByHour )
type RotatingFileWriter ¶
type RotatingFileWriter struct { BufferedFileWriter // contains filtered or unexported fields }
A RotatingFileWriter is a buffered file writer which will rotate before reaching its maxSize. An exception is when a record is larger than maxSize, it won't be separated into 2 files. It keeps at most backupCount backups.
func NewRotatingFileWriter ¶
func NewRotatingFileWriter(path string, maxSize uint64, backupCount uint8, options ...BufferedFileWriterOption) (*RotatingFileWriter, error)
NewRotatingFileWriter creates a new RotatingFileWriter.
type SourceFormatPart ¶
type SourceFormatPart struct{}
SourceFormatPart is a FormatPart of the source code placeholder.
type TimeFormatPart ¶
type TimeFormatPart struct{}
TimeFormatPart is a FormatPart of the time placeholder.
type TimedRotatingFileWriter ¶
type TimedRotatingFileWriter struct { BufferedFileWriter // contains filtered or unexported fields }
A TimedRotatingFileWriter is a buffered file writer which will rotate by time. Its rotateDuration can be either RotateByDate or RotateByHour. It keeps at most backupCount backups.
func NewTimedRotatingFileWriter ¶
func NewTimedRotatingFileWriter(pathPrefix string, rotateDuration RotateDuration, backupCount uint8, options ...BufferedFileWriterOption) (*TimedRotatingFileWriter, error)
NewTimedRotatingFileWriter creates a new TimedRotatingFileWriter.