vlog#
Cmdliner terms for ergonomic logging configuration.
Overview#
vlog provides cmdliner terms that configure the OCaml Logs library with a familiar CLI interface inspired by common Unix tools and Rust's RUST_LOG pattern.
Features#
- Verbosity flags:
-q,-v,-vv,-vvv - RUST_LOG-style configuration:
--log=level,src:level,... - JSON output:
--jsonfor structured logging - Protocol tracing:
--trace FILEto capture*.tracingsources - Environment variable:
<APP>_LOG(e.g.,MYAPP_LOG)
Installation#
Install with opam:
$ opam install vlog
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install vlog
Usage#
open Cmdliner
let run () =
Logs.info (fun m -> m "Starting...");
Ok ()
let cmd =
let info = Cmd.info "myapp" in
Cmd.v info Term.(const run $ Vlog.setup "myapp")
let main () = exit (Cmd.eval_result cmd)
Command Line#
$ myapp -q # errors only
$ myapp # warnings (default)
$ myapp -v # info level
$ myapp -vv # debug level
$ myapp -vvv # debug + protocol tracing
$ myapp --log=debug # set level via flag
$ myapp --log=info,http:debug # global info, http at debug
$ myapp --log=warn,tls.tracing:debug # enable specific tracing
$ MYAPP_LOG=debug myapp # set level via env var
$ myapp --json # JSON output
$ myapp --trace protocol.log # write traces to file
Verbosity Levels#
| Flag | Level | *.tracing sources |
|---|---|---|
-q |
Error | Silenced |
| (none) | Warning | Silenced |
-v |
Info | Silenced |
-vv |
Debug | Silenced |
-vvv |
Debug | Enabled |
The -vv flag gives you application-level debug output without the noisy protocol hexdumps. Use -vvv or --trace FILE when you need full protocol tracing.
JSON Output#
With --json, use the optional json_reporter parameter to enable JSON log output:
open Cmdliner
let run () = `Ok ()
let json_reporter ~app:_ ~base:_ () = Logs.nop_reporter
let cmd =
let info = Cmd.info "myapp" in
Cmd.v info
Term.(const run $ Vlog.setup ~json_reporter:(Some json_reporter) "myapp")
Requires the json-logs package.
API#
Vlog.setup- Main cmdliner term combining all flagsVlog.quiet- Term for-q/--quietVlog.verbosity- Term for-v/--verboseVlog.log_term- Term for--logwith env varVlog.json- Term for--jsonVlog.trace_file- Term for--trace FILEVlog.parse_log_spec- Parse RUST_LOG-style specVlog.apply_source_overrides- Apply per-source levelsVlog.configure_tracing_sources- Enable/disable*.tracingsources
Related Work#
- logs.cli - Basic cmdliner integration in the Logs library. Provides
--verbosityand-vflags. vlog extends this with RUST_LOG-style per-source control, JSON output, and protocol tracing. - env_logger (Rust) - The inspiration for the
--logflag syntax and<APP>_LOGenvironment variable pattern. - debug (Node.js) - Popular namespace-based debug logging. Similar concept of per-source control.
Licence#
MIT License. See LICENSE.md for details.