Skip to content

Golang package for writing logs or http panic to file

Notifications You must be signed in to change notification settings

vinbyte/logpaniccollector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc

What is it ?

The Golang package for writing log or http panic into file. You can combine this package with Filebeat or Promtail.

Install

go get -u github.com/vinbyte/logpaniccollector

You can also use vendoring tools like Govendor, Dep, or something else.

Docs

https://godoc.org/github.com/vinbyte/logpaniccollector

Usage

Please make your middleware to use the WritePanic function. Below is the example of Gin middleware

middleware/middleware.go :

package middleware

import (
	"fmt"
	"runtime/debug"

	"github.com/gin-gonic/gin"
	"github.com/vinbyte/logpaniccollector"
)

//Middleware ...
type Middleware struct{}

// PanicCatcher is use for collecting panic that happened in endpoint
func (w *Middleware) PanicCatcher(lpc *logpaniccollector.LogPanic) gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			isPanic := lpc.RecoverPanic(c.Request.RequestURI, recover())
			if isPanic {
				c.JSON(500, gin.H{
					"status":  500,
					"message": "Internal server error",
				})
				c.Abort()
			}
		}()
		c.Next()
	}
}

main.go

package main

import (
	"[YOUR_MODULE_NAME]/middleware"
	"github.com/vinbyte/logpaniccollector"
	"github.com/gin-gonic/gin"
)

func main() {
    //...
	r := gin.Default()
	lpc := logpaniccollector.New()
	lpc.LogFile = "log.log"
	lpc.AutoRemoveLog("* * * * *")
	var midd = new(middleware.Middleware)
	r.Use(midd.PanicCatcher(lpc))
    //...
}

To Do