accesslog

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MiddlewareBuilder

type MiddlewareBuilder struct {
	// contains filtered or unexported fields
}

MiddlewareBuilder is a struct that facilitates the creation of middleware functions with logging capabilities for a web service framework (hypothetical 'mist' framework in this context). It contains a field that holds a reference to a logging function which can be used to log messages. This struct could potentially be extended with additional fields and methods to further customize the behavior of the middleware being built, especially for handling logging in a consistent manner throughout the application.

Fields:

logFunc: A function of type 'func(log string)' that represents the logging strategy
         the middleware will utilize. The 'logFunc' is intended to be called within middleware
         handlers to log various messages, like errors or informational messages, to a log output,
         such as the console, a file, or a remote log management system.

Example of adding a log function to MiddlewareBuilder:

mb := &MiddlewareBuilder{
    logFunc: func(log string) {
        // Implementation of the log function, could log to stdout or a file, etc.
        fmt.Println("Log:", log)
    },
}

// With the logFunc set, MiddlewareBuilder can now be used to construct middleware that logs messages.
middleware := mb.Build() // Assuming Build method is implemented on MiddlewareBuilder.
// The middleware can be used within the web framework to apply logging per request.

func InitMiddleware added in v0.0.21

func InitMiddleware() *MiddlewareBuilder

InitMiddleware initializes a new instance of the MiddlewareBuilder struct with default configuration settings. It sets up a standard logging function that will log access events using the Go standard library's log package. The returned MiddlewareBuilder can be further configured with additional options before being used to create middleware for an HTTP server.

Returns: - A pointer to a new MiddlewareBuilder with default log function configuration.

Usage:

  • builder := InitBuilder() This will create a new MiddlewareBuilder with a default log function.

func (*MiddlewareBuilder) Build

func (b *MiddlewareBuilder) Build() mist.Middleware

Build constructs a middleware function that is compliant with the mist framework's Middleware type. The middleware created by this method encompasses a logging feature as configured via the MiddlewareBuilder. The middleware function created here, when executed, performs the following operations:

  • It first creates a deferred function that collects access log data upon request completion and logs it using the pre-configured `logFunc`.
  • Then it continues with the execution of the next middleware or final request handler (`next`) in the stack.

The method returns a closure that matches the mist.Middleware type signature and can be integrated into the middleware chain during the configuration of the HTTP server or router.

Returns:

  • mist.Middleware: A middleware function that wraps around the next handler in the middleware stack, providing access logging functionalities.

Example usage:

  • After creating an instance of MiddlewareBuilder and setting the log function, the Build method is used to obtain a new Middleware function that logs requests. router.Use(mb.Build()) // Assuming 'router' is an instance that has a 'Use' method accepting Middleware.

func (*MiddlewareBuilder) LogFunc

func (b *MiddlewareBuilder) LogFunc(fn func(log string)) *MiddlewareBuilder

LogFunc assigns a custom logging function to the MiddlewareBuilder instance. This method is used to define how messages should be logged when the middleware operates within the request handling flow. It accepts a function parameter that matches the signature needed for logging operations in the middleware. The method enables a fluent interface by returning the MiddlewareBuilder instance itself, allowing for method chaining when configuring the builder.

Parameters:

fn: A function that takes a single string argument representing the log message. The function should
    contain the code that dictates how the log messages will be processed and where they will be sent.
    This could involve logging to the console, writing to a file, or sending the data to a log aggregation
    service depending on the actual implementation passed to this method.

Returns:

*MiddlewareBuilder: A pointer to the current instance of the MiddlewareBuilder, allowing for additional
                     configuration calls to be chained.

Example usage:

mb := new(MiddlewareBuilder).
        LogFunc(func(log string) {
            // Custom logging implementation, such as sending log to an external service.
            SendLogToService(log)
        })
// At this point, 'mb' has a logging function that sends logs to an external service.

Jump to

Keyboard shortcuts

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