rlog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2019 License: MIT Imports: 5 Imported by: 3

README

rlog GoDoc Build Status

rlog logs incomming HTTP requests, and provides access to a logger from HTTP request's context.Context.

rlog uses github.com/uber-go/zap as a logger implementation.

Each log record has the key requestID.

Installation

go get github.com/i-core/rlog

Usage

package main

import (
    "fmt"
    "net/http"
    "os"

    "go.uber.org/zap"
    "github.com/i-core/rlog"
)

func main() {
    // Create a logger.
    log, err := zap.NewProduction()
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to create logger: %s", err)
        os.Exit(1)
    }

    // Create a logger middleware.
    logmw := rlog.NewMiddleware(log)

    // Wrap HTTP handler with the logger middleware.
    handler := logmw(newHandler())

    // Start a web server.
    log.Info("Server started")
    log.Fatal("Server finished", zap.Error(http.ListenAndServe(":8080", handler)))
}

func newHandler() http.Handler {
    return http.HandlerFunc(w http.ResponseWriter, r *http.Request) {
        // Get a request's logger.
        log := rlog.FromContext(r.Context())
        log.Info("Handle request")
    }
}

Contributing

Thanks for your interest in contributing to this project. Get started with our Contributing Guide.

License

The code in this project is licensed under MIT license.

Documentation

Overview

Package rlog provides access to logging service from HTTP request's context.Context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) *zap.Logger

FromContext returns a request's logger stored in a context.

func NewMiddleware

func NewMiddleware(log *zap.Logger) func(next http.Handler) http.Handler

NewMiddleware returns a middleware that places a logger with request's ID to context, and logs the request.

Types

This section is empty.

Jump to

Keyboard shortcuts

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