ginglog

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2017 License: MIT Imports: 3 Imported by: 1

README

Gin-Glog

Gin middleware for Logging with glog. It is meant as drop in replacement for the default logger used in Gin.

Build Status Coverage Status Go Report Card GoDoc

Project Context and Features

When it comes to choosing a Go framework, there's a lot of confusion about what to use. The scene is very fragmented, and detailed comparisons of different frameworks are still somewhat rare. Meantime, how to handle dependencies and structure projects are big topics in the Go community. We've liked using Gin for its speed, accessibility, and usefulness in developing microservice architectures. In creating Gin-Glog, we wanted to take fuller advantage of Gin's capabilities and help other devs do likewise.

Gin-Glog replaces the default logger of Gin to use Glog.

How Glog is different compared to other loggers

Glog is an efficient pure Go implementation of leveled logs. If you use Glog, you do not use blocking calls for writing logs. A goroutine in the background will flush queued loglines into appropriate logfiles. It provides logrotation, maintains symlinks to current files and creates flags to your command line interface.

Requirements

Gin-Glog uses the following Go packages as dependencies:

  • github.com/gin-gonic/gin
  • github.com/golang/glog

Installation

Assuming you've installed Go and Gin, run this:

go get github.com/szuecs/gin-glog

Usage

Example

Start your webapp to log to STDERR and /tmp:

% ./webapp -log_dir="/tmp" -alsologtostderr -v=2
package main

import (
    "flag"
    "time"

    "github.com/golang/glog"
    "github.com/szuecs/gin-glog"
    "github.com/gin-gonic/gin"
)

func main() {
    flag.Parse()
    router := gin.New()
    router.Use(ginglog.Logger(3 * time.Second))
    //..
    router.Use(gin.Recovery())

    glog.Warning("warning")
    glog.Error("err")
    glog.Info("info")
    glog.V(2).Infoln("This line will be printed if you use -v=N with N >= 2.")

    router.Run(":8080")
}

STDERR output of the example above. Lines with prefix "[Gin-Debug]" are hardcoded output of Gin and will not appear in your logfile:

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

W0306 16:37:12.836001     367 main.go:18] warning
E0306 16:37:12.836335     367 main.go:19] err
I0306 16:37:12.836402     367 main.go:20] info
I0306 16:26:33.901278   32538 main.go:19] This line will be printed if you use -v=N with N >= 2.
[GIN-debug] Listening and serving HTTP on :8080

Synopsis

Glog will add the following flags to your binary:

-alsologtostderr
      log to standard error as well as files
-log_backtrace_at value
      when logging hits line file:N, emit a stack trace (default :0)
-log_dir string
      If non-empty, write log files in this directory
-logtostderr
      log to standard error instead of files
-stderrthreshold value
      logs at or above this threshold go to stderr
-v value
      log level for V logs
-vmodule value
      comma-separated list of pattern=N settings for file-filtered logging

Contributing/TODO

We welcome contributions from the community; just submit a pull request. To help you get started, here are some items that we'd love help with:

  • Remove hardcoded logs in Gin
  • the code base

Please use github issues as starting point for contributions, new ideas or bugreports.

Contact

Contributors

Thanks to:

  • <your name>

License

See LICENSE file.

Documentation

Overview

Package ginglog provides a logging middleware to get https://github.com/golang/glog as logging library for https://github.com/gin-gonic/gin. It can be used as replacement for the internal logging middleware http://godoc.org/github.com/gin-gonic/gin#Logger.

Example:

package main
import (
    "flag
    "time"
    "github.com/golang/glog"
    "github.com/szuecs/gin-glog"
    "github.com/gin-gonic/gin"
)
func main() {
    flag.Parse()
    router := gin.New()
    router.Use(ginglog.Logger(3 * time.Second))
    //..
    router.Use(gin.Recovery())
    glog.Info("bootstrapped application")
    router.Run(":8080")
}

Your service will get new command line parameters from https://github.com/golang/glog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorLogger

func ErrorLogger() gin.HandlerFunc

ErrorLogger returns an ErrorLoggerT with parameter gin.ErrorTypeAny

func ErrorLoggerT

func ErrorLoggerT(typ gin.ErrorType) gin.HandlerFunc

ErrorLoggerT returns an ErrorLoggerT middleware with the given type gin.ErrorType.

func Logger

func Logger(duration time.Duration) gin.HandlerFunc

Logger prints a logline for each request and measures the time to process for a call. It formats the log entries similar to http://godoc.org/github.com/gin-gonic/gin#Logger does.

Example:

router := gin.New()
router.Use(ginglog.Logger(3 * time.Second))

Types

This section is empty.

Jump to

Keyboard shortcuts

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