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.0 KiB
Go
Raw Normal View History

2016-08-14 15:29:54 +02:00
package log
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/rifflock/lfshook"
)
2016-09-03 10:18:46 +02:00
// Log current logger
2016-08-14 15:29:54 +02:00
var Log *log.Logger
2016-09-03 10:18:46 +02:00
// ModulLog with current Log
2016-08-14 18:29:25 +02:00
type ModulLog struct {
log *log.Entry
}
2016-09-03 10:18:46 +02:00
// NewSilenceLogger initial logger withou output
2016-09-02 21:32:56 +02:00
func NewSilenceLogger(path string) *log.Logger {
Log = NewLogger(path)
//Log.Out = nil
return Log
}
2016-09-03 10:18:46 +02:00
// NewLogger initial logger
2016-08-14 15:29:54 +02:00
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
}
2016-09-03 10:18:46 +02:00
// NewModulLog with modul field
2016-08-14 18:29:25 +02:00
func NewModulLog(modul string) *ModulLog {
return &ModulLog{
log: Log.WithFields(log.Fields{
"modul": modul,
}),
}
}
2016-09-03 10:18:46 +02:00
// GetLog with api request ip in log
2016-08-14 18:29:25 +02:00
func (m *ModulLog) GetLog(r *http.Request, request string) *log.Entry {
2016-08-14 18:41:56 +02:00
ip := r.Header.Get("X-Real-IP")
if len(ip) <= 1 {
ip = r.RemoteAddr
}
2016-08-14 18:29:25 +02:00
return m.log.WithFields(log.Fields{
2016-08-14 18:41:56 +02:00
"remote": ip,
2016-08-14 15:29:54 +02:00
"request": request,
})
}