Golang Zap logger with Context, Zax is here!!!

By yuseferi, 8 June, 2023
Golang Zap logger with Context, Zax is here!!!

In Go development, efficient logging and managing contextual information are crucial aspects of building reliable and maintainable applications. The combination of the Zap logger and context package provides a robust solution for logging and context propagation. In this article, we will explore the Zax package, available on GitHub at github.com/yuseferi/zax, which combines the power of the Zap logger and context in Go.

Zap is a highly performant and production-ready logging library for Go. It focuses on providing structured and leveled logging, while keeping performance overhead minimal. With a simple and intuitive API, Zap allows developers to emit log messages with varying levels of severity, attach key-value pairs as structured context, and even customize log output formats.

Context is a package in the Go standard library that enables the propagation of request-scoped data across multiple goroutines. It allows developers to pass contextual information, such as request IDs, authentication details, or other metadata, through the call stack without explicitly passing them as function arguments. Contexts also facilitate cancellation and timeout management, making them vital for building robust and scalable applications.

The zax package, serves as a bridge between zap Logger and context, providing a convenient way to log and propagate contextual information within Go applications. By combining these two powerful tools, zax offers enhanced observability and better traceability, making debugging and monitoring easier.

Features and Benefits of zax:

  1. Seamless Integration: The zax package seamlessly integrates zap Logger and context, allowing developers to log messages while automatically propagating contextual information across goroutines.
  2. Context Propagation: With zax, you can create child contexts from the parent context and ensure that contextual information is carried along with log entries, making it easier to trace the flow of a request or operation.
  3. Enhanced Logging: The combination of zap Logger and context provides richer log entries. You can attach key-value pairs as fields to log messages, enabling better understanding and analysis of the application’s behavior.
  4. Leveled Logging: Leveraging the leveled logging capabilities of zap, zax enables developers to set log levels and filter log entries based on severity, allowing fine-grained control over the log output.
  5. Performance-Focused: Just like zap Logger, zax is designed with performance in mind, minimizing the overhead associated with logging operations. This ensures that logging doesn’t significantly impact the application’s performance.

Example:

func main(){
logger, _ := zap.NewProduction()
ctx := context.Background()
s := NewServiceA(logger)
ctx = zax.Set(ctx, logger, []zap.Field{zap.String("trace_id", "my-trace-id")})
s.funcA(ctx)
}

type ServiceA struct {
logger *zap.Logger
}

func NewServiceA(logger *zap.Logger) *ServiceA {
return &ServiceA{
logger: logger,
}
}

func (s *ServiceA) funcA(ctx context.Context) {
s.logger.Info("func A") // it does not contain trace_id, you need to add it manually
zax.Get(ctx).Info("func A") // it will logged with "trace_id" = "my-trace-id"
}

The zax package offers a powerful and efficient way to combine the zap Logger and context package in Go. By seamlessly integrating these tools, zax enables developers to log messages with contextual information, enhancing observability and traceability within their applications. Whether you are building microservices, APIs, or any other Go application, leveraging the zax package can significantly improve your logging capabilities and simplify the debugging process. Explore the zax package on GitHub and start enhancing your Go applications’ logging capabilities today!

zax repository: github.com/yuseferi/zax