From c2857f52e813089bb1c0ea3701d50deb5ab723ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 21 Jul 2026 22:53:17 +0200 Subject: [PATCH] use LUNA_LOG to set loglevel --- docs/README.md | 2 +- docs/log.md | 14 +++++++------- lua/demo/test.lua | 2 +- lua/demo/weather.lua | 2 +- lua/stubs/log.lua | 8 ++++---- src/main.rs | 7 ++++++- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/docs/README.md b/docs/README.md index c195ff0..8a5bde2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@ also works and returns the same table). | [`math`](math.md) | Additions to stock `math`: `round`, `clamp`, `isFinite`, `sign`, `scale`. | | [`table`](table.md) | Additions to stock `table`: map/filter/reduce, merge, deep-copy, aggregates, lookups, read-only proxy. | | [`os`](os.md) | Sandbox additions: `microtime` (sub-second clock) and async `sleep`. | -| [`log`](log.md) | Level-tagged diagnostic logging (`trace`…`error`), gated by `RUST_LOG`. | +| [`log`](log.md) | Level-tagged diagnostic logging (`trace`…`error`), gated by `LUNA_LOG`. | | [`sqlite`](sqlite.md) | Embedded SQLite: connect, query, parameters, transactions. | | [`http`](http.md) | HTTP/HTTPS client: requests, JSON, auth, cookie-jar sessions. | | [`fs`](fs.md) | Whole-file read/write and directory listing, behind a per-script folder permission system. | diff --git a/docs/log.md b/docs/log.md index a37f935..c1d9db1 100644 --- a/docs/log.md +++ b/docs/log.md @@ -43,16 +43,16 @@ log.debug("request opts:", utils.dump(opts)) ## Controlling what is shown Log output goes to **standard error**, and which levels are shown is set by the -`RUST_LOG` environment variable. By default only `WARN` and `ERROR` appear: +`LUNA_LOG` environment variable. By default only `WARN` and `ERROR` appear: ```sh luna script.lua # default: warnings and errors only -RUST_LOG=info luna script.lua # info and above -RUST_LOG=debug luna script.lua # debug and above -RUST_LOG=trace luna script.lua # everything +LUNA_LOG=info luna script.lua # info and above +LUNA_LOG=debug luna script.lua # debug and above +LUNA_LOG=trace luna script.lua # everything ``` -A level shows itself and everything more severe — `RUST_LOG=info` includes +A level shows itself and everything more severe — `LUNA_LOG=info` includes `info`, `warn`, and `error` but not `debug` or `trace`. Messages below the active level are discarded cheaply, so leaving `log.debug` calls in place costs almost nothing when they are not enabled. @@ -71,7 +71,7 @@ turn up or down without editing the script. ```lua local rows = con:query("SELECT * FROM users") -log.info("fetched", #rows, "rows") -- diagnostic; hidden unless RUST_LOG=info +log.info("fetched", #rows, "rows") -- diagnostic; hidden unless LUNA_LOG=info for _, r in ipairs(rows) do print(r.name) -- the actual result end @@ -80,7 +80,7 @@ end ## Notes - **Always loaded.** No `require`; `log` is a global in every script. -- **Quiet by default.** With no `RUST_LOG` set, only `warn`/`error` are emitted, +- **Quiet by default.** With no `LUNA_LOG` set, only `warn`/`error` are emitted, so `info`/`debug`/`trace` are silent until you opt in. - **Arguments are stringified like `print`.** Multiple arguments are joined with tabs; tables print as their address, so use `utils.dump` for contents. diff --git a/lua/demo/test.lua b/lua/demo/test.lua index 1c23292..8ccff49 100644 --- a/lua/demo/test.lua +++ b/lua/demo/test.lua @@ -54,7 +54,7 @@ local t2 = os.microtime() print("microtime ok:", t2 > t1) print("clock >= 0:", os.clock() >= 0) -print("\n=== log (set RUST_LOG=info to see output on stderr) ===") +print("\n=== log (set LUNA_LOG=info to see output on stderr) ===") log.info("hello from log.info") log.warn("hello from log.warn") diff --git a/lua/demo/weather.lua b/lua/demo/weather.lua index 9996c79..e86e1dc 100644 --- a/lua/demo/weather.lua +++ b/lua/demo/weather.lua @@ -9,7 +9,7 @@ -- table map / ifilter / ifilterMap / min / max / mean / concat -- math round / clamp / scale -- utils try, dump --- log diagnostics on stderr (run with RUST_LOG=info to see them) +-- log diagnostics on stderr (run with LUNA_LOG=info to see them) -- -- Data comes from https://wttr.in (no API key). The app prompts for a city, -- shows current conditions and a 3-day forecast, then lets you drill into a diff --git a/lua/stubs/log.lua b/lua/stubs/log.lua index c5086b6..8691325 100644 --- a/lua/stubs/log.lua +++ b/lua/stubs/log.lua @@ -1,20 +1,20 @@ ---@meta -- Type stubs for the native `log` module: level-tagged diagnostics on stderr. --- Visibility is controlled by RUST_LOG (default: warn and error only). +-- Visibility is controlled by LUNA_LOG (default: warn and error only). -- Arguments are stringified like print and joined with tabs; use -- utils.dump(t) to log a table's contents. Reference: docs/log.md log = {} ----Very fine-grained, step-by-step detail. Shown with RUST_LOG=trace. +---Very fine-grained, step-by-step detail. Shown with LUNA_LOG=trace. ---@param ... any function log.trace(...) end ----Developer diagnostics. Shown with RUST_LOG=debug. +---Developer diagnostics. Shown with LUNA_LOG=debug. ---@param ... any function log.debug(...) end ----Normal high-level progress. Shown with RUST_LOG=info. +---Normal high-level progress. Shown with LUNA_LOG=info. ---@param ... any function log.info(...) end diff --git a/src/main.rs b/src/main.rs index 1215aeb..7d8104a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,12 @@ impl Args { #[tokio::main] async fn main() { - env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init(); + // `log` output is governed by $LUNA_LOG (and $LUNA_LOG_STYLE for color), + // not env_logger's default $RUST_LOG. + env_logger::Builder::from_env( + env_logger::Env::new().filter_or("LUNA_LOG", "warn").write_style("LUNA_LOG_STYLE"), + ) + .init(); // A panicking runtime must not strand the user on the alternate screen or // with a hidden cursor; restoring first also puts the panic message on