logging

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 5 Imported by: 0

README

Logging

The logging package provides a simple, flexible, and color-coded logging system for Golang. Below are the main features and methods of the package:

Logger Constructor

  • NewLogger(prefix string, minLevel LogLevel, output io.Writer) *Logger:
    Creates a new logger instance.
    • prefix: A string prefix added to all log messages.
    • minLevel: The minimum log level to output (DEBUG, INFO, WARN, ERROR). Messages below this level are ignored.
    • output: The destination for log output (e.g., os.Stdout, os.Stderr, or any io.Writer). Defaults to os.Stdout if nil.

Log Levels

  • DEBUG: Used for detailed debug information.
  • INFO: General informational messages.
  • WARN: Warnings about potential issues.
  • ERROR: Critical errors.

Logger Methods

  • Info(message string):
    Logs an informational message with the log level INFO.

  • Debug(message string):
    Logs a debug message with the log level DEBUG.

  • Warn(message string):
    Logs a warning message with the log level WARN.

  • Error(message string):
    Logs an error message with the log level ERROR.

Key Features

  • Color-Coded Logs:
    Log messages are color-coded based on the log level:

    • DEBUG: Green
    • INFO: Blue
    • WARN: Yellow
    • ERROR: Red
  • Timestamped Logs:
    Each log message includes a timestamp in the format YYYY-MM-DD HH:MM:SS.

  • Log Filtering by Level:
    Logs below the specified minimum level (minLevel) are ignored.

  • Custom Output:
    Logs can be directed to any io.Writer, allowing flexible output destinations (e.g., files, network connections).

  • Disable Colors:
    The disableColors field in the Logger struct can be set to true to disable color codes (useful for testing or plain-text logs).

Redaction

The logging package can redact sensitive values before writing logs. Two methods are provided:

  • SetRedactionRules(rules map[string]string)

    • Treats each map key as a literal pattern prefix (e.g., password: or api_key=).
    • Matches the key plus the following non-space characters and replaces them with the key concatenated with the provided replacement.
    • Example: {"password:": "***REDACTED***"} turns password:secret into password:***REDACTED***.
  • SetRedactionRegex(patterns map[string]string) error

    • Accepts full regular expressions as keys and replacement strings as values.
    • Compiles each regex and returns an error if any pattern is invalid.
    • The replacement string replaces the matched substring.

Notes

  • If the minLevel is set to DEBUG, all log messages will be displayed.
  • Logs are automatically flushed to the configured output as soon as they're written.
  • To log without colors (e.g., for testing), set the disableColors field to true in the Logger instance.

Examples:

For examples of each function, please checkout EXAMPLES.md


Documentation

Overview

Package logging provides a simple logging library with support for multiple log levels, custom prefixes, colored output, and customizable output streams.

Index

Constants

View Source
const (
	ColorReset  = "\033[0m"  // Reset to default color
	ColorBlue   = "\033[34m" // Blue color for INFO level logs
	ColorGreen  = "\033[32m" // Green color for DEBUG level logs
	ColorYellow = "\033[33m" // Yellow color for WARN level logs
	ColorRed    = "\033[31m" // Red color for ERROR level logs
)

ANSI color codes for different log levels

Variables

This section is empty.

Functions

This section is empty.

Types

type LogLevel

type LogLevel int

LogLevel represents the severity level of a log message.

const (
	DEBUG LogLevel = iota // DEBUG is used for detailed information during development.
	INFO                  // INFO is used for general informational messages.
	WARN                  // WARN is used for warnings that are not critical.
	ERROR                 // ERROR is used for critical error messages.
)

Defined log levels in increasing order of severity.

type Logger

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

Logger is a configurable logging instance that supports multiple log levels, optional colored output, and custom prefixes for log messages.

func NewLogger

func NewLogger(prefix string, minLevel LogLevel, output io.Writer) *Logger

NewLogger creates and returns a new Logger instance with the specified prefix, minimum log level, and output destination. If output is nil, os.Stdout is used as the default destination.

Parameters:

  • prefix: A string prefix that will appear in all log messages.
  • minLevel: The minimum log level for a message to be logged.
  • output: The io.Writer to which log messages will be written.

Returns:

A pointer to a configured Logger instance.

func (*Logger) Debug

func (l *Logger) Debug(message string)

Debug logs a message at the DEBUG level.

Parameters:

  • message: The debug message to log.

func (*Logger) Error

func (l *Logger) Error(message string)

Error logs a message at the ERROR level.

Parameters:

  • message: The error message to log.

func (*Logger) Info

func (l *Logger) Info(message string)

Info logs a message at the INFO level.

Parameters:

  • message: The informational message to log.

func (*Logger) SetRedactionRegex added in v1.26.0

func (l *Logger) SetRedactionRegex(patterns map[string]string) error

SetRedactionRegex allows users to define custom regex patterns for redaction. This provides more flexibility than SetRedactionRules.

Parameters:

  • patterns: A map where keys are regex patterns and values are replacement strings.

func (*Logger) SetRedactionRules added in v1.26.0

func (l *Logger) SetRedactionRules(rules map[string]string)

SetRedactionRules configures the logger to redact sensitive information based on the provided patterns. Each key represents a pattern to match, and the value is the replacement text.

Parameters:

  • rules: A map where keys are patterns (e.g., "password=", "credit_card=") and values are replacement strings (e.g., "***REDACTED***").

func (*Logger) Warn

func (l *Logger) Warn(message string)

Warn logs a message at the WARN level.

Parameters:

  • message: The warning message to log.

type RedactionRule added in v1.26.0

type RedactionRule struct {
	Pattern     *regexp.Regexp // Regex pattern to match sensitive data
	Replacement string         // Replacement text for matched patterns
}

RedactionRule defines how to redact sensitive information

Jump to

Keyboard shortcuts

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