https://github.com/themost-framework/json-logger
Json Logger
https://github.com/themost-framework/json-logger
Last synced: 4 months ago
JSON representation
Json Logger
- Host: GitHub
- URL: https://github.com/themost-framework/json-logger
- Owner: themost-framework
- License: bsd-3-clause
- Created: 2024-02-26T07:37:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-27T07:11:01.000Z (over 1 year ago)
- Last Synced: 2024-04-25T10:23:03.813Z (about 1 year ago)
- Language: JavaScript
- Size: 109 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @themost/json-logger
[](https://nodei.co/npm/@themost/json-logger/)
[](https://badge.fury.io/js/%40themost%2Fjson-logger)
A simple JSON logger for Node.js applications.
## Installation
```bash
npm i @themost/json-logger
```## Usage
```javascript
const { JsonLogger } = require('@themost/json-logger');
const logger = new JsonLogger();
logger.info('Hello, world!');
```Use `JsonLogger` class to create a new instance of JSON logger.
```javascript
const { JsonLogger } = require('@themost/json-logger');
const logger = new JsonLogger({
level: 'info',
dateFormat: 'DD/MMM/YYYY:HH:mm:ss Z',
format: 'json',
stdout: process.stdout,
stderr: process.stderr
});
```The `JsonLogger` class accepts an options object with the following properties:
- `level`: The minimum log level to log. The default value is `info`. Available log levels are `info`, `warn`, `error`, `debug` and `verbose`.
- `format`: The log format. The default value is `json`. Available log formats are `json` and `raw`.
- `stdout`: The standard output stream. The default value is `process.stdout`.
- `stderr`: The standard error stream. The default value is `process.stderr`.
- `dateFormat`: The date format to use. The default value is `DD/MMM/YYYY:HH:mm:ss Z`. The date format is based on the [date-and-time](https://github.com/knowledgecode/date-and-time) library.If the `format` property is set to `json`, the log message will be formatted as a JSON object. Otherwise, the log message will be formatted as a raw string.
If you try to instantiate a `JsonLogger` without log level, the default log level will be `info` or `debug` based on the `NODE_ENV` environment variable
when it is set to `test` or `development` respectively.## Methods
The `JsonLogger` provides the following methods:
- `info(message: string, ...args: any[])`: Logs an informational message.
- `warn(message: string, ...args: any[])`: Logs a warning message.
- `error(message: string, ...args: any[])`: Logs an error message.
- `debug(message: string, ...args: any[])`: Logs a debug message.
- `verbose(message: string, ...args: any[])`: Logs a verbose message.
- `log(message: string, ...args: any[])`: A shorthand for `info` method.