loggers

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: Apache-2.0 Imports: 4 Imported by: 3

README

GoDoc

loggers

import "github.com/go-coldbrew/log/loggers"

Package loggers provides loggers implementation for log package

Index

Constants

These are the different logging levels. You can set the logging level to log on your instance of logger, obtained with `logs.New()`.

const (
    // ErrorLevel level. Logs. Used for errors that should definitely be noted.
    // Commonly used for hooks to send errors to an error tracking service.
    ErrorLevel = iota
    // WarnLevel level. Non-critical entries that deserve eyes.
    WarnLevel
    // InfoLevel level. General operational entries about what's going on inside the
    // application.
    InfoLevel
    // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
    DebugLevel
)

Variables

AllLevels A constant exposing all logging levels

var AllLevels = []Level{
    ErrorLevel,
    WarnLevel,
    InfoLevel,
    DebugLevel,
}

DefaultOptions stores all default options in loggers package

var (
    DefaultOptions = Options{
        ReplaceStdLogger:   false,
        JSONLogs:           true,
        Level:              InfoLevel,
        TimestampFieldName: "@timestamp",
        LevelFieldName:     "level",
        CallerInfo:         true,
        CallerFileDepth:    2,
        CallerFieldName:    "caller",
    }
)

func AddToLogContext

func AddToLogContext(ctx context.Context, key string, value interface{}) context.Context

AddToLogContext adds log fields to context. Any info added here will be added to all logs using this context

func FetchCallerInfo

func FetchCallerInfo(skip int, depth int) (function string, file string, line int)

FetchCallerInfo fetches function name, file name and line number from stack

type BaseLogger

BaseLogger is the interface that needs to be implemented by client loggers

type BaseLogger interface {
    Log(ctx context.Context, level Level, skip int, args ...interface{})
    SetLevel(level Level)
    GetLevel() Level
}

type Level

Level type

type Level uint32
func ParseLevel
func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the log level constant.

func (Level) String
func (level Level) String() string

Convert the Level to a string. E.g. ErrorLevel becomes "error".

type LogFields

LogFields contains all fields that have to be added to logs

type LogFields map[string]interface{}
func FromContext
func FromContext(ctx context.Context) LogFields

FromContext fetchs log fields from provided context

func (LogFields) Add
func (o LogFields) Add(key string, value interface{})

Add or modify log fields

func (LogFields) Del
func (o LogFields) Del(key string)

Del deletes a log field entry

type Option

Option defines an option for BaseLogger

type Option func(*Options)
func WithCallerFieldName
func WithCallerFieldName(name string) Option

WithCallerFieldName sets the name of callerinfo field

func WithCallerFileDepth
func WithCallerFileDepth(depth int) Option

WithCallerFileDepth sets the depth of file to use in caller info

func WithCallerInfo
func WithCallerInfo(callerInfo bool) Option

WithCallerInfo enables/disables adding caller info to logs

func WithJSONLogs
func WithJSONLogs(json bool) Option

WithJSONLogs enables/disables json logs

func WithLevelFieldName
func WithLevelFieldName(name string) Option

WithLevelFieldName sets the name of the level field in logs

func WithReplaceStdLogger
func WithReplaceStdLogger(replaceStdLogger bool) Option

WithReplaceStdLogger enables/disables replacing std logger

func WithTimestampFieldName
func WithTimestampFieldName(name string) Option

WithTimestampFieldName sets the name of the time stamp field in logs

type Options

Options contain all common options for BaseLoggers

type Options struct {
    ReplaceStdLogger   bool
    JSONLogs           bool
    Level              Level
    TimestampFieldName string
    LevelFieldName     string
    CallerInfo         bool
    CallerFileDepth    int
    CallerFieldName    string
}
func GetDefaultOptions
func GetDefaultOptions() Options

GetDefaultOptions fetches loggers default options

Generated by gomarkdoc

Documentation

Overview

Package loggers provides loggers implementation for log package

Index

Constants

View Source
const (
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel = iota
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger, obtained with `logs.New()`.

View Source
const COLDBREW_CALL_STACK_SIZE = 3

COLDBREW_CALL_STACK_SIZE number stack frame involved between the logger call from application to zap call.

Variables

AllLevels A constant exposing all logging levels

View Source
var (
	DefaultOptions = Options{
		ReplaceStdLogger:   false,
		JSONLogs:           true,
		Level:              InfoLevel,
		TimestampFieldName: "ts",
		LevelFieldName:     "level",
		CallerInfo:         true,
		CallerFileDepth:    2,
		CallerFieldName:    "caller",
	}
)

DefaultOptions stores all default options in loggers package

View Source
var (
	LogsContextKey string = "LogsContextKey"
)

Functions

func AddToLogContext

func AddToLogContext(ctx context.Context, key string, value interface{}) context.Context

AddToLogContext adds log fields to context. Any info added here will be added to all logs using this context

func FetchCallerInfo

func FetchCallerInfo(skip int, depth int) (function string, file string, line int)

FetchCallerInfo fetches function name, file name and line number from stack

func GetNewLogContextWithValue added in v0.5.0

func GetNewLogContextWithValue(ctx context.Context, key string, value interface{}) context.Context

Types

type BaseLogger

type BaseLogger interface {
	Log(ctx context.Context, level Level, skip int, args ...interface{})
	SetLevel(level Level)
	GetLevel() Level
}

BaseLogger is the interface that needs to be implemented by client loggers

type Level

type Level uint32

Level type

func ParseLevel

func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the log level constant.

func (Level) String

func (level Level) String() string

Convert the Level to a string. E.g. ErrorLevel becomes "error".

type LogFields

type LogFields map[string]interface{}

LogFields contains all fields that have to be added to logs

func FromContext

func FromContext(ctx context.Context) LogFields

FromContext fetchs log fields from provided context

func (LogFields) Add

func (o LogFields) Add(key string, value interface{})

Add or modify log fields

func (LogFields) Del

func (o LogFields) Del(key string)

Del deletes a log field entry

type Option

type Option func(*Options)

Option defines an option for BaseLogger

func WithCallerFieldName

func WithCallerFieldName(name string) Option

WithCallerFieldName sets the name of callerinfo field

func WithCallerFileDepth

func WithCallerFileDepth(depth int) Option

WithCallerFileDepth sets the depth of file to use in caller info

func WithCallerInfo

func WithCallerInfo(callerInfo bool) Option

WithCallerInfo enables/disables adding caller info to logs

func WithJSONLogs

func WithJSONLogs(json bool) Option

WithJSONLogs enables/disables json logs

func WithLevelFieldName

func WithLevelFieldName(name string) Option

WithLevelFieldName sets the name of the level field in logs

func WithReplaceStdLogger

func WithReplaceStdLogger(replaceStdLogger bool) Option

WithReplaceStdLogger enables/disables replacing std logger

func WithTimestampFieldName

func WithTimestampFieldName(name string) Option

WithTimestampFieldName sets the name of the time stamp field in logs

type Options

type Options struct {
	ReplaceStdLogger   bool
	JSONLogs           bool
	Level              Level
	TimestampFieldName string
	LevelFieldName     string
	CallerInfo         bool
	CallerFileDepth    int
	CallerFieldName    string
}

Options contain all common options for BaseLoggers

func GetDefaultOptions

func GetDefaultOptions() Options

GetDefaultOptions fetches loggers default options

Directories

Path Synopsis
Package gokit provides BaseLogger implementation for go-kit/log
Package gokit provides BaseLogger implementation for go-kit/log
Package logrus provides a BaseLogger implementation for logrus
Package logrus provides a BaseLogger implementation for logrus
Package stdlog provides a BaseLogger implementation for golang "log" package
Package stdlog provides a BaseLogger implementation for golang "log" package
Package zap provides a BaseLogger implementation for uber/zap
Package zap provides a BaseLogger implementation for uber/zap

Jump to

Keyboard shortcuts

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