47 lines
772 B
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,
|
|
})
|
|
}
|