|
| 1 | +# logger-node |
| 2 | + |
| 3 | +A simple and fast JSON logging library for Node.js services |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```javascript |
| 8 | +import { Logger } from "@cuckoointernet/logger-node"; |
| 9 | + |
| 10 | +const logger = new Logger("my-package"); |
| 11 | +``` |
| 12 | + |
| 13 | +### Log simple messages at different levels |
| 14 | + |
| 15 | +```javascript |
| 16 | +logger.debug("Hello Cuckoo!"); |
| 17 | +logger.info("Hello Cuckoo!"); |
| 18 | +logger.warn("Hello Cuckoo!"); |
| 19 | +logger.error("Hello Cuckoo!"); |
| 20 | +logger.fatal("Hello Cuckoo!"); |
| 21 | +``` |
| 22 | + |
| 23 | +### Log additional data via the second argument |
| 24 | + |
| 25 | +```javascript |
| 26 | +// Data in object supplied is automatically merged into the log record |
| 27 | +logger.info("Hello Cuckoo!", { colour: "yellow" }); |
| 28 | +``` |
| 29 | + |
| 30 | +#### Serialisers |
| 31 | + |
| 32 | +Data provided to the second argument undergoes additional processing if they match certain keys. For instance, if you pass an object with an `error` key it will be run through a serialiser that is able to process stack information in a better way. The standard serialisers are: |
| 33 | + |
| 34 | +| Field | Description | |
| 35 | +| ------- | ------------------------------------------------------------------------------------------------------------------------------ | |
| 36 | +| `error` | Used for serialising JavaScript error objects, including traversing an error's cause chain for error objects with a `.cause()` | |
| 37 | +| `err` | Same as `error` (deprecated) | |
| 38 | +| `req` | Common fields from a node.js HTTP request object | |
| 39 | +| `res` | Common fields from a node.js HTTP response object | |
| 40 | + |
| 41 | +### Log JavaScript `Error` objects |
| 42 | + |
| 43 | +```javascript |
| 44 | +// Alternatively you can supply an instance of Error to log its exception details via the second argument |
| 45 | +logger.warn("Sad Cuckoo...", new Error("Wings were clipped!")); |
| 46 | + |
| 47 | +// To log an Error *and* other data at the same time, use the 'error' field name |
| 48 | +logger.error("Sad Cuckoo...", { |
| 49 | + error: new Error("Wings were clipped!"), |
| 50 | + colour: "yellow", |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +### Return a JavaScript `Error` after logging |
| 55 | + |
| 56 | +When logging at levels `error` and `fatal` you can return a JavaScript `Error` that has the same message as the log record and then `throw`: |
| 57 | + |
| 58 | +```javascript |
| 59 | +// The message of the error thrown will be "Mission failed" |
| 60 | +throw logger.error("Mission failed").returnError(); |
| 61 | + |
| 62 | +// You can log additional data via the second argument as per usual |
| 63 | +throw logger |
| 64 | + .fatal("Mission failed", { reason: "Ran out of fuel..." }) |
| 65 | + .returnError(); |
| 66 | +``` |
| 67 | + |
| 68 | +### Child Loggers |
| 69 | + |
| 70 | +A child logger can be created from an existing one to specialize a logger for a sub-component of your application, i.e. to create a new logger with additional bound fields that will be included in its log records. |
| 71 | + |
| 72 | +```javascript |
| 73 | +const parentLogger = new Logger("parent", "debug"); |
| 74 | + |
| 75 | +// The child logger inherits the same name and log level as the parent |
| 76 | +const childLogger = parentLogger.createChildLogger({ |
| 77 | + subPackage: "child", |
| 78 | + anotherChildField: "whatever-you-want", |
| 79 | +}); |
| 80 | + |
| 81 | +// All log records will contain the two additional fields setup at initialisation, ie: subPackage & anotherChildField |
| 82 | +childLogger.info("Hello from child"); |
| 83 | +``` |
| 84 | + |
| 85 | +## Log Levels |
| 86 | + |
| 87 | +Setting a logger instance to a particular level results in only log records of that level and above being written. You can configure it via the options below: |
| 88 | + |
| 89 | +1. If not specified the logger defaults to `info` level: |
| 90 | + |
| 91 | +```javascript |
| 92 | +const logger = new Logger("my-package"); |
| 93 | +``` |
| 94 | + |
| 95 | +2. Set via `logLevel` constructor parameter: |
| 96 | + |
| 97 | +```javascript |
| 98 | +const logger = new Logger("my-package", "debug"); |
| 99 | +``` |
| 100 | + |
| 101 | +3. Set via `LOG_LEVEL` environment variable: |
| 102 | + |
| 103 | +```javascript |
| 104 | +// process.env.LOG_LEVEL = "debug" |
| 105 | +const logger = new Logger("my-package"); |
| 106 | +``` |
| 107 | + |
| 108 | +The available log levels and best practices guidance on when to use them are as follows: |
| 109 | + |
| 110 | +- `fatal` (60): The service/app is going to stop or become unusable now |
| 111 | +- `error` (50): Fatal for a particular request, but the service/app continues servicing other requests |
| 112 | +- `warn` (40): A note on something that should probably be looked at |
| 113 | +- `info` (30): Detail on regular operation |
| 114 | +- `debug` (20): Anything else, i.e. too verbose to be included in "info" level |
| 115 | + |
| 116 | +If you want to prevent the logger from printing any messages you can set the log level to `silent`. This is sometimes useful, for example when running tests to reduce noise in the terminal. |
| 117 | + |
| 118 | +## Log Records |
| 119 | + |
| 120 | +The structure of log records is outlined below: |
| 121 | + |
| 122 | +``` |
| 123 | +{ |
| 124 | + // User supplied data |
| 125 | + name: "my-package", |
| 126 | + msg: "Hello Cuckoo!", |
| 127 | +
|
| 128 | + ...any additional data supplied via second argument to logger methods (see examples above) |
| 129 | +
|
| 130 | + // Record metadata (added automatically) |
| 131 | + logLevel: "info", |
| 132 | + level: 30, |
| 133 | + time: "2022-02-03T19:02:57.534Z", |
| 134 | + hostname: "banana.local", |
| 135 | + pid: 123, |
| 136 | +
|
| 137 | + // AWS metadata (added automatically if applicable) |
| 138 | + @requestId: <id> |
| 139 | +} |
| 140 | +``` |
0 commit comments