log

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package log is internal log wrapper functionality

Index

Constants

View Source
const RedactionString = "[FILTERED]"

RedactionString represents the filtered value used in place of sensitive data in the log package.

Variables

This section is empty.

Functions

func AddToContext

func AddToContext(ctx context.Context, l *Logger) context.Context

AddToContext returns new context.Context with additional logger.

func Fatal

func Fatal(v ...interface{})

Fatal is equivalent to Print() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf is equivalent to Printf() followed by a call to os.Exit(1).

func IsSensitiveHeader

func IsSensitiveHeader(name string) bool

IsSensitiveHeader will return true if the given parameter name should be masked for sensitivity.

func IsSensitiveParam

func IsSensitiveParam(name string) bool

IsSensitiveParam will return true if the given parameter name should be masked for sensitivity.

func MaskURL

func MaskURL(originalURL string) string

MaskURL will mask the sensitive components in an URL with `[FILTERED]`. This list should maintain parity with the list Based on https://stackoverflow.com/a/52965552/474597.

func NewLoggingContext

func NewLoggingContext(ctx context.Context, fields ...interface{}) context.Context

NewLoggingContext returns a copy of context that also includes a configured logger.

func OnCloseError

func OnCloseError(logger *Logger, f io.Closer)

OnCloseError execute function f with possible error return. It logs an error if error from f is found it aims to be used for deferred method such : resp.Body.Close(), tx.Rollback() Pass nil as logger to use default package logger So it should be used like: `defer log.OnOnErrorFunc(logger, resp.Body.Close).

func OnCloseErrorf

func OnCloseErrorf(logger *Logger, f io.Closer, format string, v ...interface{})

OnCloseErrorf execute function f with possible error return. It logs an error if error from f is found it aims to be used for deferred method such : resp.Body.Close(), tx.Rollback() Pass nil as logger to use default package logger So it should be used like: `defer log.OnOnErrorFunc(logger, resp.Body.Close, "closing response body for path %s", path).

func Print

func Print(v ...interface{})

Print calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(format string, v ...interface{})

Printf calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...interface{})

Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.

func Set

func Set(l *Logger)

Set the base package logger.

Types

type BatchConfig

type BatchConfig struct {
	Enabled  bool
	Interval time.Duration
	MaxLines int
}

BatchConfig is configuration for async log batch writer.

type ErrFunc

type ErrFunc func() error

ErrFunc is any function which takes no argument and possibly returns error e.g. tx.Rollback(), resp.Body.Close().

type Level

type Level int

Level is log output level.

const (
	// LevelDisabled level.
	LevelDisabled Level = -1
	// LevelDebug level.
	LevelDebug Level = 0
	// LevelInfo level.
	LevelInfo Level = 1
	// LevelWarn level.
	LevelWarn Level = 2
	// LevelError level.
	LevelError Level = 3
)

func GetLevelFromString

func GetLevelFromString(level string) Level

GetLevelFromString return error level based on config string.

type Logger

type Logger struct {
	// Level of min logging
	Level Level
	// Version
	Version string
	// Revision
	Revision string
	// DebugLog logger
	StdLog zerolog.Logger
	// ErrorLog logger
	ErrLog zerolog.Logger
	// contains filtered or unexported fields
}

Logger is structured leveled logger.

func FromCtx

func FromCtx(ctx context.Context) *Logger

FromCtx returns current logger in context. If there is no logger in context it returns a new one with current config values. logger initial attribute fields is copied from existing defaultLogger fields.

func NewDevLogger

func NewDevLogger(fileLogger *lumberjack.Logger, batchCfg *BatchConfig, stfields ...interface{}) *Logger

NewDevLogger logger. Pretty logging for development mode. Not recommended for production use. If static fields are provided those values will define the default static fields for each new built instance if they were not yet configured.

func NewFromConfig

func NewFromConfig(cfg kitconfig.KVStore, path string) (l *Logger, err error)

NewFromConfig returns logger based on config file

given config yaml file contents:

	log:
	  level: info
	  json-enabled: false
	  file:
		enabled: true
		path: ./logs/promo-engine.log
		maxsize-mb: 10
		maxage-days: 7
		maxbackup-files: 2
	  batch:
		enabled: false
		max-lines: 1000
		interval: 15ms

then we can call using :

	v := viper.New()
	... set v file configs, etc

	logger := log.NewFromConfig(v, "log")
	..continue using logger.

func NewLogger

func NewLogger(level Level, name string, fileLogger *lumberjack.Logger, batchCfg *BatchConfig, stfields ...interface{}) *Logger

NewLogger logger. If static fields are provided those values will define the default static fields for each new built instance if they were not yet configured.

func (*Logger) AddField

func (l *Logger) AddField(key, value interface{})

AddField add dynamic field key-value The receiver instance will always append these key-value pairs to the output.

func (*Logger) Debug

func (l *Logger) Debug(msg string, meta ...interface{})

Debug logs debug messages.

func (*Logger) Error

func (l *Logger) Error(err error, msg string, meta ...interface{})

Error logs error messages.

func (*Logger) Info

func (l *Logger) Info(msg string, meta ...interface{})

Info logs info messages.

func (*Logger) ResetFields

func (l *Logger) ResetFields()

ResetFields clear all the logger's assigned dymanic fields Remove dynamic fields.

func (*Logger) Set

func (l *Logger) Set() *Logger

Set default package logger. Can be used chained with NewLogger to create a new one, set it up as package default logger and get it for use in one step. i.e: logger := log.NewLogger(log.Debug, "name", "version", "revision").Set().

func (*Logger) SetFields

func (l *Logger) SetFields(dynafields ...interface{})

SetFields set logger dynamic fields. The receiver instance will always append these key-value pairs to the output.

func (*Logger) UpdateLogLevel

func (l *Logger) UpdateLogLevel(level Level)

UpdateLogLevel updates log level.

func (*Logger) Warn

func (l *Logger) Warn(msg string, meta ...interface{})

Warn logs warning messages.

func (*Logger) WarnError

func (l *Logger) WarnError(err error, msg string, meta ...interface{})

WarnError used for log error but in `warn` level e.g.

if err != nil {
  log.FromCtx(ctx).WarnError(err, "something happened. continue...")
}.

Jump to

Keyboard shortcuts

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