All Projects → gin-contrib → logger

gin-contrib / logger

Licence: MIT license
Gin middleware/handler to logger url path using rs/zerolog

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to logger

website
Official website and document for Gin
Stars: ✭ 88 (-26.05%)
Mutual labels:  gin, gin-gonic
go-gin-web-server
Deploy Go Gin on Render
Stars: ✭ 23 (-80.67%)
Mutual labels:  gin, gin-gonic
geo-smart-system
Open Source Realtime Tracking System
Stars: ✭ 36 (-69.75%)
Mutual labels:  gin, gin-gonic
ginhelper
gin framework helper
Stars: ✭ 16 (-86.55%)
Mutual labels:  gin, gin-gonic
httpsign
Signing HTTP Messages Middleware
Stars: ✭ 54 (-54.62%)
Mutual labels:  gin, gin-gonic
go-12factor-example
Example the 12factor app using golang
Stars: ✭ 20 (-83.19%)
Mutual labels:  gin, gin-gonic
gin-rest-api
Example golang using gin framework everything you need, i create this tutorial special for beginner.
Stars: ✭ 56 (-52.94%)
Mutual labels:  gin, gin-gonic
gin-swagger
DRY templates for go-swagger
Stars: ✭ 79 (-33.61%)
Mutual labels:  gin, gin-gonic
use-reducer-logger
A very basic logger for the useReducer function in the React Hooks API.
Stars: ✭ 89 (-25.21%)
Mutual labels:  logger, logger-middleware
guzzle-log-middleware
A Guzzle middleware to log request and responses automatically
Stars: ✭ 61 (-48.74%)
Mutual labels:  logger, logger-middleware
go api boilerplate
🐶Go (Golang)🚀REST / GraphQL API + Postgres boilerplate
Stars: ✭ 127 (+6.72%)
Mutual labels:  gin, gin-gonic
requestid
Request ID middleware for Gin Framework
Stars: ✭ 115 (-3.36%)
Mutual labels:  gin, gin-gonic
go-gin-logrus
Gin Web Framework for using Logrus as the Gin logger with Tracing middleware
Stars: ✭ 38 (-68.07%)
Mutual labels:  gin, gin-gonic
NLog
Flexible logging for C# and Unity
Stars: ✭ 158 (+32.77%)
Mutual labels:  logger
glog-gokit
This packages is a replacement for glog in projects that use the go-kit logger.
Stars: ✭ 18 (-84.87%)
Mutual labels:  logger
sprout
Golang logging library supporting log retrieval.
Stars: ✭ 85 (-28.57%)
Mutual labels:  logger
web-marisa
🍄 白丝魔理沙网页版
Stars: ✭ 65 (-45.38%)
Mutual labels:  gin
noodlog
🍜 Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.
Stars: ✭ 42 (-64.71%)
Mutual labels:  logger
ginadmin
基于Gin开发的后台管理系统,集成了、数据库操作、日志管理、权限分配管理、多模板页面、自动分页器、数据库迁移和填充、Docker集成部署等功能、静态资源打包
Stars: ✭ 149 (+25.21%)
Mutual labels:  gin
logger
☠ 😈 👀 Simple,Secure & Undetected (6.11.2017) keylogger for Windows :)
Stars: ✭ 37 (-68.91%)
Mutual labels:  logger

logger

Run Tests codecov Go Report Card GoDoc Join the chat at https://gitter.im/gin-gonic/gin

Gin middleware/handler to logger url path using rs/zerolog.

Example

package main

import (
  "fmt"
  "net/http"
  "regexp"
  "time"

  "github.com/gin-contrib/logger"
  "github.com/gin-contrib/requestid"
  "github.com/gin-gonic/gin"
  "github.com/rs/zerolog"
  "github.com/rs/zerolog/log"
)

var rxURL = regexp.MustCompile(`^/regexp\d*`)

func main() {
  r := gin.New()

  // Add a logger middleware, which:
  //   - Logs all requests, like a combined access and error log.
  //   - Logs to stdout.
  // r.Use(logger.SetLogger())

  // Example pong request.
  r.GET("/pong", logger.SetLogger(), func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Example ping request.
  r.GET("/ping", logger.SetLogger(
    logger.WithSkipPath([]string{"/skip"}),
    logger.WithUTC(true),
    logger.WithSkipPathRegexp(rxURL),
  ), func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Example skip path request.
  r.GET("/skip", logger.SetLogger(
    logger.WithSkipPath([]string{"/skip"}),
  ), func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Example skip path request.
  r.GET("/regexp1", logger.SetLogger(
    logger.WithSkipPathRegexp(rxURL),
  ), func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Example skip path request.
  r.GET("/regexp2", logger.SetLogger(
    logger.WithSkipPathRegexp(rxURL),
  ), func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // add custom fields.
  r.GET("/id", requestid.New(requestid.WithGenerator(func() string {
    return "foobar"
  })), logger.SetLogger(
    logger.WithLogger(func(c *gin.Context, l zerolog.Logger) zerolog.Logger {
      if trace.SpanFromContext(c.Request.Context()).SpanContext().IsValid() {
        l = l.With().
          Str("trace_id", trace.SpanFromContext(c.Request.Context()).SpanContext().TraceID().String()).
          Str("span_id", trace.SpanFromContext(c.Request.Context()).SpanContext().SpanID().String()).
          Logger()
      }

      return l.With().
        Str("id", requestid.Get(c)).
        Str("foo", "bar").
        Str("path", c.Request.URL.Path).
        Logger()
    }),
  ), func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Listen and Server in 0.0.0.0:8080
  if err := r.Run(":8080"); err != nil {
    log.Fatal().Msg("can' start server with 8080 port")
  }
}

Screenshot

Run app server:

go run _example/main.go

Test request:

curl http://localhost:8080/ping
curl http://localhost:8080/pong

screenshot

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].