Top Related Projects
Structured, pluggable logging for Go.
Zero Allocation JSON Logger
Blazing fast, structured, leveled logging in Go.
Structured, composable logging for Go
lumberjack is a log rolling package for Go
Quick Overview
Apex/log is a structured logging package for Go, designed to be both simple and powerful. It provides a clean API for logging with various levels, handlers, and formatters, making it easy to integrate structured logging into Go applications.
Pros
- Simple and intuitive API
- Supports multiple output formats (JSON, logfmt, text)
- Extensible with custom handlers and formatters
- Includes built-in support for context fields and error logging
Cons
- Not as feature-rich as some other logging libraries
- Limited built-in integrations with third-party services
- May require additional configuration for complex logging scenarios
Code Examples
Logging with fields:
log.WithFields(log.Fields{
"user": "jdoe",
"action": "login",
}).Info("User logged in")
Using a custom logger:
logger := log.WithFields(log.Fields{
"app": "myapp",
"env": "production",
})
logger.Info("Application started")
Logging errors:
err := someFunction()
if err != nil {
log.WithError(err).Error("An error occurred")
}
Getting Started
To use apex/log in your Go project, first install it:
go get -u github.com/apex/log
Then, in your Go code:
import (
"github.com/apex/log"
"github.com/apex/log/handlers/json"
)
func main() {
// Set the default handler to JSON
log.SetHandler(json.New(os.Stdout))
// Log some information
log.WithFields(log.Fields{
"user": "jdoe",
}).Info("Hello, World!")
}
This will output JSON-formatted logs to stdout. You can customize the handler, log level, and add fields as needed for your application.
Competitor Comparisons
Structured, pluggable logging for Go.
Pros of Logrus
- More feature-rich with extensive logging options and customization
- Supports structured logging with fields for better organization
- Has a larger community and ecosystem of hooks and formatters
Cons of Logrus
- Heavier and potentially slower due to more features
- No longer actively maintained (in maintenance mode)
- More complex API compared to Log's simpler interface
Code Comparison
Log:
log.WithFields(log.Fields{
"user": "john",
"id": 123,
}).Info("User logged in")
Logrus:
logrus.WithFields(logrus.Fields{
"user": "john",
"id": 123,
}).Info("User logged in")
Summary
Log is a lightweight, performant logging library with a simple API, while Logrus offers more features and structured logging capabilities. Log is actively maintained and focuses on simplicity, whereas Logrus is feature-rich but in maintenance mode. Both libraries provide similar basic functionality, but Logrus offers more advanced options at the cost of increased complexity and potential performance overhead.
Zero Allocation JSON Logger
Pros of zerolog
- Higher performance and lower allocation overhead
- Supports structured logging out of the box
- More flexible and customizable logging options
Cons of zerolog
- Steeper learning curve due to more complex API
- Less idiomatic Go syntax compared to apex/log
- Requires more setup for basic usage
Code Comparison
zerolog:
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
logger.Info().Str("key", "value").Msg("Hello, World!")
apex/log:
log.SetHandler(text.New(os.Stdout))
log.WithField("key", "value").Info("Hello, World!")
Both libraries offer structured logging capabilities, but zerolog uses a more fluent interface for adding fields. apex/log follows a more traditional logging pattern, which may be more familiar to developers coming from other logging libraries.
zerolog's approach allows for better performance and lower allocations, but it may require more effort to set up and use effectively. apex/log provides a simpler API that's easier to get started with, but it may not offer the same level of performance or flexibility as zerolog.
Ultimately, the choice between these libraries depends on your specific requirements for performance, ease of use, and logging features.
Blazing fast, structured, leveled logging in Go.
Pros of zap
- Significantly faster performance and lower allocation overhead
- More flexible and customizable logging options
- Better support for structured logging and field-based logging
Cons of zap
- Steeper learning curve and more complex API
- Requires more setup and configuration for basic use cases
Code Comparison
zap:
logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("Failed to fetch URL",
zap.String("url", url),
zap.Int("attempt", attempt),
zap.Duration("backoff", backoff),
)
log:
log.WithFields(log.Fields{
"url": url,
"attempt": attempt,
"backoff": backoff,
}).Info("Failed to fetch URL")
Summary
zap offers superior performance and more advanced features, making it suitable for high-performance applications with complex logging requirements. However, it comes with a steeper learning curve and more setup overhead.
log provides a simpler, more straightforward API that's easier to get started with, but may not be as performant or flexible for advanced use cases.
The choice between the two depends on the specific needs of your project, balancing simplicity and ease of use against performance and advanced features.
Structured, composable logging for Go
Pros of log15
- More flexible logging levels with custom severity ordering
- Built-in support for structured logging with key-value pairs
- Extensive customization options for log formatting and output
Cons of log15
- Slightly more complex API compared to apex/log
- Less active maintenance and updates in recent years
- Potentially higher memory usage due to more features
Code Comparison
log15:
log := log15.New("module", "app/server")
log.Info("server starting", "listen", ":8080")
apex/log:
log.WithFields(log.Fields{
"module": "app/server",
}).Info("server starting")
Both libraries offer structured logging, but log15 provides a more concise syntax for adding context to log entries. apex/log uses a chained method approach, while log15 allows passing key-value pairs directly to the logging methods.
log15 offers more flexibility in terms of log levels and formatting, making it suitable for complex logging requirements. However, apex/log provides a simpler API and is more actively maintained, which may be preferable for projects with straightforward logging needs.
Consider your project's specific requirements, such as performance, customization needs, and long-term maintenance when choosing between these logging libraries.
lumberjack is a log rolling package for Go
Pros of Lumberjack
- Simple and focused on log rotation and management
- Supports log file size limits and automatic rotation
- Lightweight with minimal dependencies
Cons of Lumberjack
- Limited built-in formatting options
- Lacks advanced features like structured logging or log levels
- No built-in support for different output destinations (e.g., console, network)
Code Comparison
Lumberjack:
log := lumberjack.Logger{
Filename: "/var/log/myapp/foo.log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
Compress: true, // disabled by default
}
Apex/log:
log.SetHandler(cli.New(os.Stderr))
log.WithFields(log.Fields{
"user": "jsmith",
"id": "123",
}).Info("user logged in")
Lumberjack focuses on log file management and rotation, while Apex/log provides a more comprehensive logging solution with structured logging, multiple output handlers, and log levels. Lumberjack is ideal for applications that primarily need file-based logging with rotation, whereas Apex/log offers more flexibility and features for complex logging requirements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Package log implements a simple structured logging API inspired by Logrus, designed with centralization in mind. Read more on Medium.
Handlers
- apexlogs â handler for Apex Logs
- cli â human-friendly CLI output
- discard â discards all logs
- es â Elasticsearch handler
- graylog â Graylog handler
- json â JSON output handler
- kinesis â AWS Kinesis handler
- level â level filter handler
- logfmt â logfmt plain-text formatter
- memory â in-memory handler for tests
- multi â fan-out to multiple handlers
- papertrail â Papertrail handler
- text â human-friendly colored output
- delta â outputs the delta between log calls and spinner
Example
Example using the Apex Logs handler.
package main
import (
"errors"
"time"
"github.com/apex/log"
)
func main() {
ctx := log.WithFields(log.Fields{
"file": "something.png",
"type": "image/png",
"user": "tobi",
})
for range time.Tick(time.Millisecond * 200) {
ctx.Info("upload")
ctx.Info("upload complete")
ctx.Warn("upload retry")
ctx.WithError(errors.New("unauthorized")).Error("upload failed")
ctx.Errorf("failed to upload %s", "img.png")
}
}
Top Related Projects
Structured, pluggable logging for Go.
Zero Allocation JSON Logger
Blazing fast, structured, leveled logging in Go.
Structured, composable logging for Go
lumberjack is a log rolling package for Go
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot