Documentation
¶
Overview ¶
Package logmonitor provides structured, leveled logging utilities designed for web applications. It utilizes the zap logging library to facilitate structured logging and integrates seamlessly with the Gin web framework. The package includes middleware for logging HTTP requests, capturing key information such as response status, method, path, and processing time for each request.
The logger is configured with a development-friendly setup that outputs logs in a color-coded, human-readable format, which is ideal for development and debugging purposes. The RequestLogger middleware can be readily applied to a Gin engine to augment request logging with granular details, aiding in application monitoring and troubleshooting.
Usage example:
func main() { router := gin.Default() router.Use(logmonitor.RequestLogger(logmonitor.Logger)) // ... additional middleware and route setup ... router.Run(":8080") }
It is crucial to flush any buffered log entries upon application termination to ensure all logs are committed to their intended destination. This is accomplished by invoking the Logger.Sync() method, typically in the main function using defer to guarantee execution even during an unexpected exit.
Flush logs on application exit example:
func main() { defer func() { if err := logmonitor.Logger.Sync(); err != nil { fmt.Fprintf(os.Stderr, "Failed to flush logs: %v\n", err) } }() // ... remainder of the main function ... }
The package also defines constants for various components to categorize logs, allowing for filtering and analysis based on specific parts of the application. These constants are utilized when creating log fields, ensuring consistent identification across logs.
This package's global Logger variable provides access to the configured zap logger for use throughout the application. SetLogger allows for the replacement of the default logger with a customized one if necessary.
Additional types and functions, such as BadRequestError and associated constructors, offer convenience in generating structured logs with common fields and handling specific error scenarios.
The RequestLogger middleware logs vital request details and should be included as part of the Gin router setup to capture request metrics in a structured log format.
Copyright (c) 2023 H0llyW00dzZ
Index ¶
Constants ¶
const ( ComponentNoSQL = "datastore" ComponentCache = "cache" // Currently unused. ComponentProjectIDENV = "projectid" ComponentInternalSecretENV = "customsecretkey" ComponentMachineOperation = "signal" // Currently unused. ComponentGopher = "hostmachine" )
Component constants for structured logging. This is used to identify the component that is logging the message.
const ( URLmismatchContextLog = "URL mismatch" URLShorteneredContextLog = "URL shortened and saved" FailedToRetriveURLContextLog = "Failed to retrieve URL" FailedToUpdateURLContextLog = "Failed to update URL" FailedToDeletedURLContextLog = "Failed to deleted URL" FailedToValidateURLContextLog = "Failed to validate URL" FailedToGetURLContextLog = "Failed to get URL" URLnotfoundContextLog = "URL not found" IDnotfoundContextLog = "ID not found" NoURLIDContextLog = "No URL and ID found" NoURLIDDBContextLog = "No URL and ID found in DB" URLupdateContextLog = "URL updated successfully" URLdeleteContextLog = "URL deleted" URLisNilContextLog = "URL is nil after GetURL call" URLRetriveContextLog = "URL retrieved successfully" ServerStartContextLog = "Server is starting and Listening on address" ServerFailContextLog = "Server failed to start" SignalContextLog = "Signal received" ServerForcetoShutdownContextLog = "Server is forced to shutdown:" DataStoreFailContextLog = "Datastore failed to connect" StartupFailedContextLog = "Startup failed" StartupFailureContextLog = "Startup failure" FailedtoCloseDatastoreContextLog = "failed to close datastore client:" DatastoreFailedtoCheckHealthContextLog = "datastore client failed health check:" FailedToCreateDatastoreClientContextLog = "failed to create datastore client:" FailedToIntializeLoggerContextLog = "failed to initialize logger:" DataStoreProjectIDEnvContextLog = "DATASTORE_PROJECT_ID environment variable not set" InternelSecretEnvContextLog = "INTERNAL_SECRET environment variable not set" InvalidNewURLFormatContextLog = "Invalid new URL format" MisMatchBetweenPathIDandPayloadIDContextLog = "Mismatch between path ID and payload ID" PathIDandPayloadIDDoesnotMatchContextLog = "Path ID and payload ID does not match" )
Define context logs for different components.
const ( ErrorEmoji = "❌" SuccessEmoji = "✅" InfoEmoji = "🛈" WarningEmoji = "⚠️" K8sEmoji = "☸️" DeployEmoji = "🚀" AlertEmoji = "🚨" UrlshortenerEmoji = "🔗" SignalSatelliteEmoji = "📡" ModernGopherEmoji = "🤖" GetBackEmoji = "🔙" RedirectEmoji = "🔀" SosEmoji = "🆘" DeleteEmoji = "🗑️" UpdateEmoji = "🔄" NewEmoji = "🆕" )
Define emojis for different log levels.
const ( HeaderResponseError = "error" HeaderResponseInternalServerError = "internal server error" HeaderResponseshortened_url = "shortened_url" HeaderResponseURlUpdated = "URL updated successfully" HeaderResponseInvalidRequestJSONBinding = "Invalid request - JSON binding error" HeaderResponseInvalidURLFormat = "Invalid URL format" HeaderResponseInvalidRequestPayload = "Invalid request payload" HeaderResponseInvalidRequest = "Invalid request" HeaderResponseURLDeleted = "URL deleted successfully" HeaderResponseIDandURLNotFound = "ID and URL not found" HeaderResponseForbidden = "Forbidden" HeaderResponseFailedtoGenerateID = "Failed to generate ID" HeaderResponseFailedtoSaveURL = "Failed to save URL" HeaderResponseStatus = "status" )
Define header response for different components.
const ( HeaderRequestOldURL = "old_url" HeaderRequestNewURL = "new_url" )
Define header request for different components.
const ( HeaderID = "id" HeaderURL = "url" HeaderMessage = "message" HeaderSchemeHTTP = "http" HeaderSchemeHTTPS = "https" HeaderXProto = "X-Forwarded-Proto" HeaderXinternalSecret = "X-Internal-Secret" )
Define header for different components.
Variables ¶
var Logger *zap.Logger
Logger is a global variable to access the zap logger throughout the logmonitor package.
Functions ¶
func CreateLogFields ¶ added in v0.2.2
func CreateLogFields(operation string, options ...LogFieldOption) []zap.Field
CreateLogFields generates common log fields for use in various parts of the application.
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 ¶
type BadRequestError ¶ added in v0.1.10
BadRequestError is a custom error type for bad requests.
func NewBadRequestError ¶ added in v0.1.10
func NewBadRequestError(userMessage string, err error) *BadRequestError
NewBadRequestError creates a new instance of BadRequestError. This function is used to construct an error with a user-friendly message and an underlying error, which can be used to provide detailed error information while also giving a clear message to the end-user.
func (*BadRequestError) Error ¶ added in v0.1.10
func (e *BadRequestError) Error() string
Error returns the message of the underlying error. This method allows BadRequestError to satisfy the error interface.
type LogFieldOption ¶ added in v0.2.2
LogFieldOption defines a function signature for options that can be passed to createLogFields.
func WithComponent ¶ added in v0.2.2
func WithComponent(component string) LogFieldOption
WithComponent returns a LogFieldOption that adds a 'component' field to the log.
func WithError ¶ added in v0.2.2
func WithError(err error) LogFieldOption
WithError returns a LogFieldOption that adds an 'error' field to the log.
func WithID ¶ added in v0.2.2
func WithID(id string) LogFieldOption
WithID returns a LogFieldOption that adds an 'id' field to the log.
func WithSignal ¶ added in v0.2.6
func WithSignal(signal os.Signal) LogFieldOption
WithSignal returns a LogFieldOption that adds a 'signal' field to the log.