log

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: MIT Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func Bold(text string) string

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 SetNoColor

func SetNoColor(disable bool)

SetNoColor sets the global color output setting

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

func C(color string) *Logger

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 Debug

func Debug(format string, args ...interface{}) *Logger

Debug is a convenience function that creates a new logger and calls Debug.

func Error

func Error(format string, args ...interface{}) *Logger

Error is a convenience function that creates a new logger and calls Error.

func Info

func Info(format string, args ...interface{}) *Logger

Info is a convenience function that creates a new logger and calls Info.

func L

func L(level int) *Logger

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 Log

func Log(format string, args ...interface{}) *Logger

Log is a convenience function that creates a new logger and calls Log.

func N

func N() *Logger

N is a convenience function that creates a new logger and disables the newline

func New

func New() *Logger

New creates a new logger with default settings (level 0, no prefix)

func P

func P(prefix string) *Logger

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 Success

func Success(format string, args ...interface{}) *Logger

Success is a convenience function that creates a new logger and calls Success.

func Warn added in v0.1.6

func Warn(format string, args ...interface{}) *Logger

Warn is a convenience function that creates a new logger and calls Warn.

func (*Logger) B added in v0.1.3

func (l *Logger) B() *Logger

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

func (l *Logger) C(color string) *Logger

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

func (l *Logger) Debug(format string, args ...interface{}) *Logger

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

func (l *Logger) Error(format string, args ...interface{}) *Logger

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

func (l *Logger) Info(format string, args ...interface{}) *Logger

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

func (l *Logger) L(level int) *Logger

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

func (l *Logger) Log(format string, args ...interface{}) *Logger

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) N

func (l *Logger) N() *Logger

N disables the newline at the end of the log message

func (*Logger) P

func (l *Logger) P(prefix string) *Logger

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

func (*Logger) Success

func (l *Logger) Success(format string, args ...interface{}) *Logger

Success prints success message and returns a new logger. Success messages are prefixed with "✓".

Example:

log.Success("All items processed")
// Output: ✓ All items processed

func (*Logger) Warn added in v0.1.6

func (l *Logger) Warn(format string, args ...interface{}) *Logger

Warn prints warning message and returns a new logger. Warning messages are prefixed with "!".

Example:

log.Warn("Low disk space: %d%%", 10)
// Output: ! Low disk space: 10%

Jump to

Keyboard shortcuts

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