// Package logx is a tiny leveled logger that writes human-readable lines to a // stream and, optionally, tees everything to a log file. Output is deliberately // plain ASCII so it survives RMM script-output capture and legacy consoles. package logx import ( "fmt" "io" "os" "path/filepath" "sync" "time" ) // Level controls verbosity. type Level int const ( // LevelDebug logs every command that is executed. LevelDebug Level = iota // LevelInfo is the default. LevelInfo // LevelWarn hides progress chatter. LevelWarn // LevelError only reports failures. LevelError ) // Logger writes to a console stream and an optional file. type Logger struct { mu sync.Mutex console io.Writer file *os.File level Level } // New returns a Logger writing to w at the given level. func New(w io.Writer, level Level) *Logger { if w == nil { w = io.Discard } return &Logger{console: w, level: level} } // AttachFile starts teeing output to path, creating parent directories. // A failure here is never fatal: logging to a file is a convenience, and an // endpoint with a read-only or missing state directory must still be able to // install software. func (l *Logger) AttachFile(path string) error { if path == "" { return nil } if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { return err } f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) if err != nil { return err } l.mu.Lock() l.file = f l.mu.Unlock() l.writeFileOnly(fmt.Sprintf("\n=== deployr run started %s ===\n", time.Now().Format(time.RFC3339))) return nil } // Close flushes and closes the log file, if any. func (l *Logger) Close() { l.mu.Lock() defer l.mu.Unlock() if l.file != nil { _ = l.file.Close() l.file = nil } } // SetLevel changes verbosity at runtime. func (l *Logger) SetLevel(level Level) { l.mu.Lock() l.level = level l.mu.Unlock() } func (l *Logger) writeFileOnly(s string) { l.mu.Lock() defer l.mu.Unlock() if l.file != nil { _, _ = io.WriteString(l.file, s) } } func (l *Logger) emit(level Level, prefix, format string, args ...any) { msg := fmt.Sprintf(format, args...) line := msg + "\n" if prefix != "" { line = prefix + " " + line } l.mu.Lock() defer l.mu.Unlock() if l.file != nil { _, _ = io.WriteString(l.file, time.Now().Format("15:04:05")+" "+line) } if level >= l.level { _, _ = io.WriteString(l.console, line) } } // Debugf logs command-level detail. func (l *Logger) Debugf(format string, args ...any) { l.emit(LevelDebug, " |", format, args...) } // Infof logs normal progress. func (l *Logger) Infof(format string, args ...any) { l.emit(LevelInfo, "", format, args...) } // Warnf logs a recoverable problem. func (l *Logger) Warnf(format string, args ...any) { l.emit(LevelWarn, "warn:", format, args...) } // Errorf logs a failure. func (l *Logger) Errorf(format string, args ...any) { l.emit(LevelError, "error:", format, args...) } // Resultf logs a per-app outcome line, e.g. "[ok] chrome installed via winget". func (l *Logger) Resultf(status, format string, args ...any) { l.emit(LevelWarn, fmt.Sprintf("[%-4s]", status), format, args...) }