Documentation
¶
Overview ¶
Package log provides a simple logging package with support for hierarchical logging and colors.
The package supports four built-in log levels with their own prefix symbols and colors:
- Debug: » (light gray, only shown when verbose mode is enabled)
- Info: + (blue)
- Success: ✓ (green)
- Error: × (red)
The package also supports hierarchical logging with indentation levels and custom prefixes. You can use:
- L(level) to specify the indentation level (each level adds 2 spaces)
- P(prefix) to specify a custom prefix
- C(color) to specify a custom color
Basic usage:
// Simple logging with built-in levels (with default colors)
log.Info("Processing item %d", 1)
// Output: + Processing item 1 (in blue)
// Hierarchical logging with custom prefix and colors
log.Info("Found 2 items").
L(1).P("→").C(log.ColorCyan).Log("Processing item 1").
L(1).Success("Item 1 processed").
L(1).P("→").C(log.ColorCyan).Log("Processing item 2").
L(1).Error("Failed to process item 2")
// Output:
// + Found 2 items (in blue)
// → Processing item 1 (in cyan)
// ✓ Item 1 processed (in green)
// → Processing item 2 (in cyan)
// × Failed to process item 2 (in red)
// Debug logging (only shown when verbose mode is enabled)
log.SetVerbose(true)
log.Debug("Debug message")
// Output: » Debug message (in light gray)
All logging functions return a new Logger pointer, allowing for method chaining:
// L(level) sets the indentation level
// P(prefix) sets a custom prefix
// C(color) sets a custom color
// Log() outputs message with current level, prefix and color
log.L(1).P("→").C(log.ColorYellow).Log("Message 1").Log("Message 2")
// Output:
// → Message 1 (in yellow)
// → Message 2 (in yellow)
Each method (L, P, C, Log, etc.) returns a new Logger instance with the updated settings, making it safe for concurrent use and allowing for flexible logging patterns.
Available colors for use with C():
- log.ColorReset (reset to default color)
- log.ColorRed (red)
- log.ColorGreen (green)
- log.ColorYellow (yellow)
- log.ColorBlue (blue)
- log.ColorPurple (purple)
- log.ColorCyan (cyan)
- log.ColorGray (light gray)
- log.StyleBold (bold style)
Index ¶
- Variables
- func SetNoColor(disable bool)
- func SetVerbose(v bool)
- type Logger
- func C(color string) *Logger
- func Debug(format string, args ...interface{}) *Logger
- func Error(format string, args ...interface{}) *Logger
- func Info(format string, args ...interface{}) *Logger
- func L(level int) *Logger
- func Log(format string, args ...interface{}) *Logger
- func N() *Logger
- func New() *Logger
- func P(prefix string) *Logger
- func Success(format string, args ...interface{}) *Logger
- func (l *Logger) C(color string) *Logger
- func (l *Logger) Debug(format string, args ...interface{}) *Logger
- func (l *Logger) Error(format string, args ...interface{}) *Logger
- func (l *Logger) Info(format string, args ...interface{}) *Logger
- func (l *Logger) L(level int) *Logger
- func (l *Logger) Log(format string, args ...interface{}) *Logger
- func (l *Logger) N() *Logger
- func (l *Logger) P(prefix string) *Logger
- func (l *Logger) Success(format string, args ...interface{}) *Logger
Constants ¶
This section is empty.
Variables ¶
var ( ColorReset = colorReset ColorRed = colorRed ColorGreen = colorGreen ColorYellow = colorYellow ColorBlue = colorBlue ColorPurple = colorPurple ColorCyan = colorCyan ColorGray = colorGray StyleBold = styleBold )
Color constants for use with C() method
Functions ¶
func SetVerbose ¶
func SetVerbose(v bool)
SetVerbose sets the verbose flag. When verbose is true, Debug messages will be printed. When verbose is false, Debug messages will be suppressed.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents a logger with a specific indentation level and prefix
func C ¶
C sets the color of the logger and returns a new logger. The color will be used by subsequent Log calls.
Example:
log.L(1).C(colorRed).Log("Error message")
// Output:
// Error message (in red)
func L ¶
L sets the indentation level and returns a new logger. Each level adds 2 spaces of indentation.
Example:
log.L(1).P("→").Log("Child message")
// Output:
// → Child message
func N ¶
func N() *Logger
N is a convenience function that creates a new logger and disables the newline
func P ¶
P sets a custom prefix for the logger and returns a new logger. The prefix will be used by subsequent Log calls.
Example:
log.L(1).P("→").Log("Child message")
// Output:
// → Child message
func (*Logger) C ¶
C sets the color of the logger and returns a new logger. The color will be used by subsequent Log calls.
Example:
log.L(1).C(colorRed).Log("Error message")
// Output:
// Error message (in red)
func (*Logger) Debug ¶
Debug prints debug message if verbose is true and returns a new logger. Debug messages are prefixed with "»" and are only shown when verbose mode is enabled.
Example:
log.SetVerbose(true)
log.Debug("Processing data")
// Output: » Processing data
func (*Logger) Error ¶
Error prints error message and returns a new logger. Error messages are prefixed with "×".
Example:
log.Error("Failed to process item: %v", err)
// Output: × Failed to process item: connection refused
func (*Logger) Info ¶
Info prints info message and returns a new logger. Info messages are prefixed with "+".
Example:
log.Info("Processing %d items", 5)
// Output: + Processing 5 items
func (*Logger) L ¶
L sets the indentation level and returns a new logger. Each level adds 2 spaces of indentation.
Example:
log.P("→").L(1).Log("Child message")
// Output:
// → Child message
func (*Logger) Log ¶
Log prints message with current level and prefix, then returns a new logger.
Example:
log.L(1).P("→").Log("Message 1").Log("Message 2")
// Output:
// → Message 1
// → Message 2