log

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2024 License: MIT Imports: 5 Imported by: 0

README

zerolog-quick

Go Reference Go Report Card Release License

zerolog-quick is a Go package that provides pre-configured loggers using the zerolog library. It offers both plain and colored console loggers with various formatting options.

Installation

  go get github.com/go-mods/zerolog-quick

Usage

Default JSON Logger

The default logger is a JSON logger that writes to os.Stderr:

import log "github.com/go-mods/zerolog-quick"

func main() {
    log.Info().Msg("This is an info message")
}
Console Loggers
Plain Console Loggers

The plain console logger writes to os.Stderr and does not use any colors:

import (
    log "github.com/go-mods/zerolog-quick"
    "github.com/go-mods/zerolog-quick/console/plain"
)

func main() {
    // Message only
    log.Logger = plain.Message
    log.Info().Msg("Info message")
    // Date and message
    log.Logger = plain.DateMessage
    log.Info().Msg("Info message with date")
    // Date, time, and message
    log.Logger = plain.DateTimeMessage
    log.Info().Msg("Info message with date and time")
    // Date, level, and message
    log.Logger = plain.DateLevelMessage
    log.Info().Msg("Info message with date and level")
    // Date, time, level, and message
    log.Logger = plain.DateTimeLevelMessage
    log.Info().Msg("Info message with date, time, and level")
}
Colored Console Loggers

The colored console logger writes to os.Stderr and uses colors:

import (
    log "github.com/go-mods/zerolog-quick"
    "github.com/go-mods/zerolog-quick/console/colored"
)

func main() {
    // Colored message
    log.Logger = colored.Message
    log.Info().Msg("Colored info message")
    // Colored date and message
    log.Logger = colored.DateMessage
    log.Info().Msg("Colored info message with date")
    // Colored date, time, and message
    log.Logger = colored.DateTimeMessage
    log.Info().Msg("Colored info message with date and time")
    // Colored date, level, and message
    log.Logger = colored.DateLevelMessage
    log.Info().Msg("Colored info message with date and level")
    // Colored date, time, level, and message
    log.Logger = colored.DateTimeLevelMessage
    log.Info().Msg("Colored info message with date, time, and level")
}
Custom Logger Configuration

You can create custom logger configurations using the zerolog.New() function and the zerolog.ConsoleWriter options. For example:

import (
    "os"
    "github.com/rs/zerolog"
    log "github.com/go-mods/zerolog-quick"
)

func main() {
    customLogger := zerolog.New(
        zerolog.ConsoleWriter{
            Out: os.Stdout,
            NoColor: false,
            TimeFormat: "2006-01-02 15:04:05",
            PartsOrder: []string{
                zerolog.TimestampFieldName,
                zerolog.LevelFieldName,
                zerolog.MessageFieldName,
            },
		},
    ).With().Timestamp().Logger()
    log.Logger = customLogger
    log.Info().Msg("Custom logger message")
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package log adds predefined custom logger.

Index

Constants

This section is empty.

Variables

View Source
var Logger = json.Default

Logger is the global logger. It use the default zerolog global logger defined in json module

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 Fatal

func Fatal() *zerolog.Event

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

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

func Hook

func Hook(h zerolog.Hook) zerolog.Logger

Hook returns a logger with the h Hook.

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 Level

func Level(level zerolog.Level) zerolog.Logger

Level creates a child logger with the minimum accepted level set to level.

func Log

func Log() *zerolog.Event

Log starts a new message with no level. Setting zerolog.GlobalLevel to zerolog.Disabled will still disable events produced by this method.

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

func Output

func Output(w io.Writer) zerolog.Logger

Output duplicates the global logger and sets w as its output.

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 Sample

func Sample(s zerolog.Sampler) zerolog.Logger

Sample returns a logger with the s sampler.

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.

func With

func With() zerolog.Context

With creates a child logger with the field added to its context.

func WithLevel

func WithLevel(level zerolog.Level) *zerolog.Event

WithLevel starts a new message with level.

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

Types

This section is empty.

Directories

Path Synopsis
console

Jump to

Keyboard shortcuts

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