logger

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Logger Package

The Logger package provides a flexible and configurable logging solution based on Zap. It allows you to easily integrate structured logging into your Go applications.

Installation

To use this library in your Go project, you can simply import it:

import "github.com/thegreatforge/gokit/logger"

Getting Started

The Logger package provides a simple and convenient way to set up a logger with various configuration options. It follows the "Functional Options Pattern" for configuration, allowing you to customize the logger's behavior.

Initializing the Logger

You can initialize the logger with custom configuration options using the Initialize function:

err := logger.Initialize(
    logger.Level("debug"),    // Set log level to debug
)
if err != nil {
    panic(err)
}

By default, the library initializes a logger with production settings, but you can override these settings as shown above.

Logging

Once the logger is initialized, you can use it to log messages at different log levels: Debug, Info, Warn, Error, Fatal, and Panic. For example:

logger.Debug("This is a debug message")
logger.Info("This is an info message")
logger.Error("This is an error message")

You can also log messages with formatting:

logger.Infof("Formatted message with a value: %d", 42)
Adding Fields

The library allows you to add structured fields to log entries using the WithField and WithFields methods. Fields provide additional context to your log messages:

logger.WithField("user_id", 123).Info("User logged in")
Using WithContextFields

The WithContextFields function helps you retrieve fields from contexts and add them as fields to log entries:

logger.WithContextFields(ctx).Info("Processing request")
Customizing Logging Level

You can change the logging level dynamically using the SetLevel function:

logger.SetLevel("info") // Set the logging level to "info"

Contributing

Contributions are welcome! If you find any issues, have suggestions, or want to add new features, feel free to open an issue or submit a pull request on the GitHub repository.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LoggerCtxFieldsKey ctxKeyType = "logger.fields"

Functions

func AppendFieldsToContext added in v0.2.0

func AppendFieldsToContext(ctx context.Context, fields ...Field) context.Context

AppendFieldsToContext appends the given fields to the context

func CallerSkip

func CallerSkip(skip int) option

CallerSkip add specified skip frames while reporting caller should be used to make caller to appropriate frame if this pkg is not used in vanilla form. Multiple calls are not additive.

func Debug

func Debug(args ...interface{})

Debug logs a message at DEBUG level using the default logger

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a message at DEBUG level using the default logger

func Debugln

func Debugln(args ...interface{})

Debugln logs a message at DEBUG level using the default logger

func DefaultZapLogger

func DefaultZapLogger() *zap.Logger

func Error

func Error(args ...interface{})

Error logs a message at ERROR level using the default logger

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a message at ERROR level using the default logger

func Errorln

func Errorln(args ...interface{})

Errorln logs a message at ERROR level using the default logger

func Formatter

func Formatter(format string) option

Formatter sets the log format to development friendly text or production friendly JSON based on the config value Allowed values: "text", "json"

func GetFieldValueFromContext added in v0.3.0

func GetFieldValueFromContext(ctx context.Context, FieldName string) string

GetFieldValueFromContext returns the string value from the context

func GetLevel

func GetLevel() string

GetLevel returns the current logging level Logging levels can be - "debug", "info", "warning", "error", "fatal"

func Info

func Info(args ...interface{})

Info logs a message at INFO level using the default logger

func Infof

func Infof(format string, args ...interface{})

Infof logs a message at INFO level using the default logger

func Infoln

func Infoln(args ...interface{})

Infoln logs a message at INFO level using the default logger

func Initialize

func Initialize(options ...option) (err error)

Initialize the logger with configuration options Follows the "Functional Options Pattern" for configuration. Described at: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

func Level

func Level(level string) option

Level sets the logging level. Allowed values in decreasing order of verbosity - "debug", "info", "warning", "error", "fatal", "panic" Setting a higher logging level will ignore logs from lower levels in output Default: "info"

func Panic

func Panic(args ...interface{})

Panic logs a message at PANIC level using the default logger

func Panicf

func Panicf(format string, args ...interface{})

Panicf logs a message at PANIC level using the default logger

func Panicln

func Panicln(args ...interface{})

Panicln logs a message at PANIC level using the default logger

func Print

func Print(args ...interface{})

Print logs a message at INFO level using the default logger.

func Printf

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

Printf logs a message at INFO level using the default logger.

func Println

func Println(args ...interface{})

Println logs a message at INFO level using the default logger

func SetLevel

func SetLevel(level string)

SetLevel sets the current logging level

func Sync

func Sync() error

Sync flushes any buffered log entries

func Warn

func Warn(args ...interface{})

Warn logs a message at WARNING level using the default logger

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a message at WARNING level using the default logger

func Warnln

func Warnln(args ...interface{})

Warnln logs a message at WARNING level using the default logger

Types

type Field

type Field = zap.Field

func Any

func Any(key string, val interface{}) Field

func Bool

func Bool(key string, val bool) Field

func Bools

func Bools(key string, val []bool) Field

func ByteString

func ByteString(key string, val []byte) Field

func ByteStrings

func ByteStrings(key string, val [][]byte) Field

func Duration

func Duration(key string, val time.Duration) Field

func Durationp

func Durationp(key string, val *time.Duration) Field

func Durations

func Durations(key string, val []time.Duration) Field

func Err

func Err(err error) Field

func Float32

func Float32(key string, val float32) Field

func Float32p

func Float32p(key string, val *float32) Field

func Float32s

func Float32s(key string, val []float32) Field

func Float64

func Float64(key string, val float64) Field

func Float64p

func Float64p(key string, val *float64) Field

func Float64s

func Float64s(key string, val []float64) Field

func Int

func Int(key string, val int) Field

func Int16

func Int16(key string, val int16) Field

func Int16p

func Int16p(key string, val *int16) Field

func Int16s

func Int16s(key string, val []int16) Field

func Int32

func Int32(key string, val int32) Field

func Int32p

func Int32p(key string, val *int32) Field

func Int32s

func Int32s(key string, val []int32) Field

func Int64

func Int64(key string, val int64) Field

func Int64p

func Int64p(key string, val *int64) Field

func Int64s

func Int64s(key string, val []int64) Field

func Int8

func Int8(key string, val int8) Field

func Int8p

func Int8p(key string, val *int8) Field

func Int8s

func Int8s(key string, val []int8) Field

func Intp

func Intp(key string, val *int) Field

func Ints

func Ints(key string, val []int) Field

func Stack

func Stack(key string) Field

Stack adds the current stacktrace to the log

func String

func String(key string, val string) Field

func Stringer

func Stringer(key string, val fmt.Stringer) Field

func Stringp

func Stringp(key string, val *string) Field

func Strings

func Strings(key string, val []string) Field

func Time

func Time(key string, val time.Time) Field

func Timep

func Timep(key string, val *time.Time) Field

func Times

func Times(key string, val []time.Time) Field

func Uint

func Uint(key string, val uint) Field

func Uint16

func Uint16(key string, val uint16) Field

func Uint16p

func Uint16p(key string, val *uint16) Field

func Uint16s

func Uint16s(key string, val []uint16) Field

func Uint32

func Uint32(key string, val uint32) Field

func Uint32p

func Uint32p(key string, val *uint32) Field

func Uint32s

func Uint32s(key string, val []uint32) Field

func Uint64

func Uint64(key string, val uint64) Field

func Uint64p

func Uint64p(key string, val *uint64) Field

func Uint64s

func Uint64s(key string, val []uint64) Field

func Uint8

func Uint8(key string, val uint8) Field

func Uint8p

func Uint8p(key string, val *uint8) Field

func Uint8s

func Uint8s(key string, val []uint8) Field

func Uintp

func Uintp(key string, val *uint) Field

func Uints

func Uints(key string, val []uint) Field

type Logger

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

func DefaultLogger added in v0.2.0

func DefaultLogger() *Logger

func NewLogger

func NewLogger(opts ...option) (*Logger, error)

func With

func With(fields ...Field) *Logger

With adds multiple fields to the logger. Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Logger it returns.

func WithContextFields added in v0.2.0

func WithContextFields(ctx context.Context) *Logger

WithContextFields logs messages with the fields added to the context

func WithError

func WithError(err error) *Logger

WithError creates an entry from the standard logger and adds an error to it.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithField

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

WithField creates an entry from the standard logger and adds a field to it.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithFields

func WithFields(fields map[string]interface{}) *Logger

WithFields creates an entry from the standard logger and adds multiple fields to it.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

Note: Use With instead for structured fields for better performance

func (*Logger) Debug

func (logger *Logger) Debug(args ...interface{})

Debug logs a message at DEBUG level using the provided logger

func (*Logger) Debugf

func (logger *Logger) Debugf(format string, args ...interface{})

Debugf logs a message at DEBUG level using the provided logger

func (*Logger) Debugln

func (logger *Logger) Debugln(args ...interface{})

Debugln logs a message at DEBUG level using the provided logger

func (*Logger) Error

func (logger *Logger) Error(args ...interface{})

Error logs a message at ERROR level using the provided logger

func (*Logger) Errorf

func (logger *Logger) Errorf(format string, args ...interface{})

Errorf logs a message at ERROR level using the provided logger

func (*Logger) Errorln

func (logger *Logger) Errorln(args ...interface{})

Errorln logs a message at ERROR level using the provided logger

func (*Logger) GetLevel

func (logger *Logger) GetLevel() string

GetLevel returns the current logging level Logging levels can be - "debug", "info", "warning", "error", "fatal"

func (*Logger) Info

func (logger *Logger) Info(args ...interface{})

Info logs a message at INFO level using the provided logger

func (*Logger) Infof

func (logger *Logger) Infof(format string, args ...interface{})

Infof logs a message at INFO level using the provided logger

func (*Logger) Infoln

func (logger *Logger) Infoln(args ...interface{})

Infoln logs a message at INFO level using the provided logger

func (*Logger) Panic

func (logger *Logger) Panic(args ...interface{})

Panic logs a message at PANIC level using the provided logger

func (*Logger) Panicf

func (logger *Logger) Panicf(format string, args ...interface{})

Panicf logs a message at PANIC level using the given logger

func (*Logger) Panicln

func (logger *Logger) Panicln(args ...interface{})

Panicln logs a message at PANIC level using the given logger

func (*Logger) Print

func (logger *Logger) Print(args ...interface{})

Print logs a message at INFO level using the provided logger.

func (*Logger) Printf

func (logger *Logger) Printf(format string, args ...interface{})

Printf logs a message at INFO level using the provided logger.

func (*Logger) Println

func (logger *Logger) Println(args ...interface{})

Println logs a message at INFO level using the provided logger

func (*Logger) SetLevel

func (logger *Logger) SetLevel(level string)

SetLevel sets the current logging level

func (*Logger) Sync

func (logger *Logger) Sync() error

Sync flushes any buffered log entries

func (*Logger) Warn

func (logger *Logger) Warn(args ...interface{})

Warn logs a message at WARNING level using the provided logger

func (*Logger) Warnf

func (logger *Logger) Warnf(format string, args ...interface{})

Warnf logs a message at WARNING level using the provided logger

func (*Logger) Warnln

func (logger *Logger) Warnln(args ...interface{})

Warnln logs a message at WARNING level using the provided logger

func (*Logger) With

func (logger *Logger) With(fields ...Field) *Logger

With adds multiple fields to the logger. Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Logger it returns.

func (*Logger) WithError

func (logger *Logger) WithError(err error) *Logger

WithContext adds an error as single field to the Entry.

func (*Logger) WithField

func (logger *Logger) WithField(key string, value interface{}) *Logger

WithField creates an entry from the standard logger and adds a field to it.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func (*Logger) WithFields

func (logger *Logger) WithFields(fields map[string]interface{}) *Logger

WithFields creates an entry from the standard logger and adds multiple fields to it.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

Note: Use With instead for structured fields for better performance

Jump to

Keyboard shortcuts

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