log

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 9 Imported by: 0

README

log

Log package is a very opinionated replacement for the zerolog/log package, building on zerolog.

Features

In addition to zerolog this package adds:

  • Out of the box formats log.FormatColor "color", log.FormatPlain "plain", log.FormatJSON "json"
  • Logging and formatting for stack traces of github.com/tehsphins/errs package

Usage

package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/cosygreen/errs"
	"github.com/cosygreen/log"
	"github.com/rs/zerolog"
)

func main() {
	fmt.Println("--- CONSOLE LOGGING ---")
	logPkg(log.FormatColor)

	fmt.Println("--- JSON LOGGING ---")
	logPkg(log.FormatJSON)
}

func logPkg(format log.Format) {
	ctx := log.Setup(context.Background(),
		log.WithFormat(format),
		log.ServiceName("test"),
		log.UpdateContext(func(c zerolog.Context) zerolog.Context {
			return c.Str("abc", "xyz")
		}),
	)
	err := errors.New("error msg")
	errStack := errs.WithStack(err)

	log.Ctx(ctx).Info().Str("key1", "val1").Msg("this is a log message")
	log.Ctx(ctx).Err(err).Msg("this is a custom err message")
	log.Ctx(ctx).Err(errStack).Msg("this is a custom err message")
}

Documentation

Overview

Package log provides a global logger for zerolog.

Index

Constants

This section is empty.

Variables

View Source
var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()

Logger is the global logger.

Functions

func Ctx

func Ctx(ctx context.Context) *zerolog.Logger

Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.

func Debug

func Debug() *zerolog.Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

func Err

func Err(err error) *zerolog.Event

Err starts a new message with error level with err as a field if not nil or with info level if err is nil.

You must call Msg on the returned event in order to send the event.

func Error

func Error() *zerolog.Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

func Errorf added in v0.1.2

func Errorf(format string, v ...interface{})

Errorf sends a log event using error level and no extra field. Arguments are handled in the manner of fmt.Printf.

func Fatal

func Fatal() *zerolog.Event

Fatal starts a new message with fatal level.

You must call Msg on the returned event in order to send the event.

func Info

func Info() *zerolog.Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

func NewSplitWriter

func NewSplitWriter[f FormatInput, l LevelInput](userOut, devOut io.Writer, userFormat, devFormat f, userLevel, devLevel l) io.WriteCloser

NewSplitWriter returns an output writer that logs to 2 targets with different output formats and levels. Use with WithFormat(FormatCustom). Using WithLevel would affect both outputs restricting them more. It doesn't overwrite the levels for the 2 outputs.

func Panic

func Panic() *zerolog.Event

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

func Print

func Print(v ...interface{})

Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(format string, v ...interface{})

Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.

func Setup

func Setup(ctx context.Context, opts ...SetupOption) context.Context

Setup is used to set up logging. If a context is passed, the logger will be added to the context and the context returned.

func Trace

func Trace() *zerolog.Event

Trace starts a new message with trace level.

You must call Msg on the returned event in order to send the event.

func Warn

func Warn() *zerolog.Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

Types

type Config added in v0.1.1

type Config struct {
	LogLevel    string `default:"debug" env:"LOG_LEVEL"`
	LogFormat   string `default:"color" env:"LOG_FORMAT"`
	ServiceName string `env:"SERVICE_NAME"`
	HostName    string `env:"HOST_NAME"`
	Region      string `env:"REGION"`
	PublicIP    string `env:"PUBLIC_IP"`
	HideCaller  bool   `env:"LOG_HIDE_CALLER"`
}

type Format

type Format string

Format defines the log format.

const (
	// FormatPlain uses a zerolog.ConsoleWriter without colors and sends the output to provided output or stdout.
	FormatPlain Format = "plain"
	// FormatPlainWithoutTime uses a zerolog.ConsoleWriter without colors and without time and sends the output to provided output or stdout.
	FormatPlainWithoutTime Format = "plain-notime"
	// FormatColor uses a zerolog.ConsoleWriter with colors and sends the output to provided output or stdout.
	FormatColor Format = "color"
	// FormatColorWithoutTime uses a zerolog.ConsoleWriter with colors and without time and sends the output to provided output or stdout.
	FormatColorWithoutTime Format = "color-notime"
	// FormatJSON writes JSON output to the output provided output or stdout.
	FormatJSON Format = "json"
	// FormatCustom can be used to pass in a custom output writer.
	FormatCustom Format = "custom"
)

type FormatInput

type FormatInput interface {
	string | Format
}

FormatInput allows the input format to be of type string or type Format.

type LevelInput

type LevelInput interface {
	string | zerolog.Level
}

LevelInput allows the level input to be of type string or type zerolog.Level.

type QlogAdapter added in v0.2.0

type QlogAdapter struct {
	zerolog.Logger
}

func NewQlogAdapter added in v0.2.0

func NewQlogAdapter(logger zerolog.Logger) *QlogAdapter

func (*QlogAdapter) Errorf added in v0.2.0

func (q *QlogAdapter) Errorf(format string, args ...any)

type SetupOption

type SetupOption func(*setupOptions)

SetupOption defines an option for setting up the logging.

func HostName

func HostName(name string) SetupOption

HostName sets the host name for logging.

func PublicIP

func PublicIP(ip string) SetupOption

PublicIP sets the public ip for logging.

func Region

func Region(name string) SetupOption

Region sets the data center region for logging.

func ServiceName

func ServiceName(name string) SetupOption

ServiceName sets the service name for logging.

func UpdateContext

func UpdateContext(f func(zerolog.Context) zerolog.Context) SetupOption

UpdateContext can be used to update log context, adding additional information at setup.

func WithConfig added in v0.1.1

func WithConfig(config Config) SetupOption

func WithExtraWriters added in v0.1.10

func WithExtraWriters(w ...io.Writer) SetupOption

WithExtraWriters adds extra writers to the logger, so you can use any Format option.

func WithFormat

func WithFormat[T FormatInput](format T) SetupOption

WithFormat sets the format to use for logging.

func WithLevel

func WithLevel[T LevelInput](level T) SetupOption

WithLevel sets the global log level to use for logging.

func WithOutput

func WithOutput(out io.Writer) SetupOption

WithOutput sets the writer to write the log output to. Can be used to use a customized output writer. Doing so should be done in combination with passing FormatCustom to WithFormat.

Jump to

Keyboard shortcuts

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