logger

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 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)

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.LevelEnabler, 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