| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 3 | |
| 4 | /* |
| 5 | Package logtree implements a tree-shaped logger for debug events. It provides log publishers (ie. Go code) with a |
| Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 6 | glog-like API and io.Writer API, with loggers placed in a hierarchical structure defined by a dot-delimited path |
| 7 | (called a DN, short for Distinguished Name). |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 8 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 9 | tree.MustLeveledFor("foo.bar.baz").Warningf("Houston, we have a problem: %v", err) |
| 10 | fmt.Fprintf(tree.MustRawFor("foo.bar.baz"), "some\nunstructured\ndata\n") |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 11 | |
| 12 | Logs in this context are unstructured, operational and developer-centric human readable text messages presented as lines |
| 13 | of text to consumers, with some attached metadata. Logtree does not deal with 'structured' logs as some parts of the |
| 14 | industry do, and instead defers any machine-readable logs to either be handled by metrics systems like Prometheus or |
| 15 | event sourcing systems like Kafka. |
| 16 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 17 | # Tree Structure |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 18 | |
| Serge Bazanski | 06d65bc | 2020-09-24 10:51:59 +0200 | [diff] [blame] | 19 | As an example, consider an application that produces logs with the following DNs: |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 20 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 21 | listener.http |
| 22 | listener.grpc |
| 23 | svc |
| 24 | svc.cache |
| 25 | svc.cache.gc |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 26 | |
| 27 | This would correspond to a tree as follows: |
| 28 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 29 | .------. |
| 30 | | "" | |
| 31 | | (root) | |
| 32 | '------' |
| 33 | .----------------' '------. |
| 34 | .--------------. .---------------. |
| 35 | | svc | | listener | |
| 36 | '--------------' '---------------' |
| 37 | | .----' '----. |
| 38 | .--------------. .---------------. .---------------. |
| 39 | | svc.cache | | listener.http | | listener.grpc | |
| 40 | '--------------' '---------------' '---------------' |
| 41 | | |
| 42 | .--------------. |
| 43 | | svc.cache.gc | |
| 44 | '--------------' |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 45 | |
| 46 | In this setup, every DN acts as a separate logging target, each with its own retention policy and quota. Logging to a DN |
| 47 | under foo.bar does NOT automatically log to foo - all tree mechanisms are applied on log access by consumers. Loggers |
| 48 | are automatically created on first use, and importantly, can be created at any time, and will automatically be created |
| 49 | if a sub-DN is created that requires a parent DN to exist first. Note, for instance, that a `listener` logging node was |
| 50 | created even though the example application only logged to `listener.http` and `listener.grpc`. |
| 51 | |
| 52 | An implicit root node is always present in the tree, accessed by DN "" (an empty string). All other logger nodes are |
| 53 | children (or transitive children) of the root node. |
| 54 | |
| 55 | Log consumers (application code that reads the log and passes them on to operators, or ships them off for aggregation in |
| 56 | other systems) to select subtrees of logs for readout. In the example tree, a consumer could select to either read all |
| 57 | logs of the entire tree, just a single DN (like svc), or a subtree (like everything under listener, ie. messages emitted |
| 58 | to listener.http and listener.grpc). |
| 59 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 60 | # Leveled Log Producer API |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 61 | |
| 62 | As part of the glog-like logging API available to producers, the following metadata is attached to emitted logs in |
| 63 | addition to the DN of the logger to which the log entry was emitted: |
| 64 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 65 | - timestamp at which the entry was emitted |
| 66 | - a severity level (one of FATAL, ERROR, WARN or INFO) |
| 67 | - a source of the message (file name and line number) |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 68 | |
| 69 | In addition, the logger mechanism supports a variable verbosity level (so-called 'V-logging') that can be set at every |
| 70 | node of the tree. For more information about the producer-facing logging API, see the documentation of the LeveledLogger |
| 71 | interface, which is the main interface exposed to log producers. |
| 72 | |
| Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 73 | If the submitted message contains newlines, it will be split accordingly into a single log entry that contains multiple |
| 74 | string lines. This allows for log producers to submit long, multi-line messages that are guaranteed to be non-interleaved |
| 75 | with other entries, and allows for access API consumers to maintain semantic linking between multiple lines being emitted |
| 76 | as a single atomic entry. |
| 77 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 78 | # Raw Log Producer API |
| Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 79 | |
| 80 | In addition to leveled, glog-like logging, LogTree supports 'raw logging'. This is implemented as an io.Writer that will |
| 81 | split incoming bytes into newline-delimited lines, and log them into that logtree's DN. This mechanism is primarily |
| 82 | intended to support storage of unstructured log data from external processes - for example binaries running with redirected |
| 83 | stdout/stderr. |
| 84 | |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 85 | # Log Access API |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 86 | |
| 87 | The Log Access API is mostly exposed via a single function on the LogTree struct: Read. It allows access to log entries |
| 88 | that have been already buffered inside LogTree and to subscribe to receive future entries over a channel. As outlined |
| 89 | earlier, any access can specify whether it is just interested in a single logger (addressed by DN), or a subtree of |
| 90 | loggers. |
| 91 | |
| 92 | Due to the current implementation of the logtree, subtree accesses of backlogged data is significantly slower than |
| 93 | accessing data of just one DN, or the whole tree (as every subtree backlog access performs a scan on all logged data). |
| 94 | Thus, log consumers should be aware that it is much better to stream and buffer logs specific to some long-standing |
| 95 | logging request on their own, rather than repeatedly perform reads of a subtree backlog. |
| 96 | |
| Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 97 | The data returned from the log access API is a LogEntry, which itself can contain either a raw logging entry, or a leveled |
| 98 | logging entry. Helper functions are available on LogEntry that allow canonical string representations to be returned, for |
| 99 | easy use in consuming tools/interfaces. Alternatively, the consumer can itself access the internal raw/leveled entries and |
| 100 | print them according to their own preferred format. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 101 | */ |
| 102 | package logtree |