|
1 | 1 | # Logging
|
2 | 2 |
|
3 |
| -For logging Nimbus uses the [`@std/log`](https://jsr.io/@std/log) library. |
| 3 | +Nimbus provides a very simple logger that enables you to log messages for different severity levels to the console. |
4 | 4 |
|
5 |
| -## Default setup |
| 5 | +It is basically a wrapper around the `console` object and the `console.debug()`, `console.info()`, `console.warn()`, `console.error()` and `console.critical()` methods. |
6 | 6 |
|
7 |
| -Nimbus provides a simple function to setup the logger. You can pass in the log level and the format you want to use. |
| 7 | +It helps to have consistent logs with important meta information (timestamp, log level,category, error stack traces, etc) across your application. |
8 | 8 |
|
9 |
| -The `pretty` format is recommended for development environments only. In production you should use the `json` format. |
| 9 | +No other transports or sinks are supported. As we want to keep the core as lightweight as possible and encourage the use of tools like [OpenTelemetry](https://opentelemetry.io/) to transport logs for monitoring and tracing. |
10 | 10 |
|
11 |
| -::: code-group |
| 11 | +As [Deno supports OpenTelemetry](https://docs.deno.com/runtime/fundamentals/open_telemetry/) out of the box, you can easily transport logs to any other monitoring system without the need to change the code of the application. |
12 | 12 |
|
13 |
| -```typescript [main.ts] |
14 |
| -import { setupLog } from "@nimbus/core"; |
| 13 | +## Log Levels |
15 | 14 |
|
16 |
| -setupLog({ |
17 |
| - logLevel: process.env.LOG_LEVEL, |
18 |
| - format: process.env.NODE_ENV === "development" ? "pretty" : "json", |
19 |
| -}); |
20 |
| -``` |
| 15 | +Nimbus supports the following log levels for logging messages. |
21 | 16 |
|
22 |
| -```typescript [logExample.ts] |
23 |
| -import * as log from "@std/log"; |
| 17 | +- `debug` - Outputs a `console.debug()` |
| 18 | +- `info` - Outputs a `console.info()` |
| 19 | +- `warn` - Outputs a `console.warn()` |
| 20 | +- `error` - Outputs a `console.error()` |
| 21 | +- `critical` - Outputs a `console.error()` |
24 | 22 |
|
25 |
| -log.info({ msg: "Hello World!" }); |
| 23 | +Also `silent` can be used in the setup to completely disable log output. |
26 | 24 |
|
27 |
| -log.warn({ msg: "Ding Dong!" }); |
| 25 | +## Setup |
28 | 26 |
|
29 |
| -log.error({ msg: "Ohh no!", error: new Error("Something went wrong!") }); |
| 27 | +Nimbus provides a simple function to setup the logger. You can pass in the log level and the formatter you want to use. |
30 | 28 |
|
31 |
| -log.critical({ |
32 |
| - msg: "It is over, run!", |
33 |
| - error: new Error("Something is burning!"), |
| 29 | +The `prettyLogFormatter` is recommended for development environments only. In production you should use the `jsonLogFormatter`. |
| 30 | + |
| 31 | +For the pretty formatter the `useConsoleColors` option can be used to enable colors in the console output. |
| 32 | + |
| 33 | +::: code-group |
| 34 | + |
| 35 | +```typescript [main.ts] |
| 36 | +import { |
| 37 | + jsonLogFormatter, |
| 38 | + parseLogLevel, |
| 39 | + prettyLogFormatter, |
| 40 | + setupLogger, |
| 41 | +} from "@nimbus/core"; |
| 42 | + |
| 43 | +setupLogger({ |
| 44 | + logLevel: parseLogLevel(process.env.LOG_LEVEL), |
| 45 | + formatter: |
| 46 | + process.env.NODE_ENV === "development" |
| 47 | + ? prettyLogFormatter |
| 48 | + : jsonLogFormatter, |
| 49 | + useConsoleColors: process.env.NODE_ENV === "development", |
34 | 50 | });
|
35 | 51 | ```
|
36 | 52 |
|
37 | 53 | :::
|
38 | 54 |
|
39 |
| -## Advanced Setup |
| 55 | +## Usage |
| 56 | + |
| 57 | +The logger can be accessed via the `getLogger` function. |
| 58 | +The logger is a singleton and will return the same instance every time it is called. |
40 | 59 |
|
41 |
| -In case you want to configure the logger in another way you can use the `setup` function from the `@std/log` library. And add `Nimbus` to the loggers object. |
| 60 | +To create a new log you can use the `info`, `warn`, `error` or `critical` methods depending on the severity of the message. |
42 | 61 |
|
43 |
| -View the [@std/log docs](https://jsr.io/@std/log) for all configuration options. |
| 62 | +The log input is an object that can contain the following properties: |
| 63 | + |
| 64 | +- `message` - The message to log. |
| 65 | +- `correlationId` - An optional correlation ID to keep track of commands, queries, and events that are related to each other. |
| 66 | +- `category` - An optional category of the log, useful for grouping logs together. |
| 67 | +- `data` - Optional additional data to log, can be an object with any properties. |
| 68 | +- `error` - Optional error object to log. |
| 69 | + |
| 70 | +The error object is specified as a dedicated property and not as part of the `data` object to make sure all error properties and the stack trace are preserved and logged correctly. |
44 | 71 |
|
45 | 72 | ::: code-group
|
46 | 73 |
|
47 |
| -```typescript [main.ts] |
48 |
| -import * as log from "@std/log"; |
| 74 | +```typescript [logExample.ts] |
| 75 | +import { getLogger } from "@nimbus/core"; |
49 | 76 |
|
50 |
| -log.setup({ |
51 |
| - handlers: { |
52 |
| - default: new log.ConsoleHandler("INFO", { |
53 |
| - formatter: log.formatters.jsonFormatter, |
54 |
| - }), |
55 |
| - }, |
| 77 | +const logger = getLogger(); |
| 78 | + |
| 79 | +logger.debug({ |
| 80 | + message: "Hello World!", |
| 81 | + correlationId: "1234567890", |
| 82 | + data: { foo: "bar" }, |
| 83 | +}); |
| 84 | + |
| 85 | +logger.info({ message: "Hello World!" }); |
| 86 | + |
| 87 | +logger.warn({ |
| 88 | + category: "MyCategory", |
| 89 | + message: "Ding Dong!", |
| 90 | +}); |
| 91 | + |
| 92 | +logger.error({ |
| 93 | + message: "Ohh no!", |
| 94 | + error: new Error("Something went wrong!"), |
| 95 | +}); |
56 | 96 |
|
57 |
| - loggers: { |
58 |
| - Nimbus: { |
59 |
| - level: "INFO", |
60 |
| - handlers: ["default"], |
61 |
| - }, |
| 97 | +logger.critical({ |
| 98 | + category: "MyCategory", |
| 99 | + message: "It is over, run!", |
| 100 | + error: new Error("Something is burning!"), |
| 101 | + data: { |
| 102 | + accountId: "1234567890", |
| 103 | + foo: "bar", |
62 | 104 | },
|
63 | 105 | });
|
64 | 106 | ```
|
65 | 107 |
|
66 | 108 | :::
|
| 109 | + |
| 110 | +## Nimbus Logs |
| 111 | + |
| 112 | +As the various Nimbus features have implemented log statements as well it uses the same logger provided by the `getLogger()` function. |
| 113 | + |
| 114 | +Therefore all log statements from Nimbus will respect the log level and formatter you have configured for the application. |
| 115 | + |
| 116 | +In case you do not configure the logger in your application the Nimbus logs will use the default settings. |
| 117 | + |
| 118 | +## Default Settings |
| 119 | + |
| 120 | +```typescript |
| 121 | +const defaultSettings = { |
| 122 | + logLevel: "silent", |
| 123 | + formatter: jsonLogFormatter, |
| 124 | + useConsoleColors: false, |
| 125 | +}; |
| 126 | +``` |
0 commit comments