aboutsummaryrefslogtreecommitdiff
path: root/clog/clog.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-01-13 14:12:18 +0800
committerRunxi Yu <me@runxiyu.org>2025-01-13 14:12:18 +0800
commita5e3bfa7f08b4b58844c9be770423772e6034b81 (patch)
treea16819ebeead9080cd6f8d69fa8e96a609c08552 /clog/clog.go
parentmisc: Add Duplex (diff)
downloadgo-lindenii-common-a5e3bfa7f08b4b58844c9be770423772e6034b81.tar.gz
go-lindenii-common-a5e3bfa7f08b4b58844c9be770423772e6034b81.tar.zst
go-lindenii-common-a5e3bfa7f08b4b58844c9be770423772e6034b81.zip
Add color to logging functions
Diffstat (limited to 'clog/clog.go')
-rw-r--r--clog/clog.go31
1 files changed, 19 insertions, 12 deletions
diff --git a/clog/clog.go b/clog/clog.go
index 6404219..0483585 100644
--- a/clog/clog.go
+++ b/clog/clog.go
@@ -7,32 +7,39 @@ import (
)
func log(str string, keyvals []any) {
- fmt.Fprint(os.Stderr, str + " ")
- for i, j := range keyvals {
- if i&1 == 0 {
- fmt.Fprintf(os.Stderr, "%v=", j)
- } else if i == len(keyvals)-1 {
- fmt.Fprintf(os.Stderr, "%#v", j)
- } else {
- fmt.Fprintf(os.Stderr, "%#v ", j)
+ fmt.Fprint(os.Stderr, str)
+ if len(keyvals) != 0 {
+ fmt.Fprint(os.Stderr, " ")
+ for i, j := range keyvals {
+ if i&1 == 0 {
+ fmt.Fprintf(os.Stderr, "%v=", j)
+ } else if i == len(keyvals)-1 {
+ fmt.Fprintf(os.Stderr, "%#v", j)
+ } else {
+ fmt.Fprintf(os.Stderr, "%#v ", j)
+ }
}
}
fmt.Fprintln(os.Stderr)
}
func Error(str string, keyvals ...any) {
- log("ERROR "+str, keyvals)
+ log("\x1b[1;91mERROR: "+str, keyvals)
+}
+
+func Warn(str string, keyvals ...any) {
+ log("\x1b[1;93mWARNING: "+str, keyvals)
}
func Debug(str string, keyvals ...any) {
- log("DEBUG "+str, keyvals)
+ log("\x1b[90mDEBUG: "+str, keyvals)
}
func Info(str string, keyvals ...any) {
- log("INFO "+str, keyvals)
+ log("\x1b[0mINFO: "+str, keyvals)
}
func Fatal(exit int, str string, keyvals ...any) {
- log("FATAL "+str, keyvals)
+ log("\x1b[1;35mFATAL: "+str, keyvals)
os.Exit(exit)
}