logger

package
v0.0.36 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package logger provides a structured logging interface for Go applications.

This package defines a Log interface with methods for logging messages at different levels: Info, Warn, Error, and Debug. It also includes an implementation of this interface, SlogAdapter, which uses the slog.Logger for logging.

The logger package is designed to be flexible and easy to use, allowing developers to log messages with varying levels of severity. The package also includes a utility function, GetLevelFromEnv, which reads the LOG_LEVEL environment variable and returns the corresponding slog.Level.

Example usage:

// Create a new logger instance
logger := logger.NewLogger()

// Log messages at different levels
logger.Info("This is an info message")
logger.Warn("This is a warning message")
logger.Error("This is an error message")
logger.Debug("This is a debug message")

The logger package is intended to be used in applications where structured logging is required. It provides a consistent and easy-to-use interface for logging messages, making it easier to manage and analyze log data.

For more information, visit the Go.pkg.dev documentation page for this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLevelFromEnv

func GetLevelFromEnv() slog.Level

GetLevelFromEnv

This function reads the LOG_LEVEL environment variable and returns the corresponding slog.Level. If the LOG_LEVEL environment variable is not set, it returns slog.LevelInfo.

Returns:

  • The slog.Level corresponding to the LOG_LEVEL environment variable.

Example:

level := GetLevelFromEnv()

Types

type Log

type Log interface {
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Error(msg string, args ...any)
	Debug(msg string, args ...any)
}

Log is an interface that defines the methods for logging.

The methods are used to log messages at different levels. The levels are Info, Warn, Error, and Debug.

Example:

logger := logger.NewLogger()
logger.Info("This is an info message")
logger.Warn("This is a warning message")
logger.Error("This is an error message")
logger.Debug("This is a debug message")

type SlogAdapter

type SlogAdapter struct {
	Logger Log
}

SlogAdapter is a struct that implements the Log interface.

It contains a slog.Logger instance. The slog.Logger instance is used for logging.

func NewLogger

func NewLogger() *SlogAdapter

NewLogger creates a new SlogAdapter instance with a slog.Logger.

It reads the LOG_FORMAT environment variable to determine the log format (text or json). If the LOG_FORMAT environment variable is not set, it defaults to text.

Returns:

  • A new SlogAdapter instance with a slog.Logger.

Example:

logger := logger.NewLogger()
logger.Info("This is an info message")
logger.Warn("This is a warning message")
logger.Error("This is an error message")
logger.Debug("This is a debug message")

Jump to

Keyboard shortcuts

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