Spaces:
Paused
Paused
File size: 4,984 Bytes
9ada4bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# Logger
Environment-agnostic, ESM-friendly logger for simple needs.
## Why does this exist?
I've been using `debug` for quite some time but wanted to migrate my projects to better ESM support. Alas, `debug` doesn't ship as ESM so I went and wrote this little logger just for my needs. You will likely see it printing useful data in Mock Service Worker and beyond.
## Installation
```sh
npm install @open-draft/logger
```
## Usage
This package has the same API for both browser and Node.js and can run in those environments out of the box.
```js
// app.js
import { Logger } from '@open-draft/logger'
const logger = new Logger('parser')
logger.info('starting parsing...')
logger.warning('found legacy document format')
logger.success('parsed 120 documents!')
```
Logging is disabled by default. To enable logging, provide the `DEBUG` environment variable:
```sh
DEBUG=1 node ./app.js
```
> You can also use `true` instead of `1`. You can also use a specific logger's name to enable [logger filtering](#logger-filtering).
## API
- Class: `Logger`
- [`new Logger(name)`](#new-loggername)
- [`logger.debug(message, ...positionals)`](#loggerdebugmessage-positionals)
- [`logger.info(message, ...positionals)`](#loggerinfomessage-positionals)
- [`logger.success(message, ...positionals)`](#loggersuccessmessage-positionals)
- [`logger.warning(message, ...positionals)`](#loggerwarningmessage-positionals)
- [`logger.error(message, ...positionals)`](#loggererrormessage-positionals)
- [`logger.extend(name)`](#loggerextendprefix)
- [`logger.only(callback)`](#loggeronlycallback)
### `new Logger(name)`
- `name` `string` the name of the logger.
Creates a new instance of the logger. Each message printed by the logger will be prefixed with the given `name`. You can have multiple loggers with different names for different areas of your system.
```js
const logger = new Logger('parser')
```
> You can nest loggers via [`logger.extend()`](#loggerextendprefix).
### `logger.debug(message, ...positionals)`
- `message` `string`
- `positionals` `unknown[]`
Prints a debug message.
```js
logger.debug('no duplicates found, skipping...')
```
```
12:34:56:789 [parser] no duplicates found, skipping...
```
### `logger.info(message, ...positionals)`
- `message` `string`
- `positionals` `unknown[]`
Prints an info message.
```js
logger.info('new parse request')
```
```
12:34:56:789 [parser] new parse request
```
### `logger.success(message, ...positionals)`
- `message` `string`
- `positionals` `unknown[]`
Prints a success message.
```js
logger.success('prased 123 documents!')
```
```
12:34:56:789 ✔ [parser] prased 123 documents!
```
### `logger.warning(message, ...positionals)`
- `message` `string`
- `positionals` `unknown[]`
Prints a warning. In Node.js, prints it to `process.stderr`.
```js
logger.warning('found legacy document format')
```
```
12:34:56:789 ⚠ [parser] found legacy document format
```
### `logger.error(message, ...positionals)`
- `message` `string`
- `positionals` `unknown[]`
Prints an error. In Node.js, prints it to `process.stderr`.
```js
logger.error('failed to parse document')
```
```
12:34:56:789 ✖ [parser] failed to parse document
```
### `logger.extend(prefix)`
- `prefix` `string` Additional prefix to append to the logger's name.
Creates a new logger out of the current one.
```js
const logger = new Logger('parser')
function parseRequest(request) {
const requestLogger = logger.extend(`${request.method} ${request.url}`)
requestLogger.info('start parsing...')
}
```
```
12:34:56:789 [parser] [GET https://example.com] start parsing...
```
### `logger.only(callback)`
Executes a given callback only when the logging is activated. Useful for computing additional information for logs.
```js
logger.only(() => {
const documentSize = getSizeBytes(document)
logger.debug(`document size: ${documentSize}`)
})
```
> You can nest `logger.*` methods in the callback to `logger.only()`.
## Log levels
You can specify the log levels to print using the `LOG_LEVEL` environment variable.
There are the following log levels:
- `debug`
- `info`
- `success`
- `warning`
- `error`
> Providing no log level will print all the messages.
Here's an example of how to print only warnings:
```js
// app.js
import { Logger } from '@open-draft/logger'
const logger = new Logger('parser')
logger.info('some info')
logger.warning('some warning')
logger.error('some error')
```
```js
LOG_LEVEL=warning node ./app.js
```
```
12:34:56:789 ⚠ [parser] some warning
```
## Logger filtering
You can only print a specific logger by providing its name as the `DEBUG` environment variable.
```js
// app.js
import { Logger } from '@open-draft/logger'
const appLogger = new Logger('app')
const parserLogger = new Logger('parser')
appLogger.info('starting app...')
parserLogger.info('creating a new parser...')
```
```sh
DEBUG=app node ./app.js
```
```
12:34:56:789 [app] starting app...
```
|