logmonitor

package
v0.1.10 Latest Latest
Warning

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

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

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

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

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.

Jump to

Keyboard shortcuts

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