Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | /* |
| 18 | Package logtree implements a tree-shaped logger for debug events. It provides log publishers (ie. Go code) with a |
| 19 | glog-like API, with loggers placed in a hierarchical structure defined by a dot-delimited path (called a DN, short for |
| 20 | Distinguished Name). |
| 21 | |
| 22 | tree.MustLeveledFor("foo.bar.baz").Warningf("Houston, we have a problem: %v", err) |
| 23 | |
| 24 | Logs in this context are unstructured, operational and developer-centric human readable text messages presented as lines |
| 25 | of text to consumers, with some attached metadata. Logtree does not deal with 'structured' logs as some parts of the |
| 26 | industry do, and instead defers any machine-readable logs to either be handled by metrics systems like Prometheus or |
| 27 | event sourcing systems like Kafka. |
| 28 | |
| 29 | Tree Structure |
| 30 | |
Serge Bazanski | 06d65bc | 2020-09-24 10:51:59 +0200 | [diff] [blame^] | 31 | 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] | 32 | |
| 33 | listener.http |
Serge Bazanski | 06d65bc | 2020-09-24 10:51:59 +0200 | [diff] [blame^] | 34 | listener.grpc |
| 35 | svc |
| 36 | svc.cache |
| 37 | svc.cache.gc |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 38 | |
| 39 | This would correspond to a tree as follows: |
| 40 | |
| 41 | .------. |
| 42 | | "" | |
| 43 | | (root) | |
| 44 | '------' |
| 45 | .----------------' '------. |
| 46 | .--------------. .---------------. |
| 47 | | svc | | listener | |
| 48 | '--------------' '---------------' |
| 49 | | .----' '----. |
| 50 | .--------------. .---------------. .---------------. |
| 51 | | svc.cache | | listener.http | | listener.grpc | |
| 52 | '--------------' '---------------' '---------------' |
| 53 | | |
| 54 | .--------------. |
| 55 | | svc.cache.gc | |
| 56 | '--------------' |
| 57 | |
| 58 | In this setup, every DN acts as a separate logging target, each with its own retention policy and quota. Logging to a DN |
| 59 | under foo.bar does NOT automatically log to foo - all tree mechanisms are applied on log access by consumers. Loggers |
| 60 | are automatically created on first use, and importantly, can be created at any time, and will automatically be created |
| 61 | if a sub-DN is created that requires a parent DN to exist first. Note, for instance, that a `listener` logging node was |
| 62 | created even though the example application only logged to `listener.http` and `listener.grpc`. |
| 63 | |
| 64 | An implicit root node is always present in the tree, accessed by DN "" (an empty string). All other logger nodes are |
| 65 | children (or transitive children) of the root node. |
| 66 | |
| 67 | Log consumers (application code that reads the log and passes them on to operators, or ships them off for aggregation in |
| 68 | other systems) to select subtrees of logs for readout. In the example tree, a consumer could select to either read all |
| 69 | logs of the entire tree, just a single DN (like svc), or a subtree (like everything under listener, ie. messages emitted |
| 70 | to listener.http and listener.grpc). |
| 71 | |
| 72 | Log Producer API |
| 73 | |
| 74 | As part of the glog-like logging API available to producers, the following metadata is attached to emitted logs in |
| 75 | addition to the DN of the logger to which the log entry was emitted: |
| 76 | |
| 77 | - timestamp at which the entry was emitted |
| 78 | - a severity level (one of FATAL, ERROR, WARN or INFO) |
| 79 | - a source of the message (file name and line number) |
| 80 | |
| 81 | In addition, the logger mechanism supports a variable verbosity level (so-called 'V-logging') that can be set at every |
| 82 | node of the tree. For more information about the producer-facing logging API, see the documentation of the LeveledLogger |
| 83 | interface, which is the main interface exposed to log producers. |
| 84 | |
| 85 | Log Access API |
| 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 | |
| 97 | */ |
| 98 | package logtree |