mirror of
https://github.com/muun/recovery.git
synced 2025-11-10 22:10:14 -05:00
Release 2.2.3
This commit is contained in:
@@ -2,16 +2,25 @@ package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DebugMode is true when the `DEBUG` environment variable is set to "true".
|
||||
var DebugMode bool = os.Getenv("DEBUG") == "true"
|
||||
var outputStream io.Writer = io.Discard
|
||||
|
||||
// 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.
|
||||
// SetOutputStream set a writer to record all logs (except Tracef).
|
||||
func SetOutputStream(s io.Writer) {
|
||||
outputStream = s
|
||||
}
|
||||
|
||||
// Logger provides logging methods. Logs are written to the stream set by
|
||||
// SetOutputStream. If `DebugMode` is true, we also print to stdout/stderr.
|
||||
// This allows callers to log detailed information without displaying it to
|
||||
// users during normal execution.
|
||||
type Logger struct {
|
||||
tag string
|
||||
}
|
||||
@@ -26,23 +35,39 @@ 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{}) {
|
||||
// Tracef works like fmt.Printf, but only prints when `DebugMode` is true. These logs
|
||||
// are *not* recorded to the output stream.
|
||||
func (l *Logger) Tracef(format string, v ...interface{}) {
|
||||
if !DebugMode {
|
||||
return
|
||||
}
|
||||
|
||||
message := strings.TrimSpace(fmt.Sprintf(format, v...))
|
||||
|
||||
fmt.Printf("%s %s\n", l.getPrefix(), message)
|
||||
fmt.Printf("%s %s %s\n", time.Now().Format(time.RFC3339Nano), l.getPrefix(), message)
|
||||
}
|
||||
|
||||
// Printf works like fmt.Printf, but only prints when `DebugMode` is true. These logs
|
||||
// are recorded to the output stream, so they should not include sensitive information.
|
||||
func (l *Logger) Printf(format string, v ...interface{}) {
|
||||
message := strings.TrimSpace(fmt.Sprintf(format, v...))
|
||||
|
||||
log := fmt.Sprintf("%s %s %s\n", time.Now().Format(time.RFC3339Nano), l.getPrefix(), message)
|
||||
_, _ = outputStream.Write([]byte(log))
|
||||
if DebugMode {
|
||||
print(log)
|
||||
}
|
||||
}
|
||||
|
||||
// Errorf works like fmt.Errorf, but prints the error to the console if `DebugMode` is true.
|
||||
// These logs are recorded to the output stream, so they should not include sensitive information.
|
||||
func (l *Logger) Errorf(format string, v ...interface{}) error {
|
||||
err := fmt.Errorf(format, v...)
|
||||
|
||||
log := fmt.Sprintf("ERROR: %s %s %v\n", time.Now().Format(time.RFC3339Nano), l.getPrefix(), err)
|
||||
_, _ = outputStream.Write([]byte(log))
|
||||
if DebugMode {
|
||||
fmt.Printf("%s %v\n", l.getPrefix(), err)
|
||||
print(log)
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user