Documentation
¶
Index ¶
- func TimeUnix(buf []byte) []byte
- func TimeUnixMicro(buf []byte) []byte
- func TimeUnixMilli(buf []byte) []byte
- func TimeUnixNano(buf []byte) []byte
- type Event
- func (event *Event) Bool(name string, value bool) *Event
- func (event *Event) Err(err error) *Event
- func (event *Event) Float(name string, value float64) *Event
- func (event *Event) Format(name, f string, args ...interface{}) *Event
- func (event *Event) Int(name string, value int) *Event
- func (event *Event) Int64(name string, value int64) *Event
- func (event *Event) Msg(s string) error
- func (event *Event) String(name, value string) *Event
- func (event *Event) Stringer(name string, stringer interface{ ... }) *Event
- func (event *Event) Uint(name string, value uint) *Event
- func (event *Event) Uint64(name string, value uint64) *Event
- type Intermediate
- func (il *Intermediate) Bool(name string, value bool) *Intermediate
- func (il *Intermediate) Float(name string, value float64) *Intermediate
- func (il *Intermediate) Format(name, f string, args ...interface{}) *Intermediate
- func (il *Intermediate) Int(name string, value int) *Intermediate
- func (il *Intermediate) Int64(name string, value int64) *Intermediate
- func (il *Intermediate) Logger() *Logger
- func (il *Intermediate) String(name, value string) *Intermediate
- func (il *Intermediate) Tracing(value bool) *Intermediate
- func (il *Intermediate) Uint(name string, value uint) *Intermediate
- func (il *Intermediate) Uint64(name string, value uint64) *Intermediate
- type Level
- type Logger
- func (log *Logger) Debug() *Event
- func (log *Logger) Error() *Event
- func (log *Logger) Info() *Event
- func (log *Logger) Log() *Event
- func (log *Logger) NewWriter(level Level) *Writer
- func (log *Logger) SetDebug() *Logger
- func (log *Logger) SetError() *Logger
- func (log *Logger) SetInfo() *Logger
- func (log *Logger) SetLevel(level Level) *Logger
- func (log *Logger) SetTimeFormatter(callback TimeFormatter) *Logger
- func (log *Logger) SetVerbose() *Logger
- func (log *Logger) SetWarning() *Logger
- func (log *Logger) SetWriter(w io.Writer) *Logger
- func (log *Logger) Verbose() *Event
- func (log *Logger) Warning() *Event
- func (log *Logger) With() *Intermediate
- type TimeFormatter
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TimeUnix ¶ added in v0.7.0
TimeUnix appends the current Unix second time to buf as a JSON property name and value.
func TimeUnixMicro ¶ added in v0.7.0
TimeUnixMicro appends the current Unix microsecond time to buf as a JSON property name and value.
func TimeUnixMilli ¶ added in v0.7.0
TimeUnixMilli appends the current Unix millisecond time to buf as a JSON property name and value.
func TimeUnixNano ¶ added in v0.7.0
TimeUnixNano appends the current Unix nanosecond time to buf as a JSON property name and value.
Types ¶
type Event ¶ added in v0.7.0
type Event struct {
// contains filtered or unexported fields
}
Event is an in progress log event being formatted before it is written upon calling its Msg() method. Callers never need to create an Event specifically, but rather receive an Event from calling Debug(), Verbose(), Info(), Warning(), or Error() methods of Logger instance.
func (*Event) Bool ¶ added in v0.7.0
Bool encodes a boolean property value to the Event using the specified name.
func (*Event) Err ¶ added in v0.9.0
Err encodes a possibly nil error property value to the Event. When err is nil, the error value is represented as a JSON null.
func (*Event) Float ¶ added in v0.7.0
Float encodes a float64 property value to the Event using the specified name.
func (*Event) Format ¶ added in v0.7.0
Format encodes a string property value--formatting it with the provided arguments--to the Event using the specified name. This function will invoke fmt.Sprintf() function to format the formatting string with the provided arguments, allocating memory to do so. If no formatting is required, invoking Event.String(string, string) will be faster.
func (*Event) Int ¶ added in v0.7.0
Int encodes a int property value to the Event using the specified name.
func (*Event) Int64 ¶ added in v0.8.0
Int64 encodes a int64 property value to the Event using the specified name.
func (*Event) Msg ¶ added in v0.7.0
Msg adds the specified message to the Event for the message property, and writes the Event to Logger's io.Writer. The caller may provide an empty string, which will elide inclusion of the message property in the written log event. This method must be invoked to complete every Event. This method returns any error from attempting to write to the Logger's io.Writer.
func (*Event) String ¶ added in v0.7.0
String encodes a string property value to the Event using the specified name.
func (*Event) Stringer ¶ added in v0.16.0
Stringer encodes the return value of a Stringer to the Event as a property value using the specified name. This method will result in allocation if and only if the Event will be logged.
To reduce program allocations, prefer this:
logger.Debug().Stringer("address", address).Msg("listening")
Rather than this:
logger.Debug().String("address", address.String()).Msg("listening")
type Intermediate ¶ added in v0.12.0
type Intermediate struct {
// contains filtered or unexported fields
}
Intermediate is an intermediate Logger that is not capable of logging events, but used while creating a new Logger that always includes one or more properties in each logged event.
Logger.With() -> *Intermediate -> Bool() -> *Intermediate -> ... -> Logger() -> *Logger
func (*Intermediate) Bool ¶ added in v0.12.0
func (il *Intermediate) Bool(name string, value bool) *Intermediate
Bool returns a new Intermediate Logger that has the name property set to the JSON encoded bool value.
func (*Intermediate) Float ¶ added in v0.12.0
func (il *Intermediate) Float(name string, value float64) *Intermediate
Float returns a new Intermediate Logger that has the name property set to the JSON encoded float64 value.
func (*Intermediate) Format ¶ added in v0.12.0
func (il *Intermediate) Format(name, f string, args ...interface{}) *Intermediate
Format returns a new Intermediate Logger that has the name property set to the JSON encoded string value derived from the formatted string and its arguments. This function will invoke fmt.Sprintf() function to format the formatting string with the provided arguments, allocating memory to do so. If no formatting is required, invoking Intermediate.String(string, string) will be faster.
func (*Intermediate) Int ¶ added in v0.12.0
func (il *Intermediate) Int(name string, value int) *Intermediate
Int returns a new Intermediate Logger that has the name property set to the JSON encoded int value.
func (*Intermediate) Int64 ¶ added in v0.12.0
func (il *Intermediate) Int64(name string, value int64) *Intermediate
Int64 returns a new Intermediate Logger that has the name property set to the JSON encoded int64 value.
func (*Intermediate) Logger ¶ added in v0.12.0
func (il *Intermediate) Logger() *Logger
Logger converts the Intermediate Logger into a new Logger instance that includes the fields it was configured to contain.
func (*Intermediate) String ¶ added in v0.12.0
func (il *Intermediate) String(name, value string) *Intermediate
String returns a new Intermediate Logger that has the name property set to the JSON encoded string value.
func (*Intermediate) Tracing ¶ added in v0.13.0
func (il *Intermediate) Tracing(value bool) *Intermediate
Tracing returns a new Intermediate Logger that logs all events, regardless of the Logger level at the time an log event is created.
func (*Intermediate) Uint ¶ added in v0.12.0
func (il *Intermediate) Uint(name string, value uint) *Intermediate
Uint returns a new Intermediate Logger that has the name property set to the JSON encoded uint value.
func (*Intermediate) Uint64 ¶ added in v0.12.0
func (il *Intermediate) Uint64(name string, value uint64) *Intermediate
Uint64 returns a new Intermediate Logger that has the name property set to the JSON encoded uint64 value.
type Level ¶
type Level uint32
Level type defines one of several possible log levels.
const ( // Debug is for events that might help a person understand the cause of a // bug in a program. Debug Level = iota // Verbose is for events that might help a person understand the state of a // program. Verbose // Info is for events that annotate high level status of a program. Info // Warning is for events that indicate a possible problem with the // program. Warning events should be investigated and corrected soon. Warning // Error is for events that indicate a definite problem that might prevent // normal program execution. Error events should be corrected immediately. Error )
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a near zero allocation logging mechanism. Each log event is written using a single invocation of the Write method for the underlying io.Writer.
Example ¶
package main import ( "fmt" "os" "strings" ) func main() { // A Logger needs a io.Writer to which it will write all log messages. log := New(os.Stdout) // By default, a Logger has a log level of Warning, which is closer to the // UNIX philosophy of avoiding unnecessary output. This example is // intended to be more verbose for demonstrative purposes. log.SetVerbose() // log.SetDebug() log.Verbose().Msg("initializing program") log.Log().String("foo", "bar").Msg("") // When creating structure instances, consider sending the log instance to // the structure's constructor so it can prefix its log messages // accordingly. This is especially useful when the instantiated structure // might spin off goroutines to perform tasks. s := NewServer(log) s.run([]string{"one=1", "@two=2", "three=3", "@four=4"}) // Create an io.Writer that conveys all writes it receives to the // underlying io.Writer as individual log events. NOTE: This logger will // not emit the first event, but inside the loop below the Writer's log // level is changed, such that follow on events are emitted. w := log.SetWarning().NewWriter(Info) for _, line := range []string{"line 1\n", "line 2\n", "line 3\n"} { n, err := w.Write([]byte(line)) if got, want := n, len(line); got != want { log.Warning().Int("got", got).Int("want", want).Msg("bytes written mismatch") } if err != nil { log.Warning().Err(err).Msg("error during write") } w.SetInfo() } } type Server struct { log *Logger // log is used for all log output by Server // plus any other fields... } func NewServer(log *Logger) *Server { // Sometimes it is helpful to know when enter and leave a function. log.Verbose().Msg("Enter NewServer()") // However, when deferring a log entry, ensure to call it from a composed // function. defer func() { log.Debug().Msg("Leave NewServer()") }() // When creating a new runtime subsystem, create a new branch of the // provided log to be used by that component. Each branch of the log may // have potentially different log levels. log = log.With().String("structure", "Server").Logger() return &Server{log: log} } func (a *Server) run(args []string) { a.log.Verbose().Msg("Enter Server.run()") // Create a local logger instance for this method. log := a.log.With().String("method", "run").Logger() log.Verbose().Msg("starting loop") for _, arg := range args { if err := a.handleRequest(log, arg); err != nil { log.Warning().Err(err).Msg("cannot handle request") } } } func (a *Server) handleRequest(log *Logger, raw string) error { log.Debug().Msg("Enter Server.handleRequest()") defer func() { log.Debug().Msg("Leave Server.handleRequest()") }() request, err := NewRequest(log, raw) if err != nil { return fmt.Errorf("cannot create request: %w", err) } if err = request.Handle(); err != nil { return fmt.Errorf("cannot process request: %w", err) } return nil } // Request is a demonstration structure that has its own logger, which it uses // to log all events relating to handling this request. type Request struct { log *Logger // Log is the logger for this particular request. Query string // Query is the request payload. } func NewRequest(log *Logger, query string) (*Request, error) { fields := strings.Split(query, "=") if len(fields) != 2 { return nil, fmt.Errorf("cannot parse query: %q", query) } log = log.With(). String("request", query). String("left", fields[0]). String("right", fields[1]). Logger() log.Debug().Msg("new request") return &Request{log: log, Query: query}, nil } func (r *Request) Handle() error { // Anywhere in the call flow for the request, if it wants to log // something, it should log to the Request's logger. log := r.log log.Debug().Msg("handling request") return nil }
Output: {"level":"verbose","message":"initializing program"} {"foo":"bar"} {"level":"verbose","message":"Enter NewServer()"} {"level":"verbose","structure":"Server","message":"Enter Server.run()"} {"level":"verbose","structure":"Server","method":"run","message":"starting loop"} {"level":"info","message":"line 2\n"} {"level":"info","message":"line 3\n"}
func New ¶ added in v0.1.0
New returns a new Logger that writes log events to w.
By default, a Logger has a log level of Warning, which is closer to the UNIX philosophy of avoiding unnecessary output.
log := gologs.New(os.Stdout).SetTimeFormatter(gologs.TimeUnix)
func (*Logger) Debug ¶
Debug returns an Event to be formatted and sent to the Logger's underlying io.Writer when the Logger's level is Debug. If the Logger's level is above Debug, this method returns without blocking.
func (*Logger) Error ¶
Error returns an Event to be formatted and sent to the Logger's underlying io.Writer.
func (*Logger) Info ¶
Info returns an Event to be formatted and sent to the Logger's underlying io.Writer when the Logger's level is Debug, Verbose, or Info. If the Logger's level is above Info, this method returns without blocking.
func (*Logger) Log ¶ added in v0.14.0
Log returns an Event to be formatted and sent to the Logger's underlying io.Writer, regardless of the Logger's log level, and omitting the event log level in the output.
func (*Logger) NewWriter ¶ added in v0.13.0
NewWriter creates an io.Writer that conveys all writes it receives to the underlying io.Writer as individual log events.
func main() { log := gologs.New(os.Stdout).SetTimeFormatter(gologs.TimeUnix) lw := log.NewWriter() scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { _, err := lw.Write(scanner.Bytes()) if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } } if err := scanner.Err(); err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } }
func (*Logger) SetDebug ¶ added in v0.2.0
SetDebug changes the Logger's level to Debug, which allows all events to be logged. The change is made without blocking.
func (*Logger) SetError ¶ added in v0.2.0
SetError changes the Logger's level to Error, which causes all Debug, Verbose, Info, and Warning events to be ignored, and all Error events to be logged. The change is made without blocking.
func (*Logger) SetInfo ¶ added in v0.2.0
SetInfo changes the Logger's level to Info, which causes all Debug and Verbose events to be ignored, and all Info, Warning, and Error events to be logged. The change is made without blocking. The change is made without blocking.
func (*Logger) SetLevel ¶
SetLevel changes the Logger's level to the specified Level without blocking.
func (*Logger) SetTimeFormatter ¶ added in v0.7.0
func (log *Logger) SetTimeFormatter(callback TimeFormatter) *Logger
SetTimeFormatter updates the time formatting callback function that is invoked for every log message while it is being formatted, potentially blocking until any in progress log event has been written.
func (*Logger) SetVerbose ¶
SetVerbose changes the Logger's level to Verbose, which causes all Debug events to be ignored, and all Verbose, Info, Warning, and Error events to be logged. The change is made without blocking.
func (*Logger) SetWarning ¶ added in v0.2.0
SetWarning changes the Logger's level to Warning, which causes all Debug, Verbose, and Info events to be ignored, and all Warning, and Error events to be logged. The change is made without blocking.
func (*Logger) SetWriter ¶ added in v0.7.0
SetWriter directs all future writes to w, potentially blocking until any in progress log event has been written.
func (*Logger) Verbose ¶
Verbose returns an Event to be formatted and sent to the Logger's underlying io.Writer when the Logger's level is Debug or Verbose. If the Logger's level is above Verbose, this method returns without blocking.
func (*Logger) Warning ¶
Warning returns an Event to be formatted and sent to the Logger's underlying io.Writer when the Logger's level is Debug, Verbose, Info, or Warning. If the Logger's level is above Warning, this method returns without blocking.
func (*Logger) With ¶ added in v0.12.0
func (log *Logger) With() *Intermediate
With returns an Intermediate Logger instance that inherits from log, but can be modified to add one or more additional properties for every outgoing log event.
log = log.With().String("s", "value").Bool("b", true).Logger()
type TimeFormatter ¶ added in v0.13.0
func TimeFormat ¶ added in v0.12.0
func TimeFormat(format string) TimeFormatter
TimeFormat returns a time formatter that appends the current time to buf as a JSON property name and value using the specified string format.
type Writer ¶ added in v0.13.0
type Writer struct {
// contains filtered or unexported fields
}
Writer is an io.Writer that conveys all writes it receives to the underlying io.Writer as individual log events.
func (*Writer) SetDebug ¶ added in v0.13.0
SetDebug changes the Writer's level to Debug, which causes all writes to the Writer to be logged to the underlying Logger with a level of Debug. The change is made without blocking.
func (*Writer) SetError ¶ added in v0.13.0
SetError changes the Writer's level to Error, which causes all writes to the Writer to be logged to the underlying Logger with a level of Error. The change is made without blocking.
func (*Writer) SetInfo ¶ added in v0.13.0
SetInfo changes the Writer's level to Info, which causes all writes to the Writer to be logged to the underlying Logger with a level of Info. The change is made without blocking.
func (*Writer) SetLevel ¶ added in v0.13.0
SetLevel changes the Writer's level to the specified Level without blocking. This causes all writes to the Writer to be logged with the specified Level.
func (*Writer) SetVerbose ¶ added in v0.13.0
SetVerbose changes the Writer's level to Verbose, which causes all writes to the Writer to be logged to the underlying Logger with a level of Verbose. The change is made without blocking.
func (*Writer) SetWarning ¶ added in v0.13.0
SetWarning changes the Writer's level to Warning, which causes all writes to the Writer to be logged to the underlying Logger with a level of Warning. The change is made without blocking.
func (*Writer) Write ¶ added in v0.13.0
Write creates and emits a log event with its message set to the text of buf and at the log level with which it was instantiated.
On success, it returns the length of buf and nil error. Note that the number of bytes it wrote to the underlying io.Writer will always be longer than the number of bytes it receives in buf.
On failure, it returns 0 for the number of bytes it wrote to the underlying io.Writer along with the write error.