summaryrefslogtreecommitdiff
path: root/nexus/utils/logger/logger.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-03-29 22:52:46 +0530
committerBobby <[email protected]>2026-03-29 22:52:46 +0530
commit9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch)
treeda520b923b5e6758d5457b6233dd6671fc640914 /nexus/utils/logger/logger.go
parent65a143a0871c35989b7c7ea6723d39a0585c089e (diff)
downloadechoes-of-vaelun-main.tar.xz
echoes-of-vaelun-main.zip
feat: nexus account manager scaffold with auth, characters, realmsHEADmain
Diffstat (limited to 'nexus/utils/logger/logger.go')
-rw-r--r--nexus/utils/logger/logger.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/nexus/utils/logger/logger.go b/nexus/utils/logger/logger.go
new file mode 100644
index 0000000..f1cbbd1
--- /dev/null
+++ b/nexus/utils/logger/logger.go
@@ -0,0 +1,81 @@
+package logger
+
+import (
+ "fmt"
+ "os"
+
+ "go.uber.org/zap"
+ "go.uber.org/zap/zapcore"
+)
+
+var (
+ instance *zap.Logger
+ atomicLevel zap.AtomicLevel
+)
+
+func init() {
+ atomicLevel = zap.NewAtomicLevelAt(zapcore.InfoLevel)
+
+ encoderConfig := zapcore.EncoderConfig{
+ LevelKey: "level",
+ MessageKey: "msg",
+ LineEnding: "\n",
+ EncodeLevel: formatLevel,
+ }
+
+ encoder := zapcore.NewConsoleEncoder(encoderConfig)
+ stdoutSink := zapcore.AddSync(os.Stdout)
+ stderrSink := zapcore.AddSync(os.Stderr)
+
+ core := zapcore.NewTee(
+ zapcore.NewCore(encoder, stdoutSink, zap.LevelEnablerFunc(func(level zapcore.Level) bool {
+ return level < zapcore.WarnLevel && atomicLevel.Enabled(level)
+ })),
+ zapcore.NewCore(encoder, stderrSink, zap.LevelEnablerFunc(func(level zapcore.Level) bool {
+ return level >= zapcore.WarnLevel && atomicLevel.Enabled(level)
+ })),
+ )
+
+ instance = zap.New(core, zap.AddCaller())
+}
+
+func SetDebug(enabled bool) {
+ if enabled {
+ atomicLevel.SetLevel(zapcore.DebugLevel)
+ } else {
+ atomicLevel.SetLevel(zapcore.InfoLevel)
+ }
+}
+
+func Debugf(prefix string, format string, arguments ...any) {
+ emit(LevelDebug, zapcore.DebugLevel, prefix, fmt.Sprintf(format, arguments...))
+}
+
+func Infof(prefix string, format string, arguments ...any) {
+ emit(LevelInfo, zapcore.InfoLevel, prefix, fmt.Sprintf(format, arguments...))
+}
+
+func Successf(prefix string, format string, arguments ...any) {
+ emit(LevelSuccess, zapcore.InfoLevel, prefix, fmt.Sprintf(format, arguments...))
+}
+
+func Warnf(prefix string, format string, arguments ...any) {
+ emit(LevelWarn, zapcore.WarnLevel, prefix, fmt.Sprintf(format, arguments...))
+}
+
+func Errorf(prefix string, format string, arguments ...any) {
+ emit(LevelError, zapcore.ErrorLevel, prefix, fmt.Sprintf(format, arguments...))
+}
+
+func Fatalf(prefix string, format string, arguments ...any) {
+ emit(LevelError, zapcore.ErrorLevel, prefix, fmt.Sprintf(format, arguments...))
+ os.Exit(1)
+}
+
+func emit(levelLabel LogLevel, zapLevel zapcore.Level, prefix string, message any) {
+ if instance == nil {
+ panic(NotInitialized)
+ }
+
+ instance.Log(zapLevel, buildFullMessage(levelLabel, prefix, message))
+}