log

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2022 License: MIT Imports: 4 Imported by: 0

README

log

This is a simple logger that uses zap/logger as the core logger

Docs

package main

func main() {
    // simplest startup, no other setup needed
	l := log.New("service")

    // pass log level and output to configure
	l = New("service-name", WithLevel("INFO"), WithOutput(w))
	
    // use logger with custom fields and level accordingly
    l.WithFields(
		zap.String("somefield", "somevalue"),
		zap.String("somefield2", "somevalue2")).Info("some log")

    l.WithFields(
		zap.String("somefield", "somevalue"),
		zap.String("somefield2", "somevalue2")).Debug("some log")

    l.Error("some error")
    l.Warn("some warning")
}

Documentation

Index

Constants

View Source
const (
	TimeKey          = "timestamp"
	LevelKey         = "level"
	NameKey          = "logger"
	CallerKey        = "caller"
	MessageKey       = "msg"
	StackTraceKey    = "stacktrace"
	CorrelationIDKey = "correlation-id"
	SpanIDKey        = "span-id"
	TraceIDKey       = "trace-id"
	HostnameKey      = "hostname"
	ServiceNameKey   = "service-name"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Level

type Level zapcore.Level
const (
	DebugLevel Level = iota - 1
	InfoLevel
	WarnLevel
	ErrorLevel
	DPanicLevel
	PanicLevel
	FatalLevel

	DefaultLevel = InfoLevel
)

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger uses the uber zap logger package at its core logger

func New

func New(service string, options ...OptionFunc) *Logger

New creates a new logger with the given service name

setting log level and custom output are done by WithLevel() and WithOutput() funcs of this pkg these funcs are completely optional upon calling New()

func (*Logger) Debug

func (l *Logger) Debug(msg string, fields ...zap.Field)

Debug logs to the output

if the logging level is set to Debug or above

func (*Logger) Error

func (l *Logger) Error(msg string, fields ...zap.Field)

Error logs to the output

if the logging level is set to Error or above

func (*Logger) Info

func (l *Logger) Info(msg string, fields ...zap.Field)

Info logs to the output

if the logging level is set to Info or above

func (*Logger) Warn

func (l *Logger) Warn(msg string, fields ...zap.Field)

Warn logs to the output

if the logging level is set to Warn or above

func (*Logger) WithField

func (l *Logger) WithField(field zap.Field) *Logger

WithField modifies the logger to add a new field to it

func (*Logger) WithFields

func (l *Logger) WithFields(fields ...zap.Field) *Logger

WithFields returns the logger with the given fields added

type OptionFunc

type OptionFunc func(*Logger)

func WithLevel

func WithLevel(level string) OptionFunc

WithLevel allows setting a custom level to the logger

func WithOutput

func WithOutput(output zapcore.WriteSyncer) OptionFunc

WithOutput allows setting a custom output to the logger

Jump to

Keyboard shortcuts

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