mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-29 00:55:32 +00:00
better logging
This commit is contained in:
@@ -1,24 +1,43 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers/logging"
|
||||
)
|
||||
|
||||
func Panic(err error, msg string) {
|
||||
fmt.Println(msg)
|
||||
if err != nil {
|
||||
fmt.Println("Error: ", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func MaybePanic(err error, msg string) {
|
||||
func Assert(err error, msg ...string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(msg)
|
||||
fmt.Println("Error: ", err)
|
||||
logging.Error(err, msg...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func AssertNonNil(obj interface{}, msg ...string) {
|
||||
if obj != nil {
|
||||
return
|
||||
}
|
||||
|
||||
logging.Error(nil, msg...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func AssertNil(obj interface{}, msg ...string) {
|
||||
if obj == nil {
|
||||
return
|
||||
}
|
||||
|
||||
logging.Error(nil, msg...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func AssertStr(str string, msg ...string) {
|
||||
if str != "" {
|
||||
return
|
||||
}
|
||||
|
||||
logging.Error(nil, msg...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package helpers
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type EventMux[T any] struct {
|
||||
// INFO: This is a simple event multiplexer that allows to subscribe to events and to publish them.
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package helpers
|
||||
|
||||
import "fmt"
|
||||
|
||||
func LogOnDebug[T fmt.Stringer](object *T, msg string) {
|
||||
if msg != "" {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
|
||||
if object != nil {
|
||||
fmt.Println(*object)
|
||||
}
|
||||
}
|
||||
|
||||
func LogOnErr[T fmt.Stringer](object *T, err error, msg string) {
|
||||
if err != nil {
|
||||
if msg != "" {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
|
||||
if object != nil {
|
||||
fmt.Println(*object)
|
||||
}
|
||||
fmt.Println("Error: ", err)
|
||||
}
|
||||
}
|
||||
66
helpers/logging/logging.go
Normal file
66
helpers/logging/logging.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
func ObjDebug[T fmt.Stringer](object *T, msg string) {
|
||||
if msg != "" {
|
||||
slog.Debug(msg)
|
||||
}
|
||||
|
||||
if object != nil {
|
||||
obj := *object
|
||||
slog.Debug(obj.String())
|
||||
}
|
||||
}
|
||||
|
||||
func ObjErr[T fmt.Stringer](object *T, err error, msg ...string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
slog.Error(m)
|
||||
}
|
||||
}
|
||||
|
||||
if object != nil {
|
||||
obj := *object
|
||||
slog.Debug(obj.String())
|
||||
}
|
||||
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
|
||||
func Error(err error, msg ...string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
slog.Error(m)
|
||||
}
|
||||
}
|
||||
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
|
||||
func Info(msg ...string) {
|
||||
if len(msg) > 0 {
|
||||
for _, m := range msg {
|
||||
slog.Info(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SetDebug() {
|
||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
}
|
||||
|
||||
func SetInfo() {
|
||||
slog.SetLogLoggerLevel(slog.LevelInfo)
|
||||
}
|
||||
57
helpers/parselog.go
Normal file
57
helpers/parselog.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package helpers
|
||||
|
||||
import "sync"
|
||||
|
||||
type LogMessage struct {
|
||||
Commit string
|
||||
File string
|
||||
Message string
|
||||
Fatal bool
|
||||
}
|
||||
|
||||
type ParseLogger struct {
|
||||
mu sync.Mutex
|
||||
Messages []LogMessage
|
||||
}
|
||||
|
||||
func NewParseLog() *ParseLogger {
|
||||
return &ParseLogger{
|
||||
Messages: make([]LogMessage, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParseLogger) AddMessage(commit, file, message string, fatal bool) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
p.Messages = append(p.Messages, LogMessage{
|
||||
Commit: commit,
|
||||
File: file,
|
||||
Message: message,
|
||||
Fatal: fatal,
|
||||
})
|
||||
}
|
||||
|
||||
func (p *ParseLogger) Fatal() bool {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
for _, m := range p.Messages {
|
||||
if m.Fatal {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *ParseLogger) GetMessages() []LogMessage {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
res := make([]LogMessage, len(p.Messages))
|
||||
copy(res, p.Messages)
|
||||
return res
|
||||
}
|
||||
|
||||
func (p *ParseLogger) Clear() {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
p.Messages = make([]LogMessage, 0)
|
||||
}
|
||||
@@ -3,10 +3,10 @@ package helpers
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers/logging"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
@@ -96,7 +96,6 @@ func (fw *FileWatcher) Watch() error {
|
||||
return
|
||||
}
|
||||
if !event.Has(fsnotify.Chmod) {
|
||||
log.Println("event:", event)
|
||||
fw.mu.Lock()
|
||||
for _, wf := range fw.wf {
|
||||
wf(event.Name)
|
||||
@@ -107,7 +106,7 @@ func (fw *FileWatcher) Watch() error {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
log.Println("error:", err)
|
||||
logging.Error(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user