4 Commits

Author SHA1 Message Date
5141acaa12 remove duplicated JSON 2025-05-09 18:43:02 +02:00
4e2b95f945 added more strategies 2025-05-09 18:32:06 +02:00
2283c0f9b8 add interface to readme 2025-03-11 12:16:58 +01:00
1d0e63d91c add Fatal 2024-12-11 11:11:51 +01:00
10 changed files with 151 additions and 39 deletions

View File

@ -1,3 +1,9 @@
## dates and their corresponding seconds been here :)
[24-11-15]
u80864958_at_u80864958 = 979
[24-11-18]
u80864958_at_u80864958 = 3139
[25-03-11]
schreifuchs_at_archibald = 195
[25-05-09]
schreifuchs_at_archibald = 10

View File

@ -1,3 +1,24 @@
# logger
Just a simple logger
## Logger interfaces
Full:
```go
type Logger interface {
Debug(a ...any)
Debugf(format string, a ...any)
Info(a ...any)
Infof(format string, a ...any)
Warn(str ...any)
Warnf(format string, a ...any)
Error(a ...any)
Errorf(format string, a ...any)
Fatal(code int, a ...any)
Fatalf(code int, format string, a ...any)
}
```

13
log.go
View File

@ -2,6 +2,7 @@ package logger
import (
"fmt"
"os"
"git.schreifuchs.ch/schreifuchs/logger/lvl"
)
@ -59,3 +60,15 @@ func (l *Logger) Error(a ...any) {
func (l *Logger) Errorf(format string, a ...any) {
l.Logf(lvl.Error, format, a...)
}
// Fatal logs directly do stdout and exis the application with given code.
func (l *Logger) Fatal(code int, a ...any) {
fmt.Println("Fatal:", fmt.Sprint(a...))
os.Exit(code)
}
// Fatalf logs formated directly do stdout and exis the application with given code.
func (l *Logger) Fatalf(code int, format string, a ...any) {
fmt.Println("Fatal:", fmt.Sprintf(format, a...))
os.Exit(code)
}

View File

@ -3,6 +3,7 @@ package logger_test
import (
"sync"
"testing"
"time"
"git.schreifuchs.ch/schreifuchs/logger"
"git.schreifuchs.ch/schreifuchs/logger/lvl"
@ -27,5 +28,19 @@ func TestNewWith(t *testing.T) {
if expected != got {
t.Errorf("Expected %s but got %s", expected, got)
}
}
func ExampleLog() {
log := logger.New(lvl.Debug)
log.Debug("Hello World")
log.Info("Hello World")
log.Warn("Hello World")
log.Error("Hello World")
time.Sleep(time.Second / 100)
// Output:
// Debug: Hello World
// Info: Hello World
// Warn: Hello World
// Error: Hello World
}

View File

@ -22,23 +22,8 @@ func (l Level) String() string {
return "Error"
}
return "Undefined"
}
// Colorize colors a string for the shell matching to its level
func (l Level) Colorize(str string) string {
var color string
switch l {
case Debug:
color = "\033[32m" // green
case Info:
color = "\033[97m" // white
case Warn:
color = "\033[33m" // yellow
case Error:
color = "\033[31m" // red
}
return color + str + "\033[0m"
func (l Level) MarshalJSON() ([]byte, error) {
return []byte("\"" + l.String() + "\""), nil
}

View File

@ -36,25 +36,38 @@ func TestString(t *testing.T) {
}
})
}
}
func TestColor(t *testing.T) {
testCases := []struct {
level Level
message string
expected string
func TestMarshalJSON(t *testing.T) {
cases := []struct {
l Level
e string
}{
{Debug, "Debug message", "\033[32mDebug message\033[0m"},
{Info, "Info message", "\033[97mInfo message\033[0m"},
{Warn, "Warning message", "\033[33mWarning message\033[0m"},
{Error, "Error message", "\033[31mError message\033[0m"},
{
l: Debug,
e: `"Debug"`,
},
{
l: Info,
e: `"Info"`,
},
{
l: Warn,
e: `"Warn"`,
},
{
l: Error,
e: `"Error"`,
},
}
for _, tc := range testCases {
coloredMessage := tc.level.Colorize(tc.message)
if coloredMessage != tc.expected {
t.Errorf("For level %v, expected '%v', but got '%v'", tc.level, tc.expected, coloredMessage)
for _, c := range cases {
t.Run(c.e, func(t *testing.T) {
res, _ := c.l.MarshalJSON()
if string(res) != c.e {
t.Errorf("Expected %s but got %s", c.e, res)
}
})
}
}

View File

@ -1,3 +1,4 @@
// logger is a async logger
package logger
import (
@ -18,21 +19,18 @@ type Logger struct {
}
func New(l lvl.Level) *Logger {
c := make(chan *Log, 20)
go func() {
for log := range c {
fmt.Printf("%v: %s\n", log.Level, log.Message)
}
}()
return &Logger{
Level: l,
LogChan: c,
}
}
func NewWithStrategy(lvl lvl.Level, log func(<-chan *Log)) *Logger {
@ -44,5 +42,4 @@ func NewWithStrategy(lvl lvl.Level, log func(<-chan *Log)) *Logger {
Level: lvl,
LogChan: c,
}
}

29
strategy/colored.go Normal file
View File

@ -0,0 +1,29 @@
package strategy
import (
"git.schreifuchs.ch/schreifuchs/logger"
"git.schreifuchs.ch/schreifuchs/logger/lvl"
)
func Colored(logs <-chan *logger.Log) {
for log := range logs {
println(colorize(log.Level, log.Message))
}
}
// colorize colors a string for the shell matching to its level
func colorize(l lvl.Level, str string) string {
var color string
switch l {
case lvl.Debug:
color = "\033[32m" // green
case lvl.Info:
color = "\033[97m" // white
case lvl.Warn:
color = "\033[33m" // yellow
case lvl.Error:
color = "\033[31m" // red
}
return color + str + "\033[0m"
}

View File

@ -1 +0,0 @@
package strategy

34
strategy/json.go Normal file
View File

@ -0,0 +1,34 @@
package strategy
import (
"encoding/json"
"fmt"
"os"
"time"
"git.schreifuchs.ch/schreifuchs/logger"
"git.schreifuchs.ch/schreifuchs/logger/lvl"
)
// JSON logs to std wit the schema:
// {"time":"","level":"","message":""}.
func JSON(logs <-chan *logger.Log) {
for log := range logs {
timedLog := struct {
Time time.Time `json:"time"`
LVL lvl.Level `json:"level"`
Message string `json:"message"`
}{
Time: time.Now(),
LVL: log.Level,
Message: log.Message,
}
out, err := json.Marshal(timedLog)
if err != nil {
fmt.Println("Error while marshaling log: ", err)
os.Exit(1)
}
println(string(out))
}
}