2017-05-03 07:16:45 +02:00
|
|
|
// Package that provides the functionality to start und initialize the logger
|
2017-03-30 15:36:04 +02:00
|
|
|
package log
|
2017-03-30 14:49:14 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
logger "github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2017-05-03 07:16:45 +02:00
|
|
|
// Crrrent logger with it's configuration
|
2017-03-30 14:49:14 +02:00
|
|
|
var Log *logger.Logger
|
|
|
|
|
2017-04-28 10:14:59 +02:00
|
|
|
// Function to initiate a new logger
|
2017-03-30 17:02:20 +02:00
|
|
|
func init() {
|
2017-03-30 14:49:14 +02:00
|
|
|
Log = logger.New()
|
2017-04-28 10:14:59 +02:00
|
|
|
log.SetOutput(Log.Writer()) // Enable fallback if core logger
|
2017-03-30 14:49:14 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 10:14:59 +02:00
|
|
|
// Function to add the information of a http request to the log
|
2017-03-30 15:36:04 +02:00
|
|
|
func HTTP(r *http.Request) *logger.Entry {
|
2017-03-30 14:49:14 +02:00
|
|
|
ip := r.Header.Get("X-Forwarded-For")
|
|
|
|
if len(ip) <= 1 {
|
|
|
|
ip = r.RemoteAddr
|
|
|
|
}
|
|
|
|
return Log.WithFields(logger.Fields{
|
2017-03-30 17:02:20 +02:00
|
|
|
"remote": ip,
|
2017-03-30 14:49:14 +02:00
|
|
|
"method": r.Method,
|
2017-03-30 17:02:20 +02:00
|
|
|
"url": r.URL.RequestURI(),
|
2017-03-30 14:49:14 +02:00
|
|
|
})
|
|
|
|
}
|