All Projects → unrolled → Logger

unrolled / Logger

Licence: mit
HTTP middleware for Go that logs web requests to an io.Writer.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Logger

Chromelogger Python
Python library for logging variables to Google Chrome console
Stars: ✭ 232 (+866.67%)
Mutual labels:  middleware, logging
Gin Glog
Gin middleware to use glog
Stars: ✭ 53 (+120.83%)
Mutual labels:  middleware, logging
Diary
📑 Zero-dependency, fast logging library for both Node and Browser.
Stars: ✭ 79 (+229.17%)
Mutual labels:  middleware, logging
Home
Project Glimpse: Node Edition - Spend less time debugging and more time developing.
Stars: ✭ 260 (+983.33%)
Mutual labels:  middleware, logging
Go Grpc Middleware
Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more.
Stars: ✭ 4,170 (+17275%)
Mutual labels:  middleware, logging
Concurrency Logger
Log HTTP requests/responses separately, visualize their concurrency and report logs/errors in context of a request.
Stars: ✭ 400 (+1566.67%)
Mutual labels:  middleware, logging
Laravel Logger
An out the box activity logger for your Laravel or Lumen application. Laravel logger is an activity event logger for your laravel application. It comes out the box with ready to use with dashboard to view your activity. Laravel logger can be added as a middleware or called through a trait. This package is easily configurable and customizable. Supports Laravel 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6, and 7+
Stars: ✭ 366 (+1425%)
Mutual labels:  middleware, logging
Gear
A lightweight, composable and high performance web service framework for Go.
Stars: ✭ 544 (+2166.67%)
Mutual labels:  middleware, logging
React Redux Loading Bar
Loading Bar (aka Progress Bar) for Redux and React
Stars: ✭ 894 (+3625%)
Mutual labels:  middleware
Lumberjack
Web based log viewer for Erlang and Elixir applications
Stars: ✭ 22 (-8.33%)
Mutual labels:  logging
Bluezero
Middleware for distributed applications
Stars: ✭ 17 (-29.17%)
Mutual labels:  middleware
Reitit
A fast data-driven router for Clojure/Script
Stars: ✭ 892 (+3616.67%)
Mutual labels:  middleware
Thoth
An Error Logger for Go
Stars: ✭ 22 (-8.33%)
Mutual labels:  logging
Redux Firebase Middleware
🔌 🔥 Redux firebase middleware for React and React-native
Stars: ✭ 17 (-29.17%)
Mutual labels:  middleware
Yii2 Telegram Log
Telegram log target for Yii 2
Stars: ✭ 24 (+0%)
Mutual labels:  logging
Simplog
A simple logger. No dependencies, no special features, just logging.
Stars: ✭ 17 (-29.17%)
Mutual labels:  logging
Werelogs
A logging library providing efficient raw logging in the form of JSON data.
Stars: ✭ 16 (-33.33%)
Mutual labels:  logging
Yii2 Slack Log
Pretty Slack log target for Yii 2
Stars: ✭ 24 (+0%)
Mutual labels:  logging
Kona
a node.js service framework built on koa.js (generators)
Stars: ✭ 23 (-4.17%)
Mutual labels:  middleware
Mex Vocabulary
MEX Vocabulary: a lightweight machine learning interchange format
Stars: ✭ 19 (-20.83%)
Mutual labels:  logging

Logger GoDoc Test

Logger is an HTTP middleware for Go that logs web requests to an io.Writer (the default being os.Stdout). It's a standard net/http Handler, and can be used with many frameworks or directly with Go's net/http package.

Usage

// main.go
package main

import (
    "log"
    "net/http"

    "github.com/unrolled/logger"
)

var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello world"))
})

func main() {
    loggerWithConfigMiddleware := logger.New(logger.Options{
        Prefix: "MySampleWebApp",
        RemoteAddressHeaders: []string{"X-Forwarded-For"},
        OutputFlags: log.LstdFlags,
    })

    // loggerWithDefaults := logger.New()

    app := loggerMiddleware.Handler(myHandler)
    http.ListenAndServe("0.0.0.0:3000", app)
}

A simple GET request to "/info/" will output:

[MySampleWebApp] 2014/11/21 14:11:21 (12.34.56.78) "GET /info/ HTTP/1.1" 200 11 12.54µs

Here's a breakdown of what the values mean: [SuppliedPrefix] Date Time (RemoteIP) "Method RequestURI Protocol" StatusCode Size Time. Note that the Date Time is controlled by the output flags. See http://golang.org/pkg/log/#pkg-constants.

Be sure to use the Logger middleware as the very first handler in the chain. This will ensure that your subsequent handlers (like Recovery) will always be logged.

Available Options

Logger comes with a variety of configuration options (Note: these are not the default option values. See the defaults below.):

// ...
l := logger.New(logger.Options{
    Prefix: "myApp", // Prefix is the outputted keyword in front of the log message. Logger automatically wraps the prefix in square brackets (ie. [myApp] ) unless the `DisableAutoBrackets` is set to true. A blank value will not have brackets added. Default is blank (with no brackets).
    DisableAutoBrackets: false, // DisableAutoBrackets if set to true, will remove the prefix and square brackets. Default is false.
    RemoteAddressHeaders: []string{"X-Forwarded-For"}, // RemoteAddressHeaders is a list of header keys that Logger will look at to determine the proper remote address. Useful when using a proxy like Nginx: `[]string{"X-Forwarded-For"}`. Default is an empty slice, and thus will use `reqeust.RemoteAddr`.
    Out: os.Stdout, // Out is the destination to which the logged data will be written too. Default is `os.Stdout`.
    OutputFlags: log.Ldate | log.Ltime, // OutputFlags defines the logging properties. See http://golang.org/pkg/log/#pkg-constants. To disable all flags, set this to `-1`. Defaults to log.LstdFlags (2009/01/23 01:23:23).
    IgnoredRequestURIs: []string{"/favicon.ico"}, // IgnoredRequestURIs is a list of path values we do not want logged out. Exact match only!
})
// ...

Default Options

These are the preset options for Logger:

l := logger.New()

// Is the same as the default configuration options:

l := logger.New(logger.Options{
    Prefix: "",
    DisableAutoBrackets: false,
    RemoteAddressHeaders: []string{},
    Out: os.Stdout,
    OutputFlags log.LstdFlags,
    IgnoredRequestURIs: []string{},
})

Capturing the proper remote address

If your app is behind a load balancer or proxy, the default Request.RemoteAddr will likely be wrong. To ensure you're logging the correct IP address, you can set the RemoteAddressHeaders option to a list of header names you'd like to use. Logger will iterate over the slice and use the first header value it finds. If it finds none, it will default to the Request.RemoteAddr.

package main

import (
    "log"
    "net/http"

    "github.com/unrolled/logger"
)

var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello world"))
})

func main() {
    loggerWithConfigMiddleware := logger.New(logger.Options{
        RemoteAddressHeaders: []string{"X-Real-IP", "X-Forwarded-For"},
    })

    app := loggerMiddleware.Handler(myHandler)
    http.ListenAndServe("0.0.0.0:3000", app)
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].