Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yakkomajuri/structlog
A lightweight structured logging tool for Node.js without any dependencies.
https://github.com/yakkomajuri/structlog
javascript javascript-library log logger logging nodejs npm npm-package structured-logging
Last synced: about 2 months ago
JSON representation
A lightweight structured logging tool for Node.js without any dependencies.
- Host: GitHub
- URL: https://github.com/yakkomajuri/structlog
- Owner: yakkomajuri
- License: mit
- Created: 2022-03-09T10:25:12.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-15T16:25:57.000Z (almost 3 years ago)
- Last Synced: 2024-10-13T11:45:55.006Z (3 months ago)
- Topics: javascript, javascript-library, log, logger, logging, nodejs, npm, npm-package, structured-logging
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/structlog
- Size: 389 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# structlog
A lightweight structured logging tool for Node.js without any dependencies.
## Example
By default this tool will generate logs like the following:
```
2022-03-09T19:57:48.962Z [error] i'm an error [/structlog/foo.js:5] thread=MAIN logId=017f705b-4200-0002-1101-720541fa117c
2022-03-09T19:57:48.963Z [warn] i'm a warning [/structlog/foo.js:6] thread=MAIN logId=017f705b-4200-0000-e4a6-f1df2ca4f248
```However, you can configure both the log and timestamp formats.
## UsageTo use this logging tool, all you need to do is install it:
```sh
yarn add structlog
```And use it like so:
```js
import { logger } from 'structlog'logger.error('error!')
```Importing `logger` will give you a ready-made instance of `StructuredLogger` with the default config, so you don't need to instantiate a new logger instance each time or pass down references to an instance.
However, you can configure options by instantiating `StructuredLogger` yourself, like so:
```js
import { StructuredLogger } from 'structlog'const logger = new StructuredLogger({ timestampFormat: 'unix' })
```### Options
You can configure the following options by passing them in an object as a parameter when creating `StructuredLogger` :
| Option | Default value | Accepted values | Description |
| :--: | :--: | :--: | :--: |
| `timestampFormat` | `'iso'` | `'iso'` , `'gmt'`, `'unix'`, `'timestring'` ,`'localestring'` | Timestamp format to use. |
| `logFormat` | `'{timestamp} [{type}] {message} [{path}] {tags}'` | Any `string` | Log format to use `{}` to define variables. Accepted variables: `timestamp`, `type`, `message`, `path`, `tags`. |
| `pathFormat` | `'{filePath}:{lineNumber}'` | Any `string` | Format to use for the `path` component of the log. Use `{}` to define variables. Accepted variables: `filePath`, `lineNumber`, `functionName`, `methodName`, `typeName`. |
| `useColors` | `false` | `true`, `false` | Whether logs should be printed with colors according to the type (e.g. error = red). |
| `useThreadTagsExtension` | `false` | `true`, `false` | An extension that automatically adds a `thread` tag to the log specifying what thread the log happened in. |
| `useLogIdExtension` | `false` | `true`, `false` | An extension that automatically adds a `logId` tag to the log, with a unique ID as its value. |
| `pathStackDepth` | `0` | Any `number` | (_Advanced_) Level of depth to determine the path portion of the log from. For example, if you write a wrapper over the logger, set this to 1 so that the path will reflect the code that called your wrapper, rather than the wrapper itself. |