59 lines
1.0 KiB
Go
59 lines
1.0 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-Real-IP")
|
|
if len(ip) <= 1 {
|
|
ip = r.RemoteAddr
|
|
}
|
|
return m.log.WithFields(log.Fields{
|
|
"remote": ip,
|
|
"request": request,
|
|
})
|
|
}
|