improved logging

This commit is contained in:
ston1th 2022-09-19 22:52:40 +02:00
commit 05b41cedce
81 changed files with 9802 additions and 279 deletions

View file

@ -4,10 +4,6 @@ package server
import (
"errors"
"git.giftfish.de/ston1th/docstore/pkg/core"
"git.giftfish.de/ston1th/docstore/pkg/log"
"git.giftfish.de/ston1th/jwt/v3"
"github.com/gorilla/mux"
"html/template"
"io"
"net/http"
@ -15,6 +11,10 @@ import (
"reflect"
"strconv"
"time"
"git.giftfish.de/ston1th/docstore/pkg/core"
"git.giftfish.de/ston1th/jwt/v3"
"github.com/gorilla/mux"
)
const (
@ -93,7 +93,7 @@ func (c *Context) Exec() {
c.Err = errors.New("template is nil")
return
}
c.Data.Token = newXsrf(c.Token.RawSig()[:keySize])
c.Data.Token, c.Err = newXsrf(c.Token.RawSig()[:keySize])
c.Data.Version = c.Srv.Config.Version
if c.Data.BodyTitle == "" {
@ -123,10 +123,20 @@ func (c *Context) Log() {
c.HTTPStatus = int(reflect.Indirect(reflect.ValueOf(c.Response)).FieldByName("status").Int())
}
if c.Err != nil {
log.Printf("%s %s %s %d error: %s\n", c.Request.RemoteAddr, c.Request.Method, c.Request.URL, c.HTTPStatus, c.Err)
c.Srv.Log.Error(c.Err, "access",
"client", c.Request.RemoteAddr,
"method", c.Request.Method,
"status", c.HTTPStatus,
"uri", c.Request.URL.Path,
)
return
}
log.Printf("%s %s %s %d\n", c.Request.RemoteAddr, c.Request.Method, c.Request.URL, c.HTTPStatus)
c.Srv.Log.Info("access",
"client", c.Request.RemoteAddr,
"method", c.Request.Method,
"status", c.HTTPStatus,
"uri", c.Request.URL.Path,
)
}
func (c *Context) Error(i interface{}) {
@ -154,7 +164,7 @@ func (c *Context) Forbidden() {
}
func (c *Context) SwitchHandler() {
log.Debug("server: switching to normal handler")
c.Srv.Log.V(2).Info("switching to normal handler")
c.Srv.srv.Handler = c.Srv.handler
c.Redirect(core.IndexURI, http.StatusFound)
}
@ -181,7 +191,13 @@ func (c *Context) Redirect(uri string, code int) {
if len(uri) > 0 && uri[0] != '/' {
uri = "/" + uri
}
log.Printf("%s %s %s %d %s\n", c.Request.RemoteAddr, c.Request.Method, c.Request.URL, code, uri)
c.Srv.Log.Info("access",
"client", c.Request.RemoteAddr,
"method", c.Request.Method,
"status", code,
"uri", c.Request.URL.Path,
"redirect", uri,
)
http.Redirect(c.Response, c.Request, uri, code)
}
@ -244,7 +260,7 @@ func (c *Context) LoggedOn() (ok bool) {
}
func (c *Context) LogSetCookie(msg string, err error) {
log.Printf("ctx: %s %s\n", msg, err)
c.Srv.Log.Error(err, msg)
c.SetCookie(nil, 0)
}
@ -266,7 +282,7 @@ func (c *Context) setCookie(t *jwt.Token, d time.Duration) {
}
t.Claims[jwt.ExpClaim] = jwt.NewExp(d)
if err := c.Srv.JWT.Sign(t); err != nil {
log.Println(err)
c.Srv.Log.Error(err, "jwt sign")
return
}
c.Token = *t