zap-plus
A lightweight and extensible logging library built on top of Uber's Zap, providing a simplified interface for structured logging in Go applications.
Features
- ๐ High Performance: Built on Uber Zap's high-performance logging foundation
- ๐ฏ Simple Interface: Clean and intuitive API for common logging operations
- ๐ง Configurable: Support for different log levels and configurations
- ๐ฆ Lightweight: Minimal overhead with focused functionality
- ๐๏ธ Extensible: Modular design for easy customization and extension
Installation
go get github.com/hanzhang418/zap-plus
Quick Start
package main
import (
"github.com/hanzhang418/zap-plus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
// Create a new logger with Info level
logger, err := zap_plus.New(zapcore.InfoLevel)
if err != nil {
panic(err)
}
// Log some messages
logger.Info("Application started", zap.String("version", "1.0.0"))
logger.Warn("This is a warning", zap.Int("code", 404))
logger.Error("An error occurred", zap.Error(err))
}
API Reference
Creating a Logger
func New(level zapcore.Level) (logger.Logger, error)
Creates a new logger instance with the specified log level.
Parameters:
level: The minimum log level (e.g., zapcore.DebugLevel, zapcore.InfoLevel, zapcore.WarnLevel, zapcore.ErrorLevel)
Returns:
logger.Logger: Logger interface instance
error: Error if logger creation fails
Logger Interface
The logger.Logger interface provides the following methods:
type Logger interface {
Info(msg string, fields ...zap.Field)
Warn(msg string, fields ...zap.Field)
Error(msg string, fields ...zap.Field)
}
Methods
Info(msg string, fields ...zap.Field): Log an info-level message
Warn(msg string, fields ...zap.Field): Log a warning-level message
Error(msg string, fields ...zap.Field): Log an error-level message
All methods accept a message string and optional structured fields using Zap's field types.
Examples
Basic Logging
logger, _ := zap_plus.New(zapcore.InfoLevel)
logger.Info("User logged in",
zap.String("username", "john_doe"),
zap.String("ip", "192.168.1.1"))
logger.Warn("High memory usage",
zap.Float64("usage_percent", 85.5))
logger.Error("Database connection failed",
zap.String("database", "users"),
zap.Duration("timeout", time.Second*30))
Different Log Levels
// Debug level logger (logs everything)
debugLogger, _ := zap_plus.New(zapcore.DebugLevel)
// Error level logger (only logs errors)
errorLogger, _ := zap_plus.New(zapcore.ErrorLevel)
// Info level logger (logs info, warn, and error)
infoLogger, _ := zap_plus.New(zapcore.InfoLevel)
Structured Logging with Fields
logger, _ := zap_plus.New(zapcore.InfoLevel)
// Using various field types
logger.Info("Request processed",
zap.String("method", "GET"),
zap.String("path", "/api/users"),
zap.Int("status_code", 200),
zap.Duration("response_time", time.Millisecond*150),
zap.Bool("cached", true))
Project Structure
zap-plus/
โโโ zapplus.go # Main package entry point
โโโ pkg/
โ โโโ logger/
โ โโโ logger.go # Logger interface definition
โโโ internal/
โ โโโ builder/
โ โโโ builder.go # Logger builder implementation
โโโ go.mod # Go module definition
โโโ LICENSE # MIT License
โโโ README.md # This file
Dependencies
Requirements
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add some amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built on top of Uber's Zap logging library
- Inspired by the need for a simpler logging interface while maintaining high performance