logger

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: GPL-2.0 Imports: 7 Imported by: 7

Documentation

Overview

Package logger is abstraction over logger implementation

Index

Constants

View Source
const ContextKeyRequestID = ContextKey("correlationId")

ContextKeyRequestID is correlationId key for context

View Source
const ContextKeyTenantID = ContextKey("tenantId")

ContextKeyTenantID is tenantId key for context

Variables

This section is empty.

Functions

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID will get reqID from a http request and return it as a string

func GetTenantID added in v1.0.6

func GetTenantID(ctx context.Context) string

GetTenantID will get tenantID from current context and return it as a string

func ParseLevel

func ParseLevel(lvl Level) string

ParseLevel parse Level to string value

Types

type ContextKey added in v1.0.1

type ContextKey string

ContextKey type for context keys

type Fields

type Fields map[string]interface{}

Fields type, used to pass to `WithFields`.

type Level

type Level uint32

Level is logger severity level

const (
	// PanicLevel level, highest level of severity
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`.
	FatalLevel
	// ErrorLevel level.
	ErrorLevel
	// WarnLevel level.
	WarnLevel
	// InfoLevel level.
	InfoLevel
	// DebugLevel level.
	DebugLevel
	// TraceLevel level.
	TraceLevel
)

func ParseLogrusLevel

func ParseLogrusLevel(lvl logrus.Level) Level

ParseLogrusLevel takes a string level and returns the Logrus log level constant.

func ToLogLevel

func ToLogLevel(s string) Level

ToLogLevel converts string to log Level

type Logger

type Logger interface {
	// SetLevel logging severity level
	SetLevel(level Level) error
	// GetLevel returns current logging level
	GetLevel() Level
	// SetArea set area logger context
	SetArea(area string) Logger
	// SetOperation set operation logger context
	SetOperation(operation string) Logger
	// SetCorrelationID set logger context
	SetCorrelationID(correlationID string) Logger
	// GetCorrelationID returns CorrelationID for current logger context
	GetCorrelationID() string
	// SetTenantID set tenantID for current logger context
	SetTenantID(tenantID string) Logger
	// GetTenantID returns TenantID for current logger context
	GetTenantID() string

	// WithField adds a filed to log entry
	WithField(key string, value interface{}) Logger
	// WithFields adds a struct of fields to the log entry.
	WithFields(fields Fields) Logger
	// WithError Add an error as single field to the log entry.
	WithError(err error) Logger
	WithContext(ctx context.Context) Logger

	Trace(args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	// TrackFuncTime creates log record with func execution time
	TrackFuncTime(start time.Time)
}

Logger is Generic logger interface

func NewNopLogger

func NewNopLogger() Logger

NewNopLogger returns a logger that discards all log messages.

type LogrusLogger

type LogrusLogger struct {
	Logger

	// CorrelationID is a unique identifier of the request.
	CorrelationID string
	// TenantID is a unique identifier of the tenant.
	TenantID string
	// contains filtered or unexported fields
}

LogrusLogger logrus.Logger implementation of interface

func NewLogrusLogger

func NewLogrusLogger(l logrus.Level) LogrusLogger

NewLogrusLogger creates new instance of LogrusLogger

func (LogrusLogger) Debug

func (l LogrusLogger) Debug(args ...interface{})

Debug creates logs entry with Debug level

func (LogrusLogger) Error

func (l LogrusLogger) Error(args ...interface{})

Error creates logs entry with Error level

func (LogrusLogger) Fatal

func (l LogrusLogger) Fatal(args ...interface{})

Fatal creates logs entry with Fatal level

func (LogrusLogger) GetCorrelationID added in v1.0.3

func (l LogrusLogger) GetCorrelationID() string

GetCorrelationID returns CorrelationID for current logger context

func (LogrusLogger) GetLevel

func (l LogrusLogger) GetLevel() Level

GetLevel returns current logging level

func (LogrusLogger) GetTenantID added in v1.0.6

func (l LogrusLogger) GetTenantID() string

GetTenantID returns TenantID for current logger context

func (LogrusLogger) Info

func (l LogrusLogger) Info(args ...interface{})

Info creates logs entry with Info level

func (LogrusLogger) Panic

func (l LogrusLogger) Panic(args ...interface{})

Panic creates logs entry with Panic level

func (LogrusLogger) SetArea added in v1.0.6

func (l LogrusLogger) SetArea(area string) Logger

SetArea logger context (Area)

func (LogrusLogger) SetCorrelationID added in v1.0.3

func (l LogrusLogger) SetCorrelationID(correlationID string) Logger

SetCorrelationID set CorrelationID for logger context

func (LogrusLogger) SetLevel

func (l LogrusLogger) SetLevel(level Level) error

SetLevel sets logger level

func (LogrusLogger) SetOperation added in v1.0.3

func (l LogrusLogger) SetOperation(operation string) Logger

SetOperation logger context (Operation)

func (*LogrusLogger) SetOutput

func (l *LogrusLogger) SetOutput(w io.Writer)

SetOutput sets the output to desired io.Writer like file, stdout, stderr etc

func (LogrusLogger) SetTenantID added in v1.0.6

func (l LogrusLogger) SetTenantID(tenantID string) Logger

SetTenantID set TenantID for current logger context

func (LogrusLogger) Trace

func (l LogrusLogger) Trace(args ...interface{})

Trace creates logs entry with Trace level

func (LogrusLogger) TrackFuncTime

func (l LogrusLogger) TrackFuncTime(start time.Time)

TrackFuncTime creates log record with func execution time require debug level or higher usage:

func SomeFunction(list *[]string) {
   defer TimeTrack(time.Now())
   // Do whatever you want.
}

func (LogrusLogger) Warn

func (l LogrusLogger) Warn(args ...interface{})

Warn creates logs entry with Warn level

func (LogrusLogger) WithContext

func (l LogrusLogger) WithContext(ctx context.Context) Logger

WithContext adds a context to the Entry.

func (LogrusLogger) WithError

func (l LogrusLogger) WithError(err error) Logger

WithError Add an error as single field to the log entry.

func (LogrusLogger) WithField

func (l LogrusLogger) WithField(key string, value interface{}) Logger

WithField adds a filed to log entry

func (LogrusLogger) WithFields

func (l LogrusLogger) WithFields(fields Fields) Logger

WithFields adds a struct of fields to the log entry.

Jump to

Keyboard shortcuts

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