Documentation ¶
Overview ¶
Package logmonitor provides logging utilities for a web application. It leverages the zap logging library to offer structured, leveled logging. The package is designed to integrate with the Gin web framework and includes middleware that logs incoming HTTP requests, including response status, method, path, and the time taken to process each request.
The logger is initialized with a development-friendly configuration that outputs logs in a human-readable, color-coded format, suitable for development and debugging. The RequestLogger middleware can be easily added to a Gin router to enhance request logging with detailed information that can help in monitoring and troubleshooting.
Example:
func main() { router := gin.Default() router.Use(logmonitor.RequestLogger()) // ... other middlewares and routes ... router.Run(":8080") }
It is important to flush any buffered log entries when the application exits to ensure all logs are written to their destination. This can be achieved by calling the Logger.Sync() method, which is typically done in the main function using defer to ensure it's called even if the application exits unexpectedly.
Example:
func main() { defer func() { if err := logmonitor.Logger.Sync(); err != nil { fmt.Fprintf(os.Stderr, "Failed to flush log: %v\n", err) } }() // ... rest of the main function ... }
Copyright (c) 2023 H0llyW00dzZ
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Logger *zap.Logger
Logger is a global variable to access the zap logger throughout the logmonitor package.
Functions ¶
func RequestLogger ¶
func RequestLogger(logger *zap.Logger) gin.HandlerFunc
RequestLogger returns a gin.HandlerFunc (middleware) that logs requests using zap. It is intended to be used as a middleware in a Gin router setup.
Upon receiving a request, it logs the following information:
- Machine Start Time (the local time when the request is received by the server)
- HTTP status code of the response
- HTTP method of the request
- Requested path
- Duration taken to process the request
The logs are output in a structured format, making them easy to read and parse.
Types ¶
This section is empty.