logger

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 6 Imported by: 0

README

Logger

This package implements a logger using zerolog that should be used for application logging.

Usage

Importing the package
import github.com/nawiridigital/pkg/logger
Creating logger

Simplest way to instantiate a new logger is

l := logger.New(nil)

The New() constructor function takes in a logger configuration object

type Config struct {
	LogMode string
	Service string // unique name 
}

LogMode can be any of

	LModeSilent = "SILENT"
	LModeTrace  = "TRACE"
	LModeInfo   = "INFO"
	LModeWarn   = "WARN"
	LModeError  = "ERROR"
	LModeDebug  = "DEBUG"
Setting default logger

Once you create a logger instance you can set it as the default logger in your application

// create a logger instance
l := logger.New(nil)
// set it as default logger instance
logger.SetDefaultLogger(l)

// fetch the default logger instance
l = logger.Get()
Using the logger

Usages of the logger should be similar to a zerolog.Logger instance

l := logger.New(nil)
l.Info().Msg("example log")
l.Err().Error(err).Msg("database error")

Documentation

Index

Constants

View Source
const (
	LStage     = "stage"
	LData      = "data"
	LUserID    = "userId"
	LUrl       = "url"
	LMethod    = "method"
	LService   = "service"
	LContext   = "context"
	LRequestID = "requestID"
)
View Source
const (
	LModeSilent = "SILENT"
	LModeTrace  = "TRACE"
	LModeInfo   = "INFO"
	LModeWarn   = "WARN"
	LModeError  = "ERROR"
	LModeDebug  = "DEBUG"
)

Variables

This section is empty.

Functions

func Get

func Get() zerolog.Logger

Get will return copy of global logger

func New

func New(cfg *Config) zerolog.Logger

New accepts configurations on logger and creates a new logger instance If no LogMode is set, it will default to debug level

func SetDefaultLogger

func SetDefaultLogger(logger zerolog.Logger)

SetDefaultLogger will override global logger defined

Types

type Config

type Config struct {
	LogMode string
	Service string
}

type DataParams

type DataParams map[string]interface{}

DataParams can be used to pass data values to logger as interface e.g. zerolog.Info().Interface(LData, DataParams{"a": a, "b": b})

Jump to

Keyboard shortcuts

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