diff --git a/.timer.toml b/.timer.toml new file mode 100644 index 0000000..d53eed9 --- /dev/null +++ b/.timer.toml @@ -0,0 +1,3 @@ +## dates and their corresponding seconds been here :) +[24-11-15] +u80864958_at_u80864958 = 979 diff --git a/cover.cov b/cover.cov new file mode 100644 index 0000000..4fea549 --- /dev/null +++ b/cover.cov @@ -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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8242cc2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.schreifuchs.ch/schreifuchs/logger + +go 1.22.5 diff --git a/log.go b/log.go new file mode 100644 index 0000000..771b07f --- /dev/null +++ b/log.go @@ -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...) +} diff --git a/log_test.go b/log_test.go new file mode 100644 index 0000000..f7d1a38 --- /dev/null +++ b/log_test.go @@ -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) + } + +} diff --git a/lvl/lvl.go b/lvl/lvl.go new file mode 100644 index 0000000..599a6e5 --- /dev/null +++ b/lvl/lvl.go @@ -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" + +} diff --git a/lvl/lvl_test.go b/lvl/lvl_test.go new file mode 100644 index 0000000..53ab640 --- /dev/null +++ b/lvl/lvl_test.go @@ -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) + } + } +} diff --git a/resource.go b/resource.go new file mode 100644 index 0000000..5d0f9ac --- /dev/null +++ b/resource.go @@ -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, + } + +} diff --git a/strategy/console.go b/strategy/console.go new file mode 100644 index 0000000..4271d52 --- /dev/null +++ b/strategy/console.go @@ -0,0 +1 @@ +package strategy