logger

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 7 Imported by: 9

README

Usage

Importing the Package 📦

First, import the package in your Go file:

import "github.com/catalystgo/logger/logger"

Setting Up the Logger 🛠️

The logger can be initialized with a specific log level and options. By default, the log level is set to ErrorLevel.

logger.SetLogger(logger.New(zapcore.DebugLevel))

Logging Messages 📝

You can log messages at different levels using the following functions:

  • Debug(ctx context.Context, args ...interface{})
  • Debugf(ctx context.Context, format string, args ...interface{})
  • DebugKV(ctx context.Context, message string, kvs ...interface{})
  • Info(ctx context.Context, args ...interface{})
  • Infof(ctx context.Context, format string, args ...interface{})
  • InfoKV(ctx context.Context, message string, kvs ...interface{})
  • Warn(ctx context.Context, args ...interface{})
  • Warnf(ctx context.Context, format string, args ...interface{})
  • WarnKV(ctx context.Context, message string, kvs ...interface{})
  • Error(ctx context.Context, args ...interface{})
  • Errorf(ctx context.Context, format string, args ...interface{})
  • ErrorKV(ctx context.Context, message string, kvs ...interface{})
  • Fatal(ctx context.Context, args ...interface{})
  • Fatalf(ctx context.Context, format string, args ...interface{})
  • FatalKV(ctx context.Context, message string, kvs ...interface{})
  • Panic(ctx context.Context, args ...interface{})
  • Panicf(ctx context.Context, format string, args ...interface{})
  • PanicKV(ctx context.Context, message string, kvs ...interface{})

Examples 🚀

Here are some examples of how to use the logging functions:

ctx := context.Background()

logger.Debug(ctx, "This is a debug message")
logger.Debugf(ctx, "This is a debug message with a variable: %d", 42)
logger.DebugKV(ctx, "This is a debug message with key-value pairs", "key1", "value1", "key2", "value2")

logger.Info(ctx, "This is an info message")
logger.Infof(ctx, "This is an info message with a variable: %d", 42)
logger.InfoKV(ctx, "This is an info message with key-value pairs", "key1", "value1", "key2", "value2")

logger.Warn(ctx, "This is a warning message")
logger.Warnf(ctx, "This is a warning message with a variable: %d", 42)
logger.WarnKV(ctx, "This is a warning message with key-value pairs", "key1", "value1", "key2", "value2")

logger.Error(ctx, "This is an error message")
logger.Errorf(ctx, "This is an error message with a variable: %d", 42)
logger.ErrorKV(ctx, "This is an error message with key-value pairs", "key1", "value1", "key2", "value2")

logger.Fatal(ctx, "This is a fatal message")
logger.Fatalf(ctx, "This is a fatal message with a variable: %d", 42)
logger.FatalKV(ctx, "This is a fatal message with key-value pairs", "key1", "value1", "key2", "value2")

logger.Panic(ctx, "This is a panic message")
logger.Panicf(ctx, "This is a panic message with a variable: %d", 42)
logger.PanicKV(ctx, "This is a panic message with key-value pairs", "key1", "value1", "key2", "value2")

Getting and Setting the Log Level 📏

You can get and set the current log level using the Level and SetLevel functions.

currentLevel := logger.Level()
logger.SetLevel(zapcore.InfoLevel)

Global Logger 🌐

You can get and set the global logger using the Logger and SetLogger functions.

globalLogger := logger.Logger()
logger.SetLogger(globalLogger)

License 📑

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing 🤝

Contributions are welcome! Please feel free to submit a pull request or open an issue.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddKV

func AddKV(ctx context.Context, kvs ...any) context.Context

AddKV Adds KV pairs to

Example
ctx := AddKV(
	context.Background(),
	zap.String("a", "b"),
	zap.Int("c", 1),
	zap.Error(assert.AnError),
)

// В сообщении появятся поля, переданные в AddKV.
Info(ctx, "some message")

func Debug

func Debug(ctx context.Context, args ...interface{})

func DebugKV

func DebugKV(ctx context.Context, message string, kvs ...interface{})

func Debugf

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

func Error

func Error(ctx context.Context, args ...interface{})

func ErrorKV

func ErrorKV(ctx context.Context, message string, kvs ...interface{})

func Errorf

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

func Fatal

func Fatal(ctx context.Context, args ...interface{})

func FatalKV

func FatalKV(ctx context.Context, message string, kvs ...interface{})

func Fatalf

func Fatalf(ctx context.Context, format string, args ...interface{})

func FromContext

func FromContext(ctx context.Context) *zap.SugaredLogger

FromContext Gets the logger from contet

func Info

func Info(ctx context.Context, args ...interface{})

func InfoKV

func InfoKV(ctx context.Context, message string, kvs ...interface{})

func Infof

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

func Level

func Level() zapcore.Level

Level Get current log_level

func LevelFromContext

func LevelFromContext(ctx context.Context) zapcore.Level

LevelFromContext Gets the log_level from the context logger

func Logger

func Logger() *zap.SugaredLogger

Logger Get global logger

func New

func New(level zapcore.Level, options ...zap.Option) *zap.SugaredLogger

New create a new logger with specific log_leve & options

func NewWithSink

func NewWithSink(level zapcore.LevelEnabler, sink io.Writer, options ...zap.Option) *zap.SugaredLogger

func Panic

func Panic(ctx context.Context, args ...interface{})

func PanicKV

func PanicKV(ctx context.Context, message string, kvs ...interface{})

func Panicf

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

func SetLevel

func SetLevel(l zapcore.Level)

SetLevel Set current log_level

func SetLogger

func SetLogger(l *zap.SugaredLogger)

SetLogger Set global logger (not thread safe)

func ToContext

func ToContext(ctx context.Context, l *zap.SugaredLogger) context.Context

ToContext Attaches a logger to context

Example
logger := Logger()

customLogger := logger.With(
	zap.String("component", "database"),
)

ctx := ToContext(context.Background(), customLogger)

loggerFromContext := FromContext(ctx)

// Будет содержать дополнительное поле component.
loggerFromContext.Info("some message")

func Warn

func Warn(ctx context.Context, args ...interface{})

func WarnKV

func WarnKV(ctx context.Context, message string, kvs ...interface{})

func Warnf

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

func WithFields

func WithFields(ctx context.Context, fields ...zap.Field) context.Context

WithFields Adds fields to logger from context

Example
// context, containing logger
ctx := context.Background()
// new context with logger fields
ctx = WithFields(ctx,
	zap.String("kafka-topic", "my topic"),
	zap.Int32("kafka-partition", 1),
)

_ = ctx

func WithKV

func WithKV(ctx context.Context, key string, value any) context.Context

WithKV Adds KV pair to logger from context

Example
// context, containing logger
ctx := context.Background()
// new context with logger fields
ctx = WithKV(ctx, "my key", "my value")

_ = ctx

func WithLevel

func WithLevel(lvl zapcore.Level) zap.Option

WithLevel option that allows you to create a logger with a specified level from an existing one

Example
Logger().Desugar().WithOptions(WithLevel(zapcore.DebugLevel)).Sugar()

func WithName

func WithName(ctx context.Context, name string) context.Context

WithName Set a name for the logger

Example
// context, containing logger
ctx := context.Background()
ctx = WithName(ctx, "GetApples")    // -> "GetApples"
ctx = WithName(ctx, "AppleManager") // - > "GetApples.AppleManager"
ctx = WithName(ctx, "DB")           // -> "GetApples.AppleManager.DB"

_ = ctx

Types

This section is empty.

Jump to

Keyboard shortcuts

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