mirror of
https://github.com/muun/recovery.git
synced 2025-11-10 05:59:44 -05:00
Release 2.0.0
This commit is contained in:
53
utils/logger.go
Normal file
53
utils/logger.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DebugMode is true when the `DEBUG` environment variable is set to "true".
|
||||
var DebugMode bool = os.Getenv("DEBUG") == "true"
|
||||
|
||||
// Logger provides logging methods that only print when `DebugMode` is true.
|
||||
// This allows callers to log detailed information without displaying it to users during normal
|
||||
// execution.
|
||||
type Logger struct {
|
||||
tag string
|
||||
}
|
||||
|
||||
// NewLogger returns an initialized Logger instance.
|
||||
func NewLogger(tag string) *Logger {
|
||||
return &Logger{tag}
|
||||
}
|
||||
|
||||
// SetTag updates the tag of this Logger.
|
||||
func (l *Logger) SetTag(newTag string) {
|
||||
l.tag = newTag
|
||||
}
|
||||
|
||||
// Printf works like fmt.Printf, but only prints when `DebugMode` is true.
|
||||
func (l *Logger) Printf(format string, v ...interface{}) {
|
||||
if !DebugMode {
|
||||
return
|
||||
}
|
||||
|
||||
message := strings.TrimSpace(fmt.Sprintf(format, v...))
|
||||
|
||||
fmt.Printf("%s %s\n", l.getPrefix(), message)
|
||||
}
|
||||
|
||||
// Errorf works like fmt.Errorf, but prints the error to the console if `DebugMode` is true.
|
||||
func (l *Logger) Errorf(format string, v ...interface{}) error {
|
||||
err := fmt.Errorf(format, v...)
|
||||
|
||||
if DebugMode {
|
||||
fmt.Printf("%s %v\n", l.getPrefix(), err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *Logger) getPrefix() string {
|
||||
return fmt.Sprintf("[%s]", l.tag)
|
||||
}
|
||||
Reference in New Issue
Block a user