The logging package provides a simple, flexible, and color-coded logging system for Golang. Below are the main features and methods of the package:
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 anyio.Writer). Defaults toos.Stdoutifnil.
- DEBUG: Used for detailed debug information.
- INFO: General informational messages.
- WARN: Warnings about potential issues.
- ERROR: Critical errors.
-
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.
-
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 formatYYYY-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 anyio.Writer, allowing flexible output destinations (e.g., files, network connections). -
Disable Colors:
ThedisableColorsfield in theLoggerstruct can be set totrueto disable color codes (useful for testing or plain-text logs).
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:orapi_key=). - Matches the key plus the following non-space characters and replaces them with the key concatenated with the provided replacement.
- Example:
{"password:": "***REDACTED***"}turnspassword:secretintopassword:***REDACTED***.
- Treats each map key as a literal pattern prefix (e.g.,
-
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.
- If the
minLevelis set toDEBUG, 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
disableColorsfield totruein theLoggerinstance.
For examples of each function, please checkout EXAMPLES.md