zerolog_wrapper

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 7 Imported by: 0

README

A Go ZeroLog Module Wrapper

A Go wrapper module for Zerolog module with some default which I do repeatedly on all my applications.

zerolog_wrapper is a wrapper modules with some default logs configuration which helpedso we can get started with logging in Golang right away. This will ensure we follow DRY principle not to duplicate the same code in multiple applications.

Usage

package main

import (
    log "github.com/ashokrajar/zerolog_wrapper"
)

func init() {
    log.InitLog(log.TraceLevel, "dev")
}

func main() {
    log.Info().Msg("hello world")
}

Output

{"time":1494567715,"level":"info","message":"hello world"}
How to add fields
log.Info().Str("foo", "bar").Msg("hello world")

Output

{"time":1494567715,"level":"info","message":"hello world","foo":"bar"}

Documentation

Overview

Package zerolog_wrapper provides a custom log wrapper based on github.com/rs/zerolog

How to use:

import (
    log "github.com/ashokrajar/zerolog_wrapper"
)

func init() {
    log.InitLog(log.TraceLevel, "dev")
}

func main() {
    log.Info().Msg("hello world")
}
// Output: {"time":1494567715,"level":"info","message":"hello world"}

Fields can be added to log messages:

log.Info().Str("foo", "bar").Msg("hello world")
// Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}

Updating the logger context

log.UpdateContext(func(c zerolog.Context) zerolog.Context {
			return c.Str("some_default_key", "some_default_value")
		})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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.

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

func GetLogger added in v0.4.0

func GetLogger() zerolog.Logger

GetLogger returns the global logger from the zerolog package.

Returns:

The zerolog.Logger instance used for logging in the application.

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 InitLog

func InitLog(logLevelStr LogLevel, appEnv Env)

InitLog initializes a global logger

func Panic

func Panic() *zerolog.Event

Panic starts a new message with panic level.

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

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 UpdateContext added in v0.2.0

func UpdateContext(update func(c zerolog.Context) zerolog.Context)

UpdateContext is a function that updates the internal logger's context.

Parameters: update: A function taking a zerolog.Context as input and then returns a zerolog.Context.

eg:

log.UpdateContext(func(c zerolog.Context) zerolog.Context {
	return c.Str("some_default_key", "some_default_value")
})

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 Env

type Env string
const (
	Prod  Env = "prod"
	Stage Env = "stage"
	QA    Env = "qa"
	Dev   Env = "dev"
)

type LogLevel

type LogLevel string
const (
	TraceLevel LogLevel = "trace"
	DebugLevel LogLevel = "debug"
	InfoLevel  LogLevel = "info"
	WarnLevel  LogLevel = "warn"
	ErrorLevel LogLevel = "error"
	FatalLevel LogLevel = "fatal"
	PanicLevel LogLevel = "panic"
)

Jump to

Keyboard shortcuts

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