genofire/hs_monolith
genofire
/
hs_monolith
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.
hs_monolith/lib/log.go

34 lines
573 B
Go
Raw Normal View History

2017-03-30 13:51:22 +02:00
package lib
import (
2017-03-30 14:16:13 +02:00
"log"
2017-03-30 13:51:22 +02:00
"net/http"
2017-03-30 14:16:13 +02:00
logger "github.com/Sirupsen/logrus"
2017-03-30 13:51:22 +02:00
)
2017-03-30 14:16:13 +02:00
var Log *logger.Logger
2017-03-30 14:08:34 +02:00
func init(){
2017-03-30 14:16:13 +02:00
Log = logger.New()
2017-03-30 14:20:00 +02:00
log.SetOutput(Log.Writer())
2017-03-30 14:08:34 +02:00
}
2017-03-30 13:51:22 +02:00
2017-03-30 14:02:58 +02:00
func LogTimestamp(value bool) {
2017-03-30 14:16:13 +02:00
logger.SetFormatter(&logger.TextFormatter{
2017-03-30 14:01:01 +02:00
DisableTimestamp: value,
})
}
2017-03-30 13:51:22 +02:00
// LogHTTP to add information of a httprequest to log
2017-03-30 14:16:13 +02:00
func LogHTTP(r *http.Request) *logger.Entry {
2017-03-30 13:51:22 +02:00
ip := r.Header.Get("X-Forwarded-For")
if len(ip) <= 1 {
ip = r.RemoteAddr
}
2017-03-30 14:16:13 +02:00
return Log.WithFields(logger.Fields{
2017-03-30 13:51:22 +02:00
"remote": ip,
"method": r.Method,
2017-03-30 14:37:04 +02:00
"url": r.URL.RequestURI(),
2017-03-30 13:51:22 +02:00
})
}