logboek

package module
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2026 License: Apache-2.0 Imports: 7 Imported by: 196

README

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)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptedLevel

func AcceptedLevel() level.Level

func Colorize added in v0.4.3

func Colorize(style color.Style, a ...interface{}) string

func ColorizeF added in v0.5.0

func ColorizeF(style color.Style, format string, a ...interface{}) string

func ColorizeLn added in v0.5.0

func ColorizeLn(style color.Style, a ...interface{}) string

func Context

func Context(ctx context.Context) types.LoggerInterface

func Debug

func Debug() types.ManagerInterface

func Default

func Default() types.ManagerInterface

func DefaultLogger

func DefaultLogger() types.LoggerInterface

func ErrStream added in v0.5.0

func ErrStream() io.Writer

func Error

func Error() types.ManagerInterface

func FitText

func FitText(text string, options types.FitTextOptions) string

func Info

func Info() types.ManagerInterface

func IsAcceptedLevel

func IsAcceptedLevel(lvl level.Level) bool

func Log added in v0.5.0

func Log(a ...interface{})

func LogBlock

func LogBlock(headerOrFormat string, a ...interface{}) types.LogBlockInterface

func LogDetails added in v0.5.0

func LogDetails(a ...interface{})

func LogF

func LogF(format string, a ...interface{})

func LogFDetails

func LogFDetails(format string, a ...interface{})

func LogFHighlight

func LogFHighlight(format string, a ...interface{})

func LogFWithCustomStyle

func LogFWithCustomStyle(style color.Style, format string, a ...interface{})

func LogHighlight added in v0.5.0

func LogHighlight(a ...interface{})

func LogLn

func LogLn(a ...interface{})

func LogLnDetails

func LogLnDetails(a ...interface{})

func LogLnHighlight

func LogLnHighlight(a ...interface{})

func LogLnWithCustomStyle

func LogLnWithCustomStyle(style color.Style, a ...interface{})

func LogOptionalLn

func LogOptionalLn()

func LogProcess

func LogProcess(headerOrFormat string, a ...interface{}) types.LogProcessInterface

func LogProcessInline

func LogProcessInline(headerOrFormat string, a ...interface{}) types.LogProcessInlineInterface

func LogWithCustomStyle added in v0.5.0

func LogWithCustomStyle(style color.Style, a ...interface{})

func MustContext added in v0.7.0

func MustContext(ctx context.Context) types.LoggerInterface

func NewContext

func NewContext(ctx context.Context, logger types.LoggerInterface) context.Context

func NewLogger

func NewLogger(outStream, errStream io.Writer) types.LoggerInterface

func NewSubLogger

func NewSubLogger(outStream, errStream io.Writer) types.LoggerInterface

func OutStream added in v0.5.0

func OutStream() io.Writer

func Reset added in v0.4.3

func Reset()

func ResetModes added in v0.4.4

func ResetModes()

func ResetState added in v0.4.4

func ResetState()

func SetAcceptedLevel

func SetAcceptedLevel(lvl level.Level)

func Streams

func Streams() types.StreamsInterface

func Warn

func Warn() types.ManagerInterface

Types

This section is empty.

Directories

Path Synopsis
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL