logmonitor

package
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

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

This section is empty.

Variables

View Source
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.

func SetLogger added in v0.1.7

func SetLogger(logger *zap.Logger)

SetLogger sets the logger instance for the package.

Types

type BadRequestError added in v0.1.10

type BadRequestError struct {
	UserMessage string
	Err         error
}

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

type LogFieldOption func() zap.Field

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL