lib

package module
v0.0.0-...-436655d Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: MIT Imports: 8 Imported by: 0

README

go-logging-middleware

i made this in 20 minutes on accident

usage

package main

import (
    "fmt"
    "net/http"

    glm "github.com/jonasiwnl/go-logging-middleware"
)

func main() {
    logger := glm.NewLoggingMiddlewareBuilder(
		glm.NewMongoDatabase(client.Database("LoggingMiddleware").Collection("logs"))).
		WithInfoLevel(glm.Verbose).Build()

    http.Handle("/", logger.LogRoute(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, world!")
    })))
    http.ListenAndServe(":8080", nil)
}

TODO

  • IDs for logs into db
  • Add tests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database interface {
	Write(context.Context, LogSchema) error
}

* Database interface for writing logs.

func NewMongoDatabase

func NewMongoDatabase(collection *mongo.Collection) Database

func NewSQLDatabase

func NewSQLDatabase(database *sql.DB) Database

type InfoLevel

type InfoLevel int

* Noisiness level for the logger.

const (
	// Just logs route hits.
	Minimal InfoLevel = iota

	// Logs body.
	Normal

	// Logs headers, body, and other information.
	Verbose
)

type LogSchema

type LogSchema struct {
	ID          string    `bson:"_id"`
	TimeWritten time.Time `bson:"time_written"`
	Category    string    `bson:"category"`
	Info        string    `bson:"info"`
}

* Schema for a single log entry.

type LoggingMiddleware

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

func (LoggingMiddleware) LogRoute

func (q LoggingMiddleware) LogRoute(next http.Handler) http.Handler

LogRoute middleware logs the request to the database.

type LoggingMiddlewareBuilder

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

func NewLoggingMiddlewareBuilder

func NewLoggingMiddlewareBuilder(database Database) *LoggingMiddlewareBuilder

func (LoggingMiddlewareBuilder) Build

func (LoggingMiddlewareBuilder) WithDebug

func (LoggingMiddlewareBuilder) WithInfoLevel

func (b LoggingMiddlewareBuilder) WithInfoLevel(infoLevel InfoLevel) *LoggingMiddlewareBuilder

Jump to

Keyboard shortcuts

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