Documentation
¶
Overview ¶
Package log provides a simple logging package with support for hierarchical logging and colors.
The package supports five built-in log levels with their own prefix symbols and colors:
- Debug: + (light gray, only shown when verbose mode is enabled)
- Info: » (blue)
- Warn: ! (yellow)
- 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
- N() to disable newline at the end of the message
- B() to set the text to bold
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)
// Disable newline at the end of the message
log.N().Info("Enter your name: ")
// Output: » Enter your name: (without newline)
// Using bold text
// Method 1: Make entire message bold
log.B().Info("This entire message is bold")
// Output: » This entire message is bold (in bold)
// Method 2: Make part of the message bold
log.Info("Current user: %s", log.Bold("elliotxx"))
// Output: » Current user: elliotxx (with "elliotxx" in bold)
// Method 3: Multiple bold parts
log.Info("Found %s issues, %s are critical", log.Bold("10"), log.Bold("5"))
// Output: » Found 10 issues, 5 are critical (with "10" and "5" in bold)
// Method 4: Combine bold with colors
log.B().C(log.ColorRed).Error("Critical error: %s", log.Bold("permission denied"))
// Output: × Critical error: permission denied (in red, with entire message and "permission denied" in bold)
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
// B() sets the text to bold
// Log() outputs message with current level, prefix and color
log.L(1).P("→").C(log.ColorYellow).B().Log("Message 1").Log("Message 2")
// Output:
// → Message 1 (in yellow and bold)
// → Message 2 (in yellow and bold)
Each method (L, P, C, B, 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 Bold(text string) string
- func SetNoColor(disable bool)
- func SetVerbose(v bool)
- type Logger
- func B() *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 Warn(format string, args ...interface{}) *Logger
- func (l *Logger) B() *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
- func (l *Logger) Warn(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 Bold ¶ added in v0.1.3
Bold wraps text in bold style
Example:
log.Info("Normal text %s more text", log.Bold("bold text"))
// Output: » Normal text bold text more text (with "bold text" in bold)
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 B ¶ added in v0.1.3
func B() *Logger
B is a convenience function that creates a new logger with bold text.
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 Warn ¶ added in v0.1.6
Warn is a convenience function that creates a new logger and calls Warn.
func (*Logger) B ¶ added in v0.1.3
B sets the text to bold and returns a new logger.
Example:
log.B().Info("This is bold")
// Output: » This is bold (in bold)
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("Debug message")
// Output: + Debug message
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
func (*Logger) P ¶
P sets a custom prefix for the logger and returns the logger. The prefix will be used by subsequent Log calls.
Example:
log.L(1).P("→").Log("Processing item")
// Output:
// → Processing item