summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2026-01-18 16:14:14 +0200
committerMarin Ivanov <[email protected]>2026-01-18 23:55:31 +0200
commit465177182fae40d377d3e4f8e36d81a4edca43ba (patch)
tree8362d899ecfaa784364408d3f8facdddfa566508
Initial commitHEADmaster
-rw-r--r--LICENSE22
-rw-r--r--colors.go15
-rw-r--r--go.mod3
-rw-r--r--levels.go38
-rw-r--r--logger.go41
5 files changed, 119 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..699b76d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+Copyright 2026 Marin Ivanov (metala)
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+this list of conditions and the following disclaimer in the documentation and/or
+other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/colors.go b/colors.go
new file mode 100644
index 0000000..0d04bd9
--- /dev/null
+++ b/colors.go
@@ -0,0 +1,15 @@
+package logging
+
+var Reset = "\033[0m"
+var Red = "\033[31m"
+var Green = "\033[32m"
+var Yellow = "\033[33m"
+var Blue = "\033[34m"
+var Purple = "\033[35m"
+var Cyan = "\033[36m"
+var Gray = "\033[37m"
+var White = "\033[97m"
+
+func colorize(s string, color string) string {
+ return color + s + Reset
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..d1022cb
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module go.metala.org/logging
+
+go 1.21.5
diff --git a/levels.go b/levels.go
new file mode 100644
index 0000000..6b9213e
--- /dev/null
+++ b/levels.go
@@ -0,0 +1,38 @@
+package logging
+
+type LevelEnum uint16
+
+const (
+ None LevelEnum = iota
+ Error
+ Warn
+ Info
+ Debug
+)
+
+type levelInfo struct {
+ abbrev string
+ color string
+}
+
+var (
+ levelInfos = map[LevelEnum]levelInfo{
+ Error: {"ERR", Red},
+ Warn: {"WARN", Yellow},
+ Info: {"INFO", Cyan},
+ Debug: {"DEBUG", Green},
+ }
+)
+
+func (s LevelEnum) Info() levelInfo {
+ info, ok := levelInfos[s]
+ if !ok {
+ return levelInfo{"UNK", White}
+ }
+ return info
+}
+
+func (s LevelEnum) Format() string {
+ info := s.Info()
+ return colorize("["+info.abbrev+"]", info.color)
+}
diff --git a/logger.go b/logger.go
new file mode 100644
index 0000000..010749a
--- /dev/null
+++ b/logger.go
@@ -0,0 +1,41 @@
+package logging
+
+import (
+ "fmt"
+ "log"
+)
+
+type Logger struct {
+ Level LevelEnum
+
+ ns string
+}
+
+func NewLogger(level LevelEnum) *Logger {
+ return &Logger{
+ Level: level,
+ }
+}
+
+func (l *Logger) WithNS(ns string) *Logger {
+ return &Logger{
+ Level: l.Level,
+ ns: formatNS(ns),
+ }
+}
+
+func (l *Logger) Logf(level LevelEnum, format string, args ...any) {
+ if level > l.Level {
+ return
+ }
+ log.Printf("%s%s %s", l.ns, level.Format(), fmt.Sprintf(format, args...))
+}
+
+func (l *Logger) Errorf(format string, args ...any) { l.Logf(Error, format, args...) }
+func (l *Logger) Warnf(format string, args ...any) { l.Logf(Warn, format, args...) }
+func (l *Logger) Infof(format string, args ...any) { l.Logf(Info, format, args...) }
+func (l *Logger) Debugf(format string, args ...any) { l.Logf(Debug, format, args...) }
+
+func formatNS(ns string) string {
+ return colorize("["+ns+"]", Purple)
+}