Documentation
¶
Overview ¶
Package hookstdout provides a logrus hook for writing log entries to stdout with configurable field filtering, formatting options, and cross-platform color support.
Overview ¶
The hookstdout package implements a specialized logrus.Hook that writes log entries to os.Stdout with fine-grained control over output formatting, field filtering, and color support. It is built as a thin wrapper around the hookwriter package, specifically configured for stdout output with cross-platform color support via mattn/go-colorable.
This package is particularly useful for:
- Console applications requiring colored log output
- CLI tools with structured logging to stdout
- Development and debugging with readable console logs
- Production applications using stdout for log aggregation
- Docker/Kubernetes workloads where stdout is the standard log destination
Design Philosophy ¶
1. Stdout-Focused: Optimized specifically for stdout output with sensible defaults 2. Cross-Platform Colors: Automatic color support on Windows, Linux, and macOS 3. Zero Configuration: Works out-of-the-box with minimal setup 4. Flexible Formatting: Support for any logrus.Formatter with field filtering 5. Lightweight Wrapper: Delegates to hookwriter for core functionality
Key Features ¶
- Automatic stdout routing with os.Stdout as default destination
- Cross-platform color support via mattn/go-colorable
- Selective field filtering (stack traces, timestamps, caller info)
- Access log mode for message-only output
- Multiple formatter support (JSON, Text, custom)
- Level-based filtering (handle only specific log levels)
- Optional color output control (enable/disable per hook)
- Zero-allocation for disabled hooks (returns nil)
Architecture ¶
The package implements a simple delegation pattern to hookwriter:
┌──────────────────────────────────────────────┐
│ logrus.Logger │
│ │
│ ┌────────────────────────────────────┐ │
│ │ logger.Info("message") │ │
│ └────────────────┬───────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────┐ │
│ │ logrus.Entry │ │
│ └──────────┬───────┘ │
│ │ │
└────────────────────┼─────────────────────────┘
│
▼
┌────────────────────────────┐
│ HookStdOut.Fire() │
│ (delegates to │
│ HookWriter) │
└────────────┬───────────────┘
│
▼
┌─────────────────┐
│ HookWriter │
│ │
│ 1. Dup Entry │
│ 2. Filter │
│ 3. Format │
│ 4. Write │
└────────┬────────┘
│
▼
┌──────────────────────┐
│ colorable.Stdout │
│ (os.Stdout wrapper) │
└──────────────────────┘
Package Structure ¶
The hookstdout package is intentionally minimal with a single file:
- interface.go: Public API with New() and NewWithWriter() constructors
All core functionality (field filtering, formatting, entry processing) is delegated to the hookwriter package, maintaining a clean separation of concerns.
Data Flow ¶
1. Entry Creation: Application creates log entry via logger.Info/Warn/Error/etc. 2. Hook Invocation: logrus calls Fire() on all registered hooks for matching levels 3. Delegation: HookStdOut delegates to HookWriter.Fire() 4. Entry Processing: HookWriter duplicates entry, filters fields, formats output 5. Stdout Write: Formatted output written to os.Stdout via colorable wrapper
Basic Usage ¶
Create a hook and register it with a logrus logger:
import (
"github.com/sirupsen/logrus"
"github.com/nabbar/golib/logger/config"
"github.com/nabbar/golib/logger/hookstdout"
)
func main() {
// Configure hook options
opt := &config.OptionsStd{
DisableStandard: false,
DisableColor: false, // Enable color output
DisableStack: true,
DisableTimestamp: false,
EnableTrace: false,
}
// Create hook with Text formatter
hook, err := hookstdout.New(opt, nil, &logrus.TextFormatter{
ForceColors: true,
})
if err != nil {
log.Fatal(err)
}
// Register hook with logger
logger := logrus.New()
logger.AddHook(hook)
// Log entries will be written to stdout with colors
logger.WithField("msg", "Application started").Info("ignored message")
// Log entries will be written to stdout with colors
logger.WithField("msg", "User logged in").WithField("user", "john").Info("ignored message")
// Error messages will NOT be written to stdout
logger.Info("This message does not go to stdout")
// Use only field to define message, all message set into logrus function are ignored except for AccessLog (see below)
}
Configuration Options ¶
The OptionsStd struct controls hook behavior:
DisableStandard: If true, returns nil hook (completely disabled)
opt := &config.OptionsStd{DisableStandard: true}
hook, _ := hookstdout.New(opt, nil, nil) // Returns (nil, nil)
DisableColor: If true, wraps stdout to disable color escape sequences
opt := &config.OptionsStd{DisableColor: true}
// Output will not contain ANSI color codes
DisableStack: Filters out stack trace fields from output
opt := &config.OptionsStd{DisableStack: true}
logger.WithField("stack", trace).Error("error") // "stack" field removed
DisableTimestamp: Filters out timestamp fields from output
opt := &config.OptionsStd{DisableTimestamp: true}
// "time" field removed from all entries
EnableTrace: Controls caller/file/line field inclusion
opt := &config.OptionsStd{EnableTrace: false}
// Removes "caller", "file", "line" fields from output
EnableAccessLog: Enables message-only mode (ignores fields and formatters)
opt := &config.OptionsStd{EnableAccessLog: true}
logger.WithField("status", 200).Info("GET /api/users")
// Output: "GET /api/users\n" (fields ignored)
Common Use Cases ¶
Console Application with Colors:
opt := &config.OptionsStd{
DisableStandard: false,
DisableColor: false, // Enable colors
}
hook, _ := hookstdout.New(opt, nil, &logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
})
logger.AddHook(hook)
// Colored output for better readability
CLI Tool with Minimal Output:
opt := &config.OptionsStd{
DisableStack: true,
DisableTimestamp: true,
EnableTrace: false,
}
hook, _ := hookstdout.New(opt, nil, &logrus.TextFormatter{
DisableTimestamp: true,
})
// Clean, minimal CLI output
Docker Container Logs:
opt := &config.OptionsStd{
DisableColor: true, // No colors for log aggregation
}
hook, _ := hookstdout.New(opt, nil, &logrus.JSONFormatter{})
// Structured JSON logs to stdout for container log drivers
Development Mode with Debug Logs:
debugOpt := &config.OptionsStd{
DisableStack: false, // Keep stack traces
EnableTrace: true, // Include caller info
}
hook, _ := hookstdout.New(debugOpt, []logrus.Level{
logrus.DebugLevel,
logrus.InfoLevel,
}, nil)
// Verbose debug output with full context
Access Log to Stdout:
accessOpt := &config.OptionsStd{
DisableStandard: false,
EnableAccessLog: true, // Message-only mode
DisableColor: true,
}
hook, _ := hookstdout.New(accessOpt, nil, nil)
logger.Info("192.168.1.1 - GET /api/users - 200 - 45ms")
// for AccesLog, field are ignored and only message passed are written
Performance Considerations ¶
Memory Efficiency:
- Entry delegation to hookwriter minimizes allocations
- Disabled hooks (DisableStandard=true) return nil with zero allocation
- Color support via colorable adds minimal overhead (~1-2% on Windows)
Write Performance:
- Stdout writes are buffered by OS, generally fast
- Avoid high-frequency logging (>10k/sec) to stdout in production
- For high-throughput, consider async aggregation: github.com/nabbar/golib/ioutils/aggregator
Formatter Overhead:
- JSON formatters: ~50-100µs per entry (fast, structured)
- Text formatters: ~100-200µs per entry (slower, readable)
- Access log mode: ~20-30µs per entry (fastest, no formatting)
Color Performance:
- Unix/Linux/macOS: Zero overhead (native ANSI support)
- Windows: Minimal overhead via colorable's virtual terminal sequences
- Disable colors in production for slight performance gain
Thread Safety ¶
The hook implementation is thread-safe when used correctly:
- Safe: Multiple goroutines logging to the same logger with this hook
- Safe: Multiple hooks registered on the same logger
- Safe: Concurrent stdout writes (os.Stdout is thread-safe)
- Unsafe: Concurrent calls to Fire() with same entry (logrus prevents this)
- Unsafe: Modifying hook configuration after creation (immutable design)
Note: os.Stdout on Unix-like systems has atomic writes for messages < PIPE_BUF (typically 4KB), but Windows may interleave concurrent writes. For guaranteed ordering, use single-threaded logging or an aggregator.
Error Handling ¶
The hook can return errors in the following situations:
Construction Errors:
// No errors expected for New() with valid options hook, err := hookstdout.New(opt, nil, nil) // err is always nil (unless delegated hookwriter.New fails)
Runtime Errors:
// Delegated to hookwriter.Fire() err := hook.Fire(entry) // Returns formatter or writer errors
Silent Failures:
- Empty log data: Fire() returns nil without writing (normal)
- Empty access log message: Fire() returns nil without writing (normal)
- Disabled hook: New() returns (nil, nil) - not an error
Comparison with HookStdErr ¶
hookstdout vs hookstderr:
hookstdout: - Writes to os.Stdout - Typically for Info, Debug levels - Suitable for structured logs, access logs - Output can be piped/redirected separately hookstderr: - Writes to os.Stderr - Typically for Warn, Error, Fatal, Panic levels - Suitable for error logs, diagnostics - Separates errors from normal output
Use both hooks for proper stdout/stderr separation in CLI tools:
stdoutHook, _ := hookstdout.New(opt, []logrus.Level{
logrus.InfoLevel,
logrus.DebugLevel,
}, nil)
stderrHook, _ := hookstderr.New(opt, []logrus.Level{
logrus.WarnLevel,
logrus.ErrorLevel,
logrus.FatalLevel,
}, nil)
logger.AddHook(stdoutHook)
logger.AddHook(stderrHook)
Integration with golib Packages ¶
Logger Package:
import "github.com/nabbar/golib/logger" // Main logger package that uses this hook internally
Logger Config:
import "github.com/nabbar/golib/logger/config" // Provides OptionsStd configuration structure
Logger Types:
import "github.com/nabbar/golib/logger/types" // Defines Hook interface and field constants
HookWriter:
import "github.com/nabbar/golib/logger/hookwriter" // Core implementation that hookstdout delegates to
HookStdErr:
import "github.com/nabbar/golib/logger/hookstderr" // Companion package for stderr output
IOUtils Aggregator:
import "github.com/nabbar/golib/ioutils/aggregator" // For async high-performance log aggregation
Limitations ¶
Stdout-Only: This package is specifically for stdout. For other destinations, use hookwriter directly.
No Buffering: Stdout writes are unbuffered by default. For high-frequency logging, wrap stdout with bufio.Writer via NewWithWriter().
Color Limitations: Color output depends on terminal capabilities. Some environments (e.g., non-TTY pipes) may not display colors correctly even when enabled.
No Write Retries: Failed stdout writes return errors but don't retry. Generally not an issue as stdout writes rarely fail.
No Lifecycle Management: Hook doesn't manage stdout lifecycle (no Close()). This is intentional as stdout should remain open for process lifetime.
Best Practices ¶
DO:
- Enable colors for interactive terminals, disable for log aggregation
- Use JSON formatter for production, Text formatter for development
- Filter out verbose fields (stack, caller) for cleaner output
- Use level filtering to route different levels to stdout vs stderr
- Check for nil when DisableStandard is conditionally set
- Use access log mode for HTTP access logs or similar patterns
DON'T:
- Use this for file output (use hookwriter with os.Create instead)
- Enable colors when piping to files or non-TTY destinations
- Log extremely high frequency (>10k/sec) to stdout without aggregation
- Ignore the nil return when DisableStandard is true
- Mix structured and unstructured logging without clear separation
Testing ¶
The package includes comprehensive tests covering:
- Hook creation with various configurations
- Field filtering (stack, time, caller, file, line)
- Access log mode with empty messages
- Formatter integration (JSON, Text)
- Integration with logrus.Logger
- Level filtering behavior
- Multiple hooks on single logger
- Color enable/disable scenarios
Run tests:
go test -v github.com/nabbar/golib/logger/hookstdout
Check coverage:
go test -cover github.com/nabbar/golib/logger/hookstdout
Current coverage: Target >80% (delegates most logic to hookwriter)
Examples ¶
See example_test.go for runnable examples demonstrating:
- Basic hook creation and usage
- Colored console output
- Access log mode for HTTP logs
- Level-specific filtering
- Field filtering configurations
- Custom writer usage (NewWithWriter)
Related Packages ¶
- github.com/sirupsen/logrus - Underlying logging framework
- github.com/mattn/go-colorable - Cross-platform color support
- github.com/nabbar/golib/logger - Main logger package
- github.com/nabbar/golib/logger/config - Configuration types
- github.com/nabbar/golib/logger/types - Hook interface and constants
- github.com/nabbar/golib/logger/hookwriter - Core hook implementation
- github.com/nabbar/golib/logger/hookstderr - Companion stderr hook
- github.com/nabbar/golib/ioutils/aggregator - Async write aggregation
License ¶
MIT License - See LICENSE file for details.
Copyright (c) 2025 Nicolas JUHEL
Example (AccessLog) ¶
Example_accessLog demonstrates using access log mode for HTTP request logging.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf bytes.Buffer
// Enable access log mode
opt := &logcfg.OptionsStd{
DisableStandard: false,
EnableAccessLog: true, // Message-only mode
}
hook, err := loghko.NewWithWriter(&buf, opt, nil, nil)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Setup logger
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// IMPORTANT: In AccessLog mode, behavior is REVERSED!
// The message "GET /api/users - 200 OK - 45ms" IS output.
// The fields (method, path, status_code) are IGNORED.
logger.WithFields(logrus.Fields{
"method": "GET",
"path": "/api/users",
"status_code": 200,
}).Info("GET /api/users - 200 OK - 45ms")
fmt.Print(buf.String())
}
Output: GET /api/users - 200 OK - 45ms
Example (Basic) ¶
Example_basic demonstrates the simplest use case: creating a hook that writes to stdout.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
// Note: Using a buffer for predictable test output
var buf bytes.Buffer
// Configure the hook with minimal settings
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: true, // Disable color for predictable output
}
// Create the hook writing to buffer (simulating stdout)
hook, err := loghko.NewWithWriter(&buf, opt, nil, &logrus.TextFormatter{
DisableTimestamp: true, // Disable timestamp for predictable output
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Create and configure logger (output to Stderr to avoid double write)
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// IMPORTANT: The message parameter "ignored" in Info() is NOT used by the hook.
// Only the fields (here "msg") are written to the output.
// Exception: In AccessLog mode, only the message is used and fields are ignored.
logger.WithField("msg", "Application started").Info("ignored")
// Print what was written by the hook
fmt.Print(buf.String())
}
Output: level=info fields.msg="Application started"
Example (CliApplication) ¶
Example_cliApplication demonstrates a typical CLI application setup.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf bytes.Buffer
// Minimal, clean output for CLI
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: false, // Colors enabled for interactive terminals
DisableStack: true,
DisableTimestamp: true,
EnableTrace: false,
}
hook, err := loghko.NewWithWriter(&buf, opt, nil, &logrus.TextFormatter{
DisableTimestamp: true,
ForceColors: true,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// IMPORTANT: message "ignored" is NOT used, only field "msg"
logger.WithField("msg", "Processing files...").Info("ignored")
fmt.Println("CLI log written with colors")
}
Output: CLI log written with colors
Example (ColoredOutput) ¶
Example_coloredOutput demonstrates using colored output for console applications.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf bytes.Buffer
// Enable color output
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: false, // Enable color output
}
hook, err := loghko.NewWithWriter(&buf, opt, nil, &logrus.TextFormatter{
ForceColors: true,
DisableTimestamp: true,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// IMPORTANT: The message "ignored" is NOT output - only fields are used
logger.WithField("msg", "Colored log output").Info("ignored")
fmt.Println("Log written with colors (ANSI codes present)")
}
Output: Log written with colors (ANSI codes present)
Example (DisabledHook) ¶
Example_disabledHook demonstrates how to conditionally disable the hook.
package main
import (
"fmt"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
opt := &logcfg.OptionsStd{
DisableStandard: true, // This disables the hook
}
hook, err := loghko.New(opt, nil, nil)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
if hook == nil {
fmt.Println("Hook is disabled")
} else {
fmt.Println("Hook is enabled")
}
}
Output: Hook is disabled
Example (DockerContainer) ¶
Example_dockerContainer demonstrates JSON logging for containerized applications.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf bytes.Buffer
// Structured JSON logs without colors for container log drivers
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: true, // No colors for log aggregation
}
hook, err := loghko.NewWithWriter(&buf, opt, nil, &logrus.JSONFormatter{
DisableTimestamp: true,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// IMPORTANT: message "ignored" is NOT used, only fields
logger.WithFields(logrus.Fields{
"msg": "Container started",
"container": "app-1",
"image": "myapp:latest",
}).Info("ignored")
fmt.Println("JSON log written for container stdout")
}
Output: JSON log written for container stdout
Example (FieldFiltering) ¶
Example_fieldFiltering demonstrates filtering specific fields from output.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf bytes.Buffer
// Configure to filter out stack and timestamp
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: true,
DisableStack: true, // Remove stack fields
DisableTimestamp: true, // Remove time fields
EnableTrace: false, // Remove caller/file/line fields
}
hook, err := loghko.NewWithWriter(&buf, opt, nil, &logrus.TextFormatter{
DisableTimestamp: true,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// Log with fields that will be filtered
// IMPORTANT: message "ignored" is NOT used, only fields
logger.WithFields(logrus.Fields{
"msg": "Filtered log",
"stack": "trace info",
"caller": "main.go:123",
"user": "john",
}).Info("ignored")
// Only "user" field remains after filtering
fmt.Print(buf.String())
}
Output: level=info fields.msg="Filtered log" user=john
Example (JsonFormatter) ¶
Example_jsonFormatter demonstrates writing structured JSON logs to stdout.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf bytes.Buffer
// Configure options
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: true,
DisableStack: true,
DisableTimestamp: true,
}
// Create hook with JSON formatter
hook, err := loghko.NewWithWriter(&buf, opt, nil, &logrus.JSONFormatter{
DisableTimestamp: true,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Setup logger
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// IMPORTANT: The message "ignored" is NOT output - only fields are used
logger.WithFields(logrus.Fields{
"user_id": 123,
"action": "login",
"msg": "User logged in",
}).Info("ignored")
fmt.Println("Log written to stdout as JSON")
}
Output: Log written to stdout as JSON
Example (LevelFiltering) ¶
Example_levelFiltering demonstrates filtering logs by level.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf = bytes.NewBuffer(make([]byte, 0))
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: true,
}
// Only handle info and debug levels (not errors)
levels := []logrus.Level{
logrus.InfoLevel,
logrus.DebugLevel,
}
hook, err := loghko.NewWithWriter(buf, opt, levels, &logrus.TextFormatter{
DisableTimestamp: true,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// This will be written by the hook (info level)
// Note: message "ignored" is NOT used, only the field "msg"
logger.WithField("msg", "Info message").Info("ignored")
// This won't be written by the hook (wrong level)
logger.WithField("msg", "Error message").Error("ignored")
fmt.Printf("Hook captured: %s", buf.String())
}
Output: Hook captured: level=info fields.msg="Info message"
Example (TraceEnabled) ¶
Example_traceEnabled demonstrates enabling trace information in logs.
package main
import (
"bytes"
"fmt"
"os"
"github.com/sirupsen/logrus"
logcfg "github.com/nabbar/golib/logger/config"
loghko "github.com/nabbar/golib/logger/hookstdout"
)
func main() {
var buf bytes.Buffer
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: true,
EnableTrace: true, // Include caller/file/line information
}
hook, err := loghko.NewWithWriter(&buf, opt, nil, &logrus.TextFormatter{
DisableTimestamp: true,
})
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
logger := logrus.New()
logger.SetOutput(os.Stderr)
logger.AddHook(hook)
// IMPORTANT: message "ignored" is NOT used, only fields
logger.WithFields(logrus.Fields{
"msg": "Log with trace info",
"caller": "example_test.go:line",
"file": "example_test.go",
"line": 123,
"user": "john",
}).Info("ignored")
// Trace fields are included because EnableTrace is true
fmt.Print(buf.String())
}
Output: level=info caller="example_test.go:line" fields.msg="Log with trace info" file=example_test.go line=123 user=john
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HookStdOut ¶
HookStdOut is a logrus hook that writes log entries to stdout with configurable filtering and formatting options.
This interface extends logtps.Hook and provides integration with logrus logger for customized stdout log output handling. It supports field filtering (stack, timestamp, trace), custom formatters, color output, and access log mode.
The hook uses os.Stdout as the default output destination and wraps it with colorable support for cross-platform color output compatibility.
func New ¶
func New(opt *logcfg.OptionsStd, lvls []logrus.Level, f logrus.Formatter) (HookStdOut, error)
New creates a new HookStdOut instance for writing logrus entries to stdout.
This is a convenience function that calls NewWithWriter with os.Stdout as the writer. The hook supports color output via mattn/go-colorable for cross-platform compatibility.
Parameters:
- opt: Configuration options controlling behavior. If nil or DisableStandard is true, returns (nil, nil) to indicate the hook should be disabled.
- lvls: Log levels to handle. If empty or nil, defaults to logrus.AllLevels.
- f: Optional logrus.Formatter for entry formatting. If nil, uses entry.Bytes().
Configuration options (via opt):
- DisableStandard: If true, returns nil hook (disabled).
- DisableColor: If true, wraps stdout with colorable.NewNonColorable() to disable color output.
- DisableStack: If true, filters out stack trace fields from log data.
- DisableTimestamp: If true, filters out time fields from log data.
- EnableTrace: If false, filters out caller/file/line fields from log data.
- EnableAccessLog: If true, uses message-only mode (ignores fields and formatter).
Returns:
- HookStdOut: The configured hook instance, or nil if disabled.
- error: Always returns nil for this function (error handling is consistent with NewWithWriter).
Example:
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: false,
}
hook, err := hookstdout.New(opt, nil, &logrus.TextFormatter{})
if err != nil {
log.Fatal(err)
}
logger.AddHook(hook)
func NewWithWriter ¶ added in v1.19.0
func NewWithWriter(w io.Writer, opt *logcfg.OptionsStd, lvls []logrus.Level, f logrus.Formatter) (HookStdOut, error)
NewWithWriter creates a new HookStdOut instance with a custom io.Writer.
This function allows using a custom writer instead of os.Stdout, useful for testing or redirecting output to buffers, files, or other destinations while maintaining the HookStdOut interface semantics.
Parameters:
- w: The target io.Writer where log entries will be written. If nil, defaults to os.Stdout.
- opt: Configuration options controlling behavior. If nil or DisableStandard is true, returns (nil, nil) to indicate the hook should be disabled.
- lvls: Log levels to handle. If empty or nil, defaults to logrus.AllLevels.
- f: Optional logrus.Formatter for entry formatting. If nil, uses entry.Bytes().
Configuration options (via opt):
- DisableStandard: If true, returns nil hook (disabled).
- DisableColor: If true, wraps writer with colorable.NewNonColorable() to disable color output.
- DisableStack: If true, filters out stack trace fields from log data.
- DisableTimestamp: If true, filters out time fields from log data.
- EnableTrace: If false, filters out caller/file/line fields from log data.
- EnableAccessLog: If true, uses message-only mode (ignores fields and formatter).
Returns:
- HookStdOut: The configured hook instance, or nil if disabled.
- error: Returns error from hookwriter.New if the writer validation fails.
Example:
var buf bytes.Buffer
opt := &logcfg.OptionsStd{
DisableStandard: false,
DisableColor: true,
}
hook, err := hookstdout.NewWithWriter(&buf, opt, nil, nil)
if err != nil {
log.Fatal(err)
}
logger.AddHook(hook)