chore: added logger and closes #33

This commit is contained in:
2023-10-06 09:45:47 +01:00
parent 84b9c40a72
commit 73511ec401
7 changed files with 150 additions and 5 deletions

View File

@@ -6,7 +6,6 @@ import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"path"
@@ -14,6 +13,7 @@ import (
"time"
dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
"github.com/charmbracelet/log"
)
func Mul (n1 int, n2 int) int {
@@ -340,6 +340,16 @@ type Context struct {
Token *string
User *dbtypes.User
Mode AnswerType
Logger *log.Logger
}
func (c Context) ErrorCode(err error, code int, data AnyMap) *Error {
// TODO Improve Logging
if err != nil {
c.Logger.Errorf("Something went wrong returning with: %d\n.Err:\n", code)
c.Logger.Error(err)
}
return &Error{code, nil, data}
}
func (c Context) AddMap(m AnyMap) AnyMap {
@@ -384,7 +394,13 @@ func (x Handle) createContext(mode AnswerType, r *http.Request) (*Context, error
return nil, errors.Join(err, LogoffError)
}
return &Context{token, user, mode}, nil
logger := log.NewWithOptions(os.Stdout, log.Options{
ReportTimestamp: true,
TimeFormat: time.Kitchen,
Prefix: r.URL.Path,
})
return &Context{token, user, mode, logger}, nil
}
// TODO check if I can use http.Redirect
@@ -452,6 +468,7 @@ func (x Handle) StaticFiles(pathTest string, fileType string, contentType string
}
func ErrorCode(err error, code int, data AnyMap) *Error {
log.Warn("This function is deprecated please use the one provided by context")
// TODO Improve Logging
if err != nil {
fmt.Printf("Something went wrong returning with: %d\n.Err:\n", code)