use LUNA_LOG to set loglevel

master
Ondřej Hruška 1 day ago
parent b998ddc2da
commit c2857f52e8
  1. 2
      docs/README.md
  2. 14
      docs/log.md
  3. 2
      lua/demo/test.lua
  4. 2
      lua/demo/weather.lua
  5. 8
      lua/stubs/log.lua
  6. 7
      src/main.rs

@ -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. |

@ -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.

@ -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")

@ -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

@ -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

@ -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

Loading…
Cancel
Save