Commitfa7ab247Recorded21 Mar 2026Repositorysigil-log

Add log-configure-from-args! helper for CLI log setup

Message

Parses --log <path> and --log-level <level> from command-line args and configures the logger. Apps just call (log-configure-from-args!) in their main before starting the server.

Changed
 src/sigil/log.sgl | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)
Diff
src/sigil/log.sglmodified
@@ -46,7 +46,8 @@
46
47
;; Utilities
48
log-level-active?
49
current-log-level)
+49
current-log-level
+50
log-configure-from-args!)
51
52
(begin
53
@@ -367,4 +368,40 @@
368
(syntax-rules ()
369
((_ msg field ...)
370
(when (log-level-active? 5)
370
(log-fatal msg field ...)))))))
+371
(log-fatal msg field ...)))))
+372
+373
;; ============================================================
+374
;; CLI Helper
+375
;; ============================================================
+376
+377
;;; Configure logging from command-line arguments.
+378
;;;
+379
;;; Scans the process arguments for `--log <path>` and
+380
;;; `--log-level <level>`. When `--log` is found, configures
+381
;;; the logger to write to that file. When `--log-level` is found,
+382
;;; sets the level (trace, debug, info, warn, error, fatal).
+383
;;; If `--log-level` is given without `--log`, logs to stderr.
+384
;;;
+385
;;; Does nothing if neither flag is present.
+386
;;;
+387
;;; ```scheme
+388
;;; ;; In your app's main, before starting the server:
+389
;;; (log-configure-from-args!)
+390
;;; ```
+391
(define (log-configure-from-args!)
+392
(: -> void?)
+393
(let loop ((rest (cdr (command-line))) (log-file #f) (log-level #f))
+394
(cond
+395
((null? rest)
+396
(when (or log-file log-level)
+397
(let ((opts '()))
+398
(when log-level
+399
(set! opts (cons level: (cons log-level opts))))
+400
(when log-file
+401
(set! opts (cons target: (cons log-file opts))))
+402
(apply log-configure! opts))))
+403
((and (equal? (car rest) "--log") (pair? (cdr rest)))
+404
(loop (cddr rest) (cadr rest) log-level))
+405
((and (equal? (car rest) "--log-level") (pair? (cdr rest)))
+406
(loop (cddr rest) log-file (string->symbol (cadr rest))))
+407
(else (loop (cdr rest) log-file log-level)))))))