basics
This commit is contained in:
parent
058ab2bad2
commit
1de9439225
3
.timer.toml
Normal file
3
.timer.toml
Normal file
@ -0,0 +1,3 @@
|
||||
## dates and their corresponding seconds been here :)
|
||||
[24-11-15]
|
||||
u80864958_at_u80864958 = 979
|
21
cover.cov
Normal file
21
cover.cov
Normal file
@ -0,0 +1,21 @@
|
||||
mode: set
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:6.51,11.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:14.61,17.2 2 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:20.34,22.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:25.50,27.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:30.33,32.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:35.49,37.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:40.35,42.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:45.49,47.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:50.34,52.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:55.50,57.2 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:60.42,63.13 2 1
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:64.13,65.21 1 1
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:66.12,67.21 1 1
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:68.12,69.21 1 1
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:70.13,71.21 1 1
|
||||
git.schreifuchs.ch/schreifuchs/logger/log.go:73.2,73.32 1 1
|
||||
git.schreifuchs.ch/schreifuchs/logger/resource.go:23.29,27.12 2 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/resource.go:27.12,28.22 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/resource.go:28.22,30.4 1 0
|
||||
git.schreifuchs.ch/schreifuchs/logger/resource.go:34.2,37.3 1 0
|
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module git.schreifuchs.ch/schreifuchs/logger
|
||||
|
||||
go 1.22.5
|
61
log.go
Normal file
61
log.go
Normal file
@ -0,0 +1,61 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/logger/lvl"
|
||||
)
|
||||
|
||||
// Log logs a message according to the level
|
||||
func (l *Logger) Log(level lvl.Level, message string) {
|
||||
l.LogChan <- &Log{
|
||||
Level: level,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
// Logf is the same as Log but with fmt.Sprintf
|
||||
func (l *Logger) Logf(level lvl.Level, format string, a ...any) {
|
||||
str := fmt.Sprintf(format, a...)
|
||||
l.Log(level, str)
|
||||
}
|
||||
|
||||
// Debug logs at the Debug level
|
||||
func (l *Logger) Debug(a ...any) {
|
||||
l.Log(lvl.Debug, fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Debugf logs formated at the Debug level
|
||||
func (l *Logger) Debugf(format string, a ...any) {
|
||||
l.Logf(lvl.Debug, format, a...)
|
||||
}
|
||||
|
||||
// Info logs at the Info level
|
||||
func (l *Logger) Info(a ...any) {
|
||||
l.Log(lvl.Info, fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Infof logs formated at the Info level
|
||||
func (l *Logger) Infof(format string, a ...any) {
|
||||
l.Logf(lvl.Info, format, a...)
|
||||
}
|
||||
|
||||
// Warn logs at the Warn level
|
||||
func (l *Logger) Warn(str ...any) {
|
||||
l.Log(lvl.Warn, fmt.Sprint(str...))
|
||||
}
|
||||
|
||||
// Warnf logs formated at the Warn level
|
||||
func (l *Logger) Warnf(format string, a ...any) {
|
||||
l.Logf(lvl.Warn, format, a...)
|
||||
}
|
||||
|
||||
// Error logs at the Error level
|
||||
func (l *Logger) Error(a ...any) {
|
||||
l.Log(lvl.Error, fmt.Sprint(a...))
|
||||
}
|
||||
|
||||
// Errorf logs formated at the Error level
|
||||
func (l *Logger) Errorf(format string, a ...any) {
|
||||
l.Logf(lvl.Error, format, a...)
|
||||
}
|
31
log_test.go
Normal file
31
log_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package logger_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/logger"
|
||||
"git.schreifuchs.ch/schreifuchs/logger/lvl"
|
||||
)
|
||||
|
||||
func TestNewWith(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
expected := "Hello World"
|
||||
got := ""
|
||||
|
||||
wg.Add(1)
|
||||
l := logger.NewWithStrategy(lvl.Debug, func(c <-chan *logger.Log) {
|
||||
for m := range c {
|
||||
got = m.Message
|
||||
wg.Done()
|
||||
}
|
||||
})
|
||||
|
||||
l.Debug(expected)
|
||||
wg.Wait()
|
||||
|
||||
if expected != got {
|
||||
t.Errorf("Expected %s but got %s", expected, got)
|
||||
}
|
||||
|
||||
}
|
44
lvl/lvl.go
Normal file
44
lvl/lvl.go
Normal file
@ -0,0 +1,44 @@
|
||||
package lvl
|
||||
|
||||
type Level int
|
||||
|
||||
const (
|
||||
Debug Level = iota
|
||||
Info
|
||||
Warn
|
||||
Error
|
||||
)
|
||||
|
||||
// String makes the Level to a string
|
||||
func (l Level) String() string {
|
||||
switch l {
|
||||
case Debug:
|
||||
return "Debug"
|
||||
case Info:
|
||||
return "Info"
|
||||
case Warn:
|
||||
return "Warn"
|
||||
case Error:
|
||||
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"
|
||||
|
||||
}
|
60
lvl/lvl_test.go
Normal file
60
lvl/lvl_test.go
Normal file
@ -0,0 +1,60 @@
|
||||
package lvl
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
cases := []struct {
|
||||
l Level
|
||||
e string
|
||||
}{
|
||||
{
|
||||
l: Debug,
|
||||
e: "Debug",
|
||||
},
|
||||
{
|
||||
l: Info,
|
||||
e: "Info",
|
||||
},
|
||||
{
|
||||
l: Warn,
|
||||
e: "Warn",
|
||||
},
|
||||
{
|
||||
l: Error,
|
||||
e: "Error",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.e, func(t *testing.T) {
|
||||
s := c.l.String()
|
||||
|
||||
if s != c.e {
|
||||
t.Errorf("Expected %s but got %s", c.e, s)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestColor(t *testing.T) {
|
||||
testCases := []struct {
|
||||
level Level
|
||||
message string
|
||||
expected 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"},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
48
resource.go
Normal file
48
resource.go
Normal file
@ -0,0 +1,48 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.schreifuchs.ch/schreifuchs/logger/lvl"
|
||||
)
|
||||
|
||||
type Log struct {
|
||||
Message string
|
||||
Level lvl.Level
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
Level lvl.Level
|
||||
LogChan chan *Log
|
||||
// OutFile io.Writer
|
||||
}
|
||||
|
||||
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 {
|
||||
c := make(chan *Log, 20)
|
||||
|
||||
go log(c)
|
||||
|
||||
return &Logger{
|
||||
Level: lvl,
|
||||
LogChan: c,
|
||||
}
|
||||
|
||||
}
|
1
strategy/console.go
Normal file
1
strategy/console.go
Normal file
@ -0,0 +1 @@
|
||||
package strategy
|
Loading…
Reference in New Issue
Block a user