Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

werf/logboek

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

173 Commits
173 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logboek — a library for structured and informative output

Logger, channels, and streams

When creating a logger, you need to specify the streams, OutStream and ErrStream, which must adhere to the io.Writer interface (this can be a file, standard output streams, a buffer, or any custom implementation):

import "github.com/werf/logboek"

// NewLogger(outStream, errStream io.Writer) *Logger
l := logboek.NewLogger(os.Stdout, os.Stderr)

Stream settings allow you to define formatting parameters such as prefix and tag, as well as various modes of operation. These settings apply to both OutStream and ErrStream, and consequently to all channels that will be discussed later.

l.Streams()

The logger is connected to the log channels Error, Warn, Default, Info, and Debug. When using the Error and Warn channels, all messages are written to ErrStream, while for the others, they go to OutStream.

Log channels allow you to organize the output for various application modes (verbose and debug modes), branch execution, and control flow depending on the active channel (activating a channel also triggers output to lower-priority channels):

import (
    "github.com/werf/logboek"
    "github.com/werf/logboek/pkg/level"
)

switch mode {
case "verbose":
    l.SetAcceptedLevel(level.Info)
case "debug":
    l.SetAcceptedLevel(level.Debug)
case "quiet":
    l.SetAcceptedLevel(level.Error)  
}

...

if l.Debug().IsAccepted() {
  ... // do and print something special
}

If channels are not required, you can simply use the Default channel, whose methods are available at the top level of the logger:

l.LogLn() // l.Default().LogLn()
l.LogF()  // l.Default().LogF()
...

Default logger

By default, the library initializes the DefaultLogger with preset streams os.Stdout and os.Stderr. You can interact with the logger using the instance itself or the high-level library functions that correspond to all available logger methods:

import "github.com/werf/logboek"

logboek.DefaultLogger()

logboek.Default() // logboek.DefaultLogger().Default()
logboek.LogLn()   // logboek.DefaultLogger().LogLn()
logboek.Streams() // logboek.DefaultLogger().Streams()
...

Using logboek with context.Context

Logboek can propagate a logger through context.Context:

ctx := logboek.NewContext(context.Background(), myLogger)

// Inside a helper function
logboek.Context(ctx).LogLn("hello from helper")

logboek.Context(ctx) is safe: if the provided context does not hold a logger (or is nil/context.Background()), it automatically falls back to logboek.DefaultLogger().

If you need the original strict behaviour (panic when no logger found), call logboek.MustContext(ctx) instead.

// Will panic if ctx has no logger
l := logboek.MustContext(ctx)

About

A library for structured and informative output.

Resources

Stars

Watchers

Forks

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.