Documentation
¶
Overview ¶
Package fancylog provides stylized, non-blocking log output. It is designed to be fast and visually appealing with support for colors and structured logging via maps.
Index ¶
- Constants
- Variables
- func Blue(data []byte) []byte
- func Cyan(data []byte) []byte
- func Gray(data []byte) []byte
- func Green(data []byte) []byte
- func Orange(data []byte) []byte
- func Purple(data []byte) []byte
- func Red(data []byte) []byte
- type Color
- type ColorLogger
- func (cb *ColorLogger) Append(data []byte)
- func (cb *ColorLogger) AppendSpace()
- func (cb *ColorLogger) AppendWithColor(data []byte, color Color)
- func (cb *ColorLogger) Blue()
- func (cb *ColorLogger) BrightOrange()
- func (cb *ColorLogger) Cyan()
- func (cb *ColorLogger) Gray()
- func (cb *ColorLogger) Green()
- func (cb *ColorLogger) NicePurple()
- func (cb *ColorLogger) Off()
- func (cb *ColorLogger) Orange()
- func (cb *ColorLogger) Purple()
- func (cb *ColorLogger) Red()
- func (cb *ColorLogger) White()
- func (cb *ColorLogger) WriteColor(color Color)
- type FancyHttpLog
- type FancyLogger
- type FdWriter
- type FormatLog
- type HttpLog
- func (h *HttpLog) ConnectMethod(a map[string]any, status int)
- func (h *HttpLog) DebugHeaders() bool
- func (h *HttpLog) DeleteMethod(a map[string]any, status int)
- func (h *HttpLog) GetMethod(a map[string]any, status int)
- func (h *HttpLog) HeadMethod(a map[string]any, status int)
- func (h *HttpLog) OptionsMethod(a map[string]any, status int)
- func (h *HttpLog) PostMethod(a map[string]any, status int)
- func (h *HttpLog) PutMethod(a map[string]any, status int)
- func (h *HttpLog) TraceMethod(a map[string]any, status int)
- func (h *HttpLog) WithHeaders() FancyHttpLog
- type Level
- type Logger
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) DebugMap(v map[string]interface{})
- func (l *Logger) Debugf(format string, v ...interface{})
- func (l *Logger) Error(v ...interface{})
- func (l *Logger) ErrorMap(v map[string]interface{})
- func (l *Logger) Errorf(format string, v ...interface{})
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) FatalMap(v map[string]interface{})
- func (l *Logger) FatalMapWithCode(exit int, v map[string]interface{})
- func (l *Logger) FatalWithCode(exit int, v ...interface{})
- func (l *Logger) FatalWithCodef(format string, exit int, v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) HasColor() bool
- func (l *Logger) Info(v ...interface{})
- func (l *Logger) InfoMap(v map[string]interface{})
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) IsDebug() bool
- func (l *Logger) IsQuiet() bool
- func (l *Logger) IsTrace() bool
- func (l *Logger) Log(prefix Prefix, a ...any)
- func (l *Logger) LogMap(prefix Prefix, a map[string]any)
- func (l *Logger) Logf(prefix Prefix, format string, a ...any)
- func (l *Logger) NoQuiet() FancyLogger
- func (l *Logger) Quiet() FancyLogger
- func (l *Logger) SetDefaultTimeFn(timestampFunc TimestampFunc)
- func (l *Logger) SetTimestampColor(color Color)
- func (l *Logger) Trace(v ...interface{})
- func (l *Logger) TraceMap(v map[string]interface{})
- func (l *Logger) Tracef(format string, v ...interface{})
- func (l *Logger) Warn(v ...interface{})
- func (l *Logger) WarnMap(v map[string]interface{})
- func (l *Logger) Warnf(format string, v ...any)
- func (l *Logger) WithColor() FancyLogger
- func (l *Logger) WithDebug() FancyLogger
- func (l *Logger) WithTimestamp() FancyLogger
- func (l *Logger) WithTrace() FancyLogger
- func (l *Logger) WithoutColor() FancyLogger
- func (l *Logger) WithoutDebug() FancyLogger
- func (l *Logger) WithoutTimestamp() FancyLogger
- func (l *Logger) WithoutTrace() FancyLogger
- type MappedLog
- type Methods
- type Prefix
- type PrefixLog
- type PrefixText
- type StandardLog
- type TimestampFunc
Constants ¶
const DepthSkip = 3
Variables ¶
var (
// Get retrieves a buffer from the pool, creating one if necessary.
Get = _pool.Get
)
var HttpPrefixes = map[Level]Prefix{ GetLevel: { Text: GetLevel, Color: ColorCyan, }, DeleteLevel: { Text: DeleteLevel, Color: ColorCyan, }, ConnectLevel: { Text: ConnectLevel, Color: ColorCyan, }, HeadLevel: { Text: HeadLevel, Color: ColorCyan, }, OptionsLevel: { Text: OptionsLevel, Color: ColorCyan, }, PostLevel: { Text: PostLevel, Color: ColorCyan, }, PutLevel: { Text: PutLevel, Color: ColorCyan, }, TraceLevel: { Text: TraceLevel, Color: ColorCyan, }, }
HttpPrefixes maps HTTP levels to their corresponding Prefix configuration.
var Prefixes = map[Level]Prefix{ Fatal: { Text: Fatal, Color: ColorFatalRed, File: true, }, Error: { Text: Error, Color: ColorRed, }, Warn: { Text: Warn, Color: ColorOrange, }, Info: { Text: Info, Color: ColorGreen, }, Trace: { Text: Trace, Color: ColorCyan, File: true, }, Debug: { Text: Debug, Color: ColorPurple, }, }
Prefixes maps each Level to its corresponding Prefix configuration.
Functions ¶
Types ¶
type Color ¶
type Color []byte
Color represents an ANSI color escape sequence as a byte slice.
var ( ColorRed Color = []byte("\u001B[0;31m") ColorGreen Color = []byte("\u001B[0;32m") ColorOrange Color = []byte("\u001B[0;33m") ColorBlue Color = []byte("\u001B[0;34m") ColorPurple Color = []byte("\u001B[0;35m") ColorCyan Color = []byte("\u001B[0;36m") ColorGray Color = []byte("\u001B[0;37m") // ColorFatalRed is a special color for fatal errors (bold, red background). ColorFatalRed Color = []byte("\u001b[1m\u001b[31m\u001b[7m") ColorDarkOrange Color = []byte("\u001b[1m\u001b[38;5;202m") ColorBrightWhite Color = []byte("\u001b[1m\u001b[38;5;255m") ColorNicePurple Color = []byte("\u001b[1m\u001b[38;5;99m") )
func UncheckedCustomColor ¶
UncheckedCustomColor creates a custom Color from a byte slice without validation.
type ColorLogger ¶
ColorLogger is a wrapper around a pooled buffer for efficient log message construction with colors.
func NewColorLogger ¶
func NewColorLogger() ColorLogger
NewColorLogger creates a new ColorLogger instance using a buffer from the pool.
func (*ColorLogger) Append ¶
func (cb *ColorLogger) Append(data []byte)
Append appends a byte slice to the buffer.
func (*ColorLogger) AppendSpace ¶
func (cb *ColorLogger) AppendSpace()
AppendSpace appends a single space to the buffer.
func (*ColorLogger) AppendWithColor ¶
func (cb *ColorLogger) AppendWithColor(data []byte, color Color)
AppendWithColor appends a byte slice to the buffer wrapped with color escape sequences.
func (*ColorLogger) Blue ¶
func (cb *ColorLogger) Blue()
Blue applies blue color to the subsequent data.
func (*ColorLogger) BrightOrange ¶
func (cb *ColorLogger) BrightOrange()
BrightOrange applies dark orange color to the subsequent data.
func (*ColorLogger) Cyan ¶
func (cb *ColorLogger) Cyan()
Cyan applies cyan color to the subsequent data.
func (*ColorLogger) Gray ¶
func (cb *ColorLogger) Gray()
Gray applies gray color to the subsequent data.
func (*ColorLogger) Green ¶
func (cb *ColorLogger) Green()
Green applies green color to the subsequent data.
func (*ColorLogger) NicePurple ¶
func (cb *ColorLogger) NicePurple()
NicePurple applies a "nice" purple color to the subsequent data.
func (*ColorLogger) Off ¶
func (cb *ColorLogger) Off()
Off resets the color to the terminal default.
func (*ColorLogger) Orange ¶
func (cb *ColorLogger) Orange()
Orange applies orange color to the subsequent data.
func (*ColorLogger) Purple ¶
func (cb *ColorLogger) Purple()
Purple applies purple color to the subsequent data.
func (*ColorLogger) Red ¶
func (cb *ColorLogger) Red()
Red applies red color to the subsequent data.
func (*ColorLogger) White ¶
func (cb *ColorLogger) White()
White applies bright white color to the subsequent data.
func (*ColorLogger) WriteColor ¶
func (cb *ColorLogger) WriteColor(color Color)
WriteColor applies the given color to the subsequent data.
type FancyHttpLog ¶ added in v0.0.13
type FancyHttpLog interface {
FancyLogger
Methods
// WithHeaders enables logging of HTTP headers.
WithHeaders() FancyHttpLog
// DebugHeaders returns true if header logging is enabled.
DebugHeaders() bool
}
FancyHttpLog is an extension of FancyLogger specialized for HTTP request logging. It includes methods for logging various HTTP methods with status codes.
func NewHttpLogger ¶ added in v0.0.11
func NewHttpLogger(out FdWriter) FancyHttpLog
NewHttpLogger returns a new HttpLog instance with a predefined writer output.
func NewHttpLoggerWithError ¶ added in v0.0.11
func NewHttpLoggerWithError(out FdWriter, err FdWriter) FancyHttpLog
NewHttpLoggerWithError returns a new HttpLog instance with separate writers for standard and error output.
func NewHttpLoggerWithName ¶ added in v0.0.11
func NewHttpLoggerWithName(name string, out FdWriter) FancyHttpLog
NewHttpLoggerWithName returns a new HttpLog instance with a specific name and writer output.
func NewHttpLoggerWithNameAndError ¶ added in v0.0.11
func NewHttpLoggerWithNameAndError(name string, out FdWriter, err FdWriter) FancyHttpLog
NewHttpLoggerWithNameAndError returns a new HttpLog instance with a specific name and separate writers for standard and error output.
type FancyLogger ¶ added in v0.0.11
type FancyLogger interface {
StandardLog
FormatLog
MappedLog
PrefixLog
// WithColor explicitly enables colorful features on the log.
WithColor() FancyLogger
// WithoutColor explicitly disables colorful features on the log.
WithoutColor() FancyLogger
// WithDebug enables debugging output to reveal debug and trace level logs.
WithDebug() FancyLogger
// WithoutDebug disables debugging output.
WithoutDebug() FancyLogger
// WithTrace enables trace output to reveal trace level logs.
WithTrace() FancyLogger
// WithoutTrace disables trace output.
WithoutTrace() FancyLogger
// IsDebug checks the current state of debugging output.
IsDebug() bool
// IsTrace checks the current state of trace output.
IsTrace() bool
// WithTimestamp enables timestamp output on the log.
WithTimestamp() FancyLogger
// WithoutTimestamp disables timestamp output on the log.
WithoutTimestamp() FancyLogger
// Quiet disables all log output.
Quiet() FancyLogger
// NoQuiet enables all log output (disables quiet mode).
NoQuiet() FancyLogger
// IsQuiet checks if the logger is in quiet mode.
IsQuiet() bool
// HasColor checks if the logger has color enabled.
HasColor() bool
// contains filtered or unexported methods
}
FancyLogger is the primary interface for the fancylogger. It combines standard logging, formatted logging, mapped logging, and prefixed logging. It also provides configuration methods for colors, debug/trace levels, and quiet mode.
type FdWriter ¶
FdWriter interface extends existing io.Writer with file descriptor function support. This is typically implemented by os.File (like os.Stdout and os.Stderr).
type FormatLog ¶ added in v0.0.11
type FormatLog interface {
Infof(format string, a ...any)
Debugf(format string, a ...any)
Warnf(format string, a ...any)
Errorf(format string, a ...any)
Tracef(format string, a ...any)
Fatalf(format string, a ...any)
}
FormatLog defines the formatted logging methods (printf-style) for different severity levels.
type HttpLog ¶ added in v0.0.11
type HttpLog struct {
FancyLogger
// contains filtered or unexported fields
}
HttpLog implements the FancyHttpLog interface.
func (*HttpLog) ConnectMethod ¶ added in v0.0.13
ConnectMethod logs a CONNECT request with structured data and status code.
func (*HttpLog) DebugHeaders ¶ added in v0.0.13
DebugHeaders returns whether header logging is enabled.
func (*HttpLog) DeleteMethod ¶ added in v0.0.13
DeleteMethod logs a DELETE request with structured data and status code.
func (*HttpLog) GetMethod ¶ added in v0.0.13
GetMethod logs a GET request with structured data and status code.
func (*HttpLog) HeadMethod ¶ added in v0.0.13
HeadMethod logs a HEAD request with structured data and status code.
func (*HttpLog) OptionsMethod ¶ added in v0.0.13
OptionsMethod logs an OPTIONS request with structured data and status code.
func (*HttpLog) PostMethod ¶ added in v0.0.13
PostMethod logs a POST request with structured data and status code.
func (*HttpLog) PutMethod ¶ added in v0.0.13
PutMethod logs a PUT request with structured data and status code.
func (*HttpLog) TraceMethod ¶ added in v0.0.13
TraceMethod logs a TRACE request with structured data and status code.
func (*HttpLog) WithHeaders ¶ added in v0.0.13
func (h *HttpLog) WithHeaders() FancyHttpLog
WithHeaders enables header logging for this HttpLog instance.
type Level ¶ added in v0.0.4
type Level string
Level represents the severity level of a log message.
const ( // Fatal level indicates a critical error that will cause the application to exit. Fatal Level = "FATAL" // Error level indicates a significant problem that needs attention. Error Level = "ERROR" // Warn level indicates a potential issue or important situation. Warn Level = "WARN" // Info level indicates general informational messages. Info Level = "INFO" // Debug level indicates detailed information for troubleshooting. Debug Level = "DEBUG" // Trace level indicates even more granular information than Debug, often including stack traces. Trace Level = "TRACE" )
const ( // GetLevel represents the GET HTTP method. GetLevel Level = "GET" // DeleteLevel represents the DELETE HTTP method. DeleteLevel Level = "DELETE" // ConnectLevel represents the CONNECT HTTP method. ConnectLevel Level = "CONNECT" // HeadLevel represents the HEAD HTTP method. HeadLevel Level = "HEAD" // OptionsLevel represents the OPTIONS HTTP method. OptionsLevel Level = "OPTIONS" // PostLevel represents the POST HTTP method. PostLevel Level = "POST" // PutLevel represents the PUT HTTP method. PutLevel Level = "PUT" // TraceLevel represents the TRACE HTTP method. TraceLevel Level = "TRACE" )
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger struct defines the underlying storage for a single logger instance.
func New ¶
New returns a new Logger instance with a predefined writer output. It automatically detects if the terminal supports coloring.
func NewWithError ¶
NewWithError returns a new Logger instance with separate writers for standard and error output. It automatically detects if the terminal supports coloring. 'out' would typically be os.Stdout and 'err' would be os.Stderr.
func NewWithName ¶
NewWithName returns a new Logger instance with a specific name and writer output. The name is used as a prefix for all log messages.
func NewWithNameAndError ¶
NewWithNameAndError returns a new Logger instance with a specific name and separate writers for standard and error output.
func (*Logger) Debug ¶
func (l *Logger) Debug(v ...interface{})
Debug prints a debug message to the output if debug output is enabled.
func (*Logger) Debugf ¶
Debugf prints a formatted debug message to the output if debug output is enabled.
func (*Logger) Error ¶
func (l *Logger) Error(v ...interface{})
Error prints an error message to the output.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v ...interface{})
Fatal prints a fatal message to the output and exits the application with status 1.
func (*Logger) FatalMap ¶
FatalMap logs structured fatal data and exits the application with status 1.
func (*Logger) FatalMapWithCode ¶
FatalMapWithCode logs structured fatal data and exits the application with the provided status code.
func (*Logger) FatalWithCode ¶
FatalWithCode prints a fatal message to the output and exits the application with the provided status code.
func (*Logger) FatalWithCodef ¶
FatalWithCodef prints a formatted fatal message to the output and exits the application with the provided status code.
func (*Logger) Fatalf ¶
Fatalf prints a formatted fatal message to the output and exits the application with status 1.
func (*Logger) HasColor ¶ added in v0.0.21
HasColor returns true if color output is enabled for this logger.
func (*Logger) Info ¶
func (l *Logger) Info(v ...interface{})
Info prints an informational message to the output.
func (*Logger) NoQuiet ¶
func (l *Logger) NoQuiet() FancyLogger
NoQuiet enables all log output (disables quiet mode).
func (*Logger) SetDefaultTimeFn ¶
func (l *Logger) SetDefaultTimeFn(timestampFunc TimestampFunc)
SetDefaultTimeFn overrides the default timestamp producer.
func (*Logger) SetTimestampColor ¶
SetTimestampColor overrides the default color for timestamps.
func (*Logger) Trace ¶
func (l *Logger) Trace(v ...interface{})
Trace prints a trace message to the output if trace output is enabled.
func (*Logger) Tracef ¶
Tracef prints a formatted trace message to the output if trace output is enabled.
func (*Logger) Warn ¶
func (l *Logger) Warn(v ...interface{})
Warn prints a warning message to the output.
func (*Logger) WithColor ¶
func (l *Logger) WithColor() FancyLogger
WithColor explicitly enables colorful features on the log.
func (*Logger) WithDebug ¶
func (l *Logger) WithDebug() FancyLogger
WithDebug enables debugging output to reveal debug and trace level logs.
func (*Logger) WithTimestamp ¶
func (l *Logger) WithTimestamp() FancyLogger
WithTimestamp enables timestamp output on the log.
func (*Logger) WithTrace ¶
func (l *Logger) WithTrace() FancyLogger
WithTrace enables trace output to reveal trace level logs.
func (*Logger) WithoutColor ¶
func (l *Logger) WithoutColor() FancyLogger
WithoutColor explicitly disables colorful features on the log.
func (*Logger) WithoutDebug ¶
func (l *Logger) WithoutDebug() FancyLogger
WithoutDebug disables debugging output.
func (*Logger) WithoutTimestamp ¶
func (l *Logger) WithoutTimestamp() FancyLogger
WithoutTimestamp disables timestamp output on the log.
func (*Logger) WithoutTrace ¶
func (l *Logger) WithoutTrace() FancyLogger
WithoutTrace disables trace output.
type MappedLog ¶ added in v0.0.11
type MappedLog interface {
InfoMap(a map[string]any)
DebugMap(a map[string]any)
WarnMap(a map[string]any)
ErrorMap(a map[string]any)
TraceMap(a map[string]any)
FatalMap(a map[string]any)
}
MappedLog defines the methods for logging structured data using maps.
type Methods ¶ added in v0.0.11
type Methods interface {
GetMethod(a map[string]any, status int)
DeleteMethod(a map[string]any, status int)
ConnectMethod(a map[string]any, status int)
HeadMethod(a map[string]any, status int)
OptionsMethod(a map[string]any, status int)
PostMethod(a map[string]any, status int)
PutMethod(a map[string]any, status int)
TraceMethod(a map[string]any, status int)
}
Methods defines the logging methods for different HTTP verbs.
type Prefix ¶
Prefix struct defines the visual representation of a log level. Text will be the prefix text included in the log. Color will be the color applied to the log prefix. File flag set to true will display code trace (stack trace).
type PrefixLog ¶ added in v0.0.11
type PrefixLog interface {
Log(prefix Prefix, a ...any)
Logf(prefix Prefix, format string, a ...any)
LogMap(prefix Prefix, a map[string]any)
}
PrefixLog defines methods for logging with custom prefixes.
type PrefixText ¶ added in v0.0.11
type PrefixText struct {
// contains filtered or unexported fields
}
PrefixText struct holds the values of the prefixes to be used and the tail size to add spaces to the end of the prefix for alignment.
type StandardLog ¶ added in v0.0.11
type StandardLog interface {
Info(a ...any)
Debug(a ...any)
Warn(a ...any)
Error(a ...any)
Trace(a ...any)
Fatal(a ...any)
}
StandardLog defines the standard logging methods for different severity levels.
type TimestampFunc ¶
TimestampFunc is a function type used to generate timestamps for log entries.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package handlers provides middleware and helper functions for integrating fancylog with various frameworks and libraries like Gin, net/http, and pgx.
|
Package handlers provides middleware and helper functions for integrating fancylog with various frameworks and libraries like Gin, net/http, and pgx. |