golog: github.com/keakon/golog Index | Files | Directories

package golog

import "github.com/keakon/golog"

Index

Package Files

formatter.go handler.go logger.go utils.go writer.go

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

func NewFileWriter Uses

func NewFileWriter(path string) (*os.File, error)

NewFileWriter creates a FileWriter by its path.

func SetInternalLogger Uses

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.

type BufferedFileWriter Uses

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 Uses

func NewBufferedFileWriter(path string) (*BufferedFileWriter, error)

NewBufferedFileWriter creates a new BufferedFileWriter.

func (*BufferedFileWriter) Close Uses

func (w *BufferedFileWriter) Close() error

Close flushes the buffer, then closes the file writer.

func (*BufferedFileWriter) Write Uses

func (w *BufferedFileWriter) Write(p []byte) (n int, err error)

Write writes a byte slice to the buffer.

type ByteFormatPart Uses

type ByteFormatPart struct {
    // contains filtered or unexported fields
}

ByteFormatPart is a FormatPart containing a byte.

func (*ByteFormatPart) Format Uses

func (p *ByteFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes its byte to the buf.

type BytesFormatPart Uses

type BytesFormatPart struct {
    // contains filtered or unexported fields
}

BytesFormatPart is a FormatPart containing a byte slice.

func (*BytesFormatPart) Format Uses

func (p *BytesFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes its bytes to the buf.

type ConsoleWriter Uses

type ConsoleWriter struct {
    *os.File // faster than io.Writer
}

A ConsoleWriter is a writer which should not be acturelly closed.

func NewConsoleWriter Uses

func NewConsoleWriter(f *os.File) *ConsoleWriter

NewConsoleWriter creates a new ConsoleWriter.

func NewStderrWriter Uses

func NewStderrWriter() *ConsoleWriter

NewStderrWriter creates a new stderr writer.

func NewStdoutWriter Uses

func NewStdoutWriter() *ConsoleWriter

NewStdoutWriter creates a new stdout writer.

func (*ConsoleWriter) Close Uses

func (w *ConsoleWriter) Close() error

Close sets its File to nil.

type DateFormatPart Uses

type DateFormatPart struct{}

DateFormatPart is a FormatPart of the date placeholder.

func (*DateFormatPart) Format Uses

func (p *DateFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes the date string of the record to the buf.

type DiscardWriter Uses

type DiscardWriter struct {
    io.Writer
}

DiscardWriter is a WriteCloser which write everything to devNull

func NewDiscardWriter Uses

func NewDiscardWriter() *DiscardWriter

NewDiscardWriter creates a new ConsoleWriter.

func (*DiscardWriter) Close Uses

func (w *DiscardWriter) Close() error

Close sets its Writer to nil.

type FormatPart Uses

type FormatPart interface {
    Format(r *Record, buf *bytes.Buffer)
}

FormatPart is an interface containing the Format() method.

type Formatter Uses

type Formatter struct {
    // contains filtered or unexported fields
}

A Formatter containing a sequence of FormatParts.

func ParseFormat Uses

func ParseFormat(format string) (formatter *Formatter)

ParseFormat parses a format string into a formatter.

func (*Formatter) Format Uses

func (f *Formatter) Format(r *Record, buf *bytes.Buffer)

Format formats a record to a bytes.Buffer. Supported format verbs:

%%: %
%l: short name of the level
%T: time string (HH:MM:SS)
%D: date string (YYYY-mm-DD)
%s: source code string (filename:line)
%S: full source code string (/path/filename.go:line)

type FullSourceFormatPart Uses

type FullSourceFormatPart struct{}

FullSourceFormatPart is a FormatPart of the full source code placeholder.

func (*FullSourceFormatPart) Format Uses

func (p *FullSourceFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes the source file path and line number of the record to the buf.

type Handler Uses

type Handler struct {
    // contains filtered or unexported fields
}

A Handler is a leveled log handler with a formatter and several writers.

func NewHandler Uses

func NewHandler(level Level, formatter *Formatter) *Handler

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 Uses

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 Uses

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 Uses

func (h *Handler) Handle(r *Record) bool

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 Level Uses

type Level uint8

Level specifies the log level.

const (
    DebugLevel Level = iota
    InfoLevel
    WarnLevel
    ErrorLevel
    CritLevel
)

All the log levels.

type LevelFormatPart Uses

type LevelFormatPart struct{}

LevelFormatPart is a FormatPart of the level placeholder.

func (*LevelFormatPart) Format Uses

func (p *LevelFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes the short level name of the record to the buf.

type Logger Uses

type Logger struct {
    // contains filtered or unexported fields
}

A Logger is a leveled logger with several handlers.

func NewLogger Uses

func NewLogger(lv Level) *Logger

NewLogger creates a new Logger of the given level. Messages with lower level than the logger will be ignored.

func NewLoggerWithWriter Uses

func NewLoggerWithWriter(w io.WriteCloser) *Logger

NewLoggerWithWriter creates an info level logger with a writer.

func NewStderrLogger Uses

func NewStderrLogger() *Logger

NewStderrLogger creates a logger with a stderr writer.

func NewStdoutLogger Uses

func NewStdoutLogger() *Logger

NewStdoutLogger creates a logger with a stdout writer.

func (*Logger) AddHandler Uses

func (l *Logger) AddHandler(h *Handler)

AddHandler adds a Handler to the Logger. A handler with lower level than the logger will be ignored.

func (*Logger) Close Uses

func (l *Logger) Close()

Close closes its handlers. It's safe to call this method more than once.

func (*Logger) Crit Uses

func (l *Logger) Crit(args ...interface{})

Crit logs a critical level message. It uses fmt.Fprint() to format args.

func (*Logger) Critf Uses

func (l *Logger) Critf(msg string, args ...interface{})

Critf logs a critical level message. It uses fmt.Fprintf() to format msg and args.

func (*Logger) Debug Uses

func (l *Logger) Debug(args ...interface{})

Debug logs a debug level message. It uses fmt.Fprint() to format args.

func (*Logger) Debugf Uses

func (l *Logger) Debugf(msg string, args ...interface{})

Debugf logs a debug level message. It uses fmt.Fprintf() to format msg and args.

func (*Logger) Error Uses

func (l *Logger) Error(args ...interface{})

Error logs an error level message. It uses fmt.Fprint() to format args.

func (*Logger) Errorf Uses

func (l *Logger) Errorf(msg string, args ...interface{})

Errorf logs a error level message. It uses fmt.Fprintf() to format msg and args.

func (*Logger) GetMinLevel Uses

func (l *Logger) GetMinLevel() Level

GetMinLevel returns its minLevel. Records lower than its minLevel will be ignored.

func (*Logger) Info Uses

func (l *Logger) Info(args ...interface{})

Info logs a info level message. It uses fmt.Fprint() to format args.

func (*Logger) Infof Uses

func (l *Logger) Infof(msg string, args ...interface{})

Infof logs a info level message. It uses fmt.Fprintf() to format msg and args.

func (*Logger) IsEnabledFor Uses

func (l *Logger) IsEnabledFor(level Level) bool

IsEnabledFor returns whether it's enabled for the level

func (*Logger) Log Uses

func (l *Logger) Log(lv Level, file string, line int, msg string, args ...interface{})

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.

func (*Logger) Warn Uses

func (l *Logger) Warn(args ...interface{})

Warn logs a warning level message. It uses fmt.Fprint() to format args.

func (*Logger) Warnf Uses

func (l *Logger) Warnf(msg string, args ...interface{})

Warnf logs a warning level message. It uses fmt.Fprintf() to format msg and args.

type MessageFormatPart Uses

type MessageFormatPart struct{}

MessageFormatPart is a FormatPart of the message placeholder.

func (*MessageFormatPart) Format Uses

func (p *MessageFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes the formatted message with args to the buf.

type Record Uses

type Record struct {
    // contains filtered or unexported fields
}

A Record is an item which contains required context for the logger.

type RotateDuration Uses

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 Uses

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 Uses

func NewRotatingFileWriter(path string, maxSize uint64, backupCount uint8) (*RotatingFileWriter, error)

NewRotatingFileWriter creates a new RotatingFileWriter.

func (*RotatingFileWriter) Write Uses

func (w *RotatingFileWriter) Write(p []byte) (n int, err error)

Write writes a byte slice to the buffer and rotates if reaching its maxSize.

type SourceFormatPart Uses

type SourceFormatPart struct{}

SourceFormatPart is a FormatPart of the source code placeholder.

func (*SourceFormatPart) Format Uses

func (p *SourceFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes the source file name and line number of the record to the buf.

type TimeFormatPart Uses

type TimeFormatPart struct{}

TimeFormatPart is a FormatPart of the time placeholder.

func (*TimeFormatPart) Format Uses

func (p *TimeFormatPart) Format(r *Record, buf *bytes.Buffer)

Format writes the time string of the record to the buf.

type TimedRotatingFileWriter Uses

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 Uses

func NewTimedRotatingFileWriter(pathPrefix string, rotateDuration RotateDuration, backupCount uint8) (*TimedRotatingFileWriter, error)

NewTimedRotatingFileWriter creates a new TimedRotatingFileWriter.

Directories

PathSynopsis
log

Package golog imports 12 packages (graph) and is imported by 1 packages. Updated 2019-01-02. Refresh now. Tools for package owners.