package logger import ( "bytes" "fmt" "io" "os" "sync" "time" ) var ( mtx sync.Mutex output io.Writer = os.Stderr ) type Logger struct { subject string } func NewLogger(subj string) *Logger { return &Logger{ subject: subj, } } func SetWriter(newOut io.Writer) { mtx.Lock() output = newOut mtx.Unlock() } func (logg *Logger) Debugf(message string, args ...any) { logg.printf("debug", message, args...) } func (logg *Logger) Infof(message string, args ...any) { logg.printf("info", message, args...) } func (logg *Logger) Warningf(message string, args ...any) { logg.printf("warning", message, args...) } func (logg *Logger) Errorf(message string, args ...any) { logg.printf("error", message, args...) } func (logg *Logger) printf(level, message string, args ...any) { timestamp := time.Now().Format(time.RFC3339) buffer := bytes.NewBuffer([]byte{}) fmt.Fprintf(buffer, "%s %s.%s: ", timestamp, logg.subject, level) fmt.Fprintf(buffer, message, args...) fmt.Fprintf(buffer, "\n") mtx.Lock() fmt.Fprint(output, buffer.String()) mtx.Unlock() }