|
| 1 | +# Pandoc lua logging |
| 2 | + |
| 3 | +This library provides pandoc-aware functions for dumping and logging lua objects. It can be used standalone but is primarily intended for using within pandoc lua filters. |
| 4 | + |
| 5 | +# Getting started |
| 6 | + |
| 7 | +Put `logging.lua` somewhere where pandoc can access it, e.g., in the same directory as your lua filters. Then try this `simple.lua` filter: |
| 8 | + |
| 9 | +```lua |
| 10 | +local logging = require 'logging' |
| 11 | + |
| 12 | +function Pandoc(pandoc) |
| 13 | + logging.temp('pandoc', pandoc) |
| 14 | +end |
| 15 | +``` |
| 16 | + |
| 17 | +...with this `simple.md` input: |
| 18 | + |
| 19 | +```markdown |
| 20 | +text |
| 21 | +``` |
| 22 | + |
| 23 | +...to get this output on `stderr`: |
| 24 | + |
| 25 | +```text |
| 26 | +(#) pandoc Pandoc { |
| 27 | + blocks: Blocks { |
| 28 | + [1] Para { |
| 29 | + content: Inlines { |
| 30 | + [1] Str text: "text" |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + meta: Meta {} |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +The `logging.temp()` function is intended for temporary debug output (hence its name) and always generates output. You can use `logging.error()`, `logging.warning()`, `logging.info()` etc. to generate output that's conditional on the log level. |
| 39 | + |
| 40 | +The initial log level is derived from the pandoc `--quiet`, `--verbose` and `--trace` command-line options. By default (i.e., if none of these options are specified) the log level is `0` and only errors and warnings will be output. |
| 41 | + |
| 42 | +With this `para.lua` filter: |
| 43 | + |
| 44 | +```lua |
| 45 | +local logging = require 'logging' |
| 46 | + |
| 47 | +function Para(para) |
| 48 | + logging.info('para', para) |
| 49 | +end |
| 50 | +``` |
| 51 | + |
| 52 | +...by default there's no output: |
| 53 | + |
| 54 | +```text |
| 55 | +% pandoc simple.md -L para.lua >/dev/null |
| 56 | +``` |
| 57 | + |
| 58 | +...but `--verbose` sets the log level to `1` (`info`): |
| 59 | + |
| 60 | +```text |
| 61 | +% pandoc simple.md -L para.lua >/dev/null --verbose |
| 62 | +[INFO] Running filter para.lua |
| 63 | +(I) para Para {, content: Inlines {[1] Str text: "text"}} |
| 64 | +[INFO] Completed filter para.lua in 8 ms |
| 65 | +``` |
| 66 | + |
| 67 | +All lua objects can be passed to `logging.info()` etc., and they will be output in a form that should be useful for lua filter development and debugging. The output is intended to be a faithful representation of the [pandoc lua types](https://pandoc.org/lua-filters.html#module-pandoc) and should make it easy to "dig down". For example, you can see that: |
| 68 | + |
| 69 | +* `para` is a [Para](https://pandoc.org/lua-filters.html#type-para) instance |
| 70 | +* `para.content` is an [Inlines](https://pandoc.org/lua-filters.html#type-inlines) instance |
| 71 | +* `para.content[1]` is a [Str](https://pandoc.org/lua-filters.html#type-str) instance |
| 72 | +* `para.content[1].text` is a string |
| 73 | + |
| 74 | +...and you could reference all of these directly in the filter. For example, with this `para2.lua` filter: |
| 75 | + |
| 76 | +```lua |
| 77 | +local logging = require 'logging' |
| 78 | + |
| 79 | +function Para(para) |
| 80 | + logging.info('para', para) |
| 81 | + logging.info('para.content', para.content) |
| 82 | + logging.info('para.content[1]', para.content[1]) |
| 83 | + logging.info('para.content[1].text', para.content[1].text) |
| 84 | +end |
| 85 | +``` |
| 86 | + |
| 87 | +...you get this: |
| 88 | + |
| 89 | +```text |
| 90 | +% pandoc simple.md -L para2.lua >/dev/null --verbose |
| 91 | +[INFO] Running filter para2.lua |
| 92 | +(I) para Para {, content: Inlines {[1] Str text: "text"}} |
| 93 | +(I) para.content Inlines {[1] Str text: "text"} |
| 94 | +(I) para.content[1] Str text: "text" |
| 95 | +(I) para.content[1].text text |
| 96 | +[INFO] Completed filter para2.lua in 8 ms |
| 97 | +``` |
| 98 | + |
| 99 | +# Module contents |
| 100 | + |
| 101 | +## logging.type(value) |
| 102 | + |
| 103 | +Returns whatever [`pandoc.utils.type()`](https://pandoc.org/lua-filters.html#pandoc.utils.type) returns, modified as follows: |
| 104 | + |
| 105 | +* Spaces are replaced with periods, e.g., `pandoc Row` becomes `pandoc.Row` |
| 106 | +* `Inline` and `Block` are replaced with the corresponding `tag` value, e.g. `Emph` or `Table` |
| 107 | + |
| 108 | +## logging.dump(value [, maxlen]) |
| 109 | + |
| 110 | +Returns a pandoc-aware string representation of `value`, which can be an arbitrary lua object. |
| 111 | + |
| 112 | +The returned string is a single line if not longer than `maxlen` (default `70`), and is otherwise multiple lines (with two character indentation). The string is not terminated with a newline. |
| 113 | + |
| 114 | +Map keys are sorted alphabetically in order to ensure that output is repeatable. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +See the *Getting started* section for examples, and note that |
| 119 | + |
| 120 | +## logging.output(...) |
| 121 | + |
| 122 | +Pass each argument to `logging.dump()` and output the results to `stderr`, separated by single spaces and terminated (if necessary) with a newline. |
| 123 | + |
| 124 | +Note: Actually (as a slight optimization) only `table` and `userdata` arguments are passed to `logging.dump()`. Other arguments are passed to the built-in `tostring()` function. |
| 125 | + |
| 126 | +## logging.loglevel |
| 127 | + |
| 128 | +Integer log level, which controls which of `logging.error()`, `logging.warning()`, `logging.info()` will generate output when called. |
| 129 | + |
| 130 | +* `-2` : (or less) suppress all logging (apart from `logging.temp()`) |
| 131 | +* `-1` : output only error messages |
| 132 | +* `0` : output error and warning messages |
| 133 | +* `1` : output error, warning and info messages |
| 134 | +* `2` : output error, warning, info and debug messages |
| 135 | +* `3` : (or more) output error, warning, info, debug and trace messages |
| 136 | + |
| 137 | +The initial log level is `0`, unless the following pandoc command-line options are specified: |
| 138 | + |
| 139 | +* `--trace` : `3` if `--verbose` is also specified; otherwise `2` |
| 140 | +* `--verbose` : `1` |
| 141 | +* `--quiet` : `-1` |
| 142 | + |
| 143 | +## logging.setloglevel(loglevel) |
| 144 | + |
| 145 | +Sets the log level and returns the previous level. |
| 146 | + |
| 147 | +Calling this function is preferable to setting `logging.loglevel` directly. |
| 148 | + |
| 149 | +## logging.error(...) |
| 150 | + |
| 151 | +If the log level is >= `-1`, calls `logging.output()` with `(E)` and the supplied arguments. |
| 152 | + |
| 153 | +## logging.warning(...) |
| 154 | + |
| 155 | +If the log level is >= `0`, calls `logging.output()` with `(W)` and the supplied arguments. |
| 156 | + |
| 157 | +## logging.info(...) |
| 158 | + |
| 159 | +If the log level is >= `1`, calls `logging.output()` with `(I)` and the supplied arguments. |
| 160 | + |
| 161 | +## logging.debug(...) |
| 162 | + |
| 163 | +If the log level is >= `2`, calls `logging.output()` with `(D)` and the supplied arguments. |
| 164 | + |
| 165 | +## logging.trace(...) |
| 166 | + |
| 167 | +If the log level is >= `3`, calls `logging.output()` with `(T)` and the supplied arguments. |
| 168 | + |
| 169 | +## logging.temp(...) |
| 170 | + |
| 171 | +Unconditionally calls `logging.output()` with `(#)` and the supplied arguments. |
0 commit comments