Documentation ¶
Overview ¶
Package log implements a simple logging package
Index ¶
- func GetId(ctx context.Context) string
- type F
- type Level
- type Logger
- func (l Logger) Error(e error, fs ...F)
- func (l Logger) Info(f F)
- func (l Logger) StdLogger() *stdLog.Logger
- func (l Logger) WithCaller() Logger
- func (l Logger) WithCtx(ctx context.Context) Logger
- func (l Logger) WithFields(f F) Logger
- func (l Logger) WithImmediate() Logger
- func (l Logger) Write(p []byte) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Level ¶ added in v0.0.14
type Level string
Level indicates the severity of a log event/message.
const ( // INFO is the log severity indicating an issue that is informational in nature. INFO Level = "info" // ERROR is the log severity indicating an issue that is critical in nature. ERROR Level = "error" // CtxKey is the name of the context key used to store the logID. CtxKey = logContextKeyType("Ong-logID") )
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents an active logging object that generates lines of output to an io.Writer. It stores log messages into a circular buffer. All those log events are only flushed to the underlying io.Writer when a message with level ERROR is logged.
It can be used simultaneously from multiple goroutines. Use New to get a valid Logger.
func New ¶
New creates a new logger. The returned logger buffers upto maxMsgs log messages in a circular buffer.
func (Logger) Error ¶
Error will log at the ERROR level.
Example ¶
package main import ( "errors" "os" "github.com/komuw/ong/log" ) func main() { l := log.New(os.Stdout, 1000) l.Info(log.F{"msg": "sending email", "email": "jane@example.com"}) l.Error(errors.New("sending email failed."), log.F{"email": "jane@example.com"}) // example output: // {"email":"jane@example.com","level":"info","logID":"r73RdRZEExH7cnax2faY7A","msg":"sending email","timestamp":"2022-09-16T12:56:05.471496845Z"} // {"email":"jane@example.com","err":"sending email failed.","level":"error","logID":"r73RdRZEExH7cnax2faY7A","timestamp":"2022-09-16T12:56:05.471500752Z"} }
Output:
func (Logger) StdLogger ¶
StdLogger returns a logger from the Go standard library log package.
That logger will use l as its output.
example usage:
l := log.New(ctx, os.Stdout, 100, true) stdLogger := l.StdLogger() stdLogger.Println("hey")
func (Logger) WithCaller ¶
WithCaller return a new logger, based on l, that will include callers info in its output.
func (Logger) WithFields ¶
WithFields return a new logger, based on l, that will include the given fields in all its output.
func (Logger) WithImmediate ¶
WithImmediate return a new logger, based on l, that will log immediately without buffering.