sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/lib/log/main.go

59 lines
1.1 KiB
Go

package log
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/rifflock/lfshook"
)
// Log current logger
var Log *log.Logger
// ModulLog with current Log
type ModulLog struct {
log *log.Entry
}
// NewSilenceLogger initial logger withou output
func NewSilenceLogger(path string) *log.Logger {
Log = NewLogger(path)
//Log.Out = nil
return Log
}
// NewLogger initial logger
func NewLogger(path string) *log.Logger {
if Log != nil {
return Log
}
Log = log.New()
//Log.Formatter = new(log.JSONFormatter)
Log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
log.WarnLevel: path,
log.ErrorLevel: path,
}))
return Log
}
// NewModulLog with modul field
func NewModulLog(modul string) *ModulLog {
return &ModulLog{
log: Log.WithFields(log.Fields{
"modul": modul,
}),
}
}
// GetLog with api request ip in log
func (m *ModulLog) GetLog(r *http.Request, request string) *log.Entry {
ip := r.Header.Get("X-Forwarded-For")
if len(ip) <= 1 {
ip = r.RemoteAddr
}
return m.log.WithFields(log.Fields{
"remote": ip,
"request": request,
})
}