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

47 lines
772 B
Go

package log
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/rifflock/lfshook"
)
var Log *log.Logger
type ModulLog struct {
log *log.Entry
}
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
}
func NewModulLog(modul string) *ModulLog {
return &ModulLog{
log: Log.WithFields(log.Fields{
"modul": modul,
}),
}
}
func (m *ModulLog) GetLog(r *http.Request, request string) *log.Entry {
ip := r.Header.Get("X-Real-IP")
if len(ip) <= 1 {
ip = r.RemoteAddr
}
return m.log.WithFields(log.Fields{
"remote": ip,
"request": request,
})
}