
committed by
Sebastian Serth

parent
4d661138e9
commit
0fd6e42487
@ -181,6 +181,14 @@ func (f File) ByteContent() []byte {
|
||||
}
|
||||
}
|
||||
|
||||
// Formatter mirrors the available Formatters of logrus for configuration purposes.
|
||||
type Formatter string
|
||||
|
||||
const (
|
||||
FormatterText = "TextFormatter"
|
||||
FormatterJSON = "JSONFormatter"
|
||||
)
|
||||
|
||||
// ContextKey is the type for keys in a request context that is used for passing data to the next handler.
|
||||
type ContextKey string
|
||||
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/openHPI/poseidon/pkg/dto"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net"
|
||||
"net/http"
|
||||
@ -12,10 +13,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const TimestampFormat = "2006-01-02T15:04:05.000000Z"
|
||||
|
||||
var log = &logrus.Logger{
|
||||
Out: os.Stderr,
|
||||
Formatter: &logrus.TextFormatter{
|
||||
TimestampFormat: "2006-01-02T15:04:05.000000Z",
|
||||
TimestampFormat: TimestampFormat,
|
||||
DisableColors: true,
|
||||
FullTimestamp: true,
|
||||
},
|
||||
@ -25,13 +28,18 @@ var log = &logrus.Logger{
|
||||
|
||||
const GracefulSentryShutdown = 5 * time.Second
|
||||
|
||||
func InitializeLogging(loglevel string) {
|
||||
level, err := logrus.ParseLevel(loglevel)
|
||||
func InitializeLogging(logLevel string, formatter dto.Formatter) {
|
||||
level, err := logrus.ParseLevel(logLevel)
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("Error parsing loglevel")
|
||||
return
|
||||
}
|
||||
log.SetLevel(level)
|
||||
if formatter == dto.FormatterJSON {
|
||||
log.Formatter = &logrus.JSONFormatter{
|
||||
TimestampFormat: TimestampFormat,
|
||||
}
|
||||
}
|
||||
log.AddHook(&SentryHook{})
|
||||
log.ExitFunc = func(i int) {
|
||||
sentry.Flush(GracefulSentryShutdown)
|
||||
@ -43,23 +51,23 @@ func GetLogger(pkg string) *logrus.Entry {
|
||||
return log.WithField("package", pkg)
|
||||
}
|
||||
|
||||
// loggingResponseWriter wraps the default http.ResponseWriter and catches the status code
|
||||
// ResponseWriter wraps the default http.ResponseWriter and catches the status code
|
||||
// that is written.
|
||||
type loggingResponseWriter struct {
|
||||
type ResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
StatusCode int
|
||||
}
|
||||
|
||||
func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {
|
||||
return &loggingResponseWriter{w, http.StatusOK}
|
||||
func NewLoggingResponseWriter(w http.ResponseWriter) *ResponseWriter {
|
||||
return &ResponseWriter{w, http.StatusOK}
|
||||
}
|
||||
|
||||
func (writer *loggingResponseWriter) WriteHeader(code int) {
|
||||
func (writer *ResponseWriter) WriteHeader(code int) {
|
||||
writer.StatusCode = code
|
||||
writer.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func (writer *loggingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
func (writer *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
conn, rw, err := writer.ResponseWriter.(http.Hijacker).Hijack()
|
||||
if err != nil {
|
||||
return conn, nil, fmt.Errorf("hijacking connection failed: %w", err)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"github.com/openHPI/poseidon/pkg/dto"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -18,7 +19,7 @@ func mockHTTPStatusHandler(status int) http.Handler {
|
||||
func TestHTTPMiddlewareWarnsWhenInternalServerError(t *testing.T) {
|
||||
var hook *test.Hook
|
||||
log, hook = test.NewNullLogger()
|
||||
InitializeLogging(logrus.DebugLevel.String())
|
||||
InitializeLogging(logrus.DebugLevel.String(), dto.FormatterText)
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
if err != nil {
|
||||
@ -34,7 +35,7 @@ func TestHTTPMiddlewareWarnsWhenInternalServerError(t *testing.T) {
|
||||
func TestHTTPMiddlewareDebugsWhenStatusOK(t *testing.T) {
|
||||
var hook *test.Hook
|
||||
log, hook = test.NewNullLogger()
|
||||
InitializeLogging(logrus.DebugLevel.String())
|
||||
InitializeLogging(logrus.DebugLevel.String(), dto.FormatterText)
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user