Socket
Socket
Sign inDemoInstall

@zenvia/logger

Package Overview
Dependencies
33
Maintainers
6
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.4.3 to 1.5.0

src/index.d.ts

13

package.json
{
"name": "@zenvia/logger",
"version": "1.4.3",
"version": "1.5.0",
"description": "A wrapper for Winston Logging Node.js library that formats the output on STDOUT as Logstash JSON format.",
"license": "MIT",
"main": "./src/lib/logger",
"main": "./src/index",
"private": false,

@@ -36,2 +36,3 @@ "publishConfig": {

"app-root-dir": "^1.0.2",
"cls-rtracer": "^2.6.0",
"winston": "^3.3.3"

@@ -43,8 +44,8 @@ },

"coveralls": "^3.1.0",
"eslint": "^7.6.0",
"eslint": "^7.10.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-plugin-import": "^2.22.0",
"mocha": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"mocha": "^8.1.3",
"nyc": "^15.1.0",
"sinon": "^9.0.2",
"sinon": "^9.2.0",
"sinon-chai": "^3.5.0",

@@ -51,0 +52,0 @@ "std-mocks": "^1.0.1"

# Zenvia Logger for Node.js
A wrapper for [Winston](https://github.com/winstonjs/winston) Logging [Node.js](https://nodejs.org/) library that formats the output on STDOUT as [Logstash](https://www.elastic.co/logstash) JSON format.
A wrapper for [Winston](https://github.com/winstonjs/winston) Logging [Node.js](https://nodejs.org/) library that formats the output on STDOUT as [Logstash](https://www.elastic.co/logstash) JSON format. Since version 1.5.0 it is possible to use log tracking. Zenvia Logger uses the [cls-trace](https://www.npmjs.com/package/cls-rtracer) to perform the tracking

@@ -31,17 +31,62 @@ [![License](https://img.shields.io/github/license/zenvia/zenvia-logger-node.svg)](LICENSE.md)

- **LOGGING_FORMATTER_DISABLED** *(version 1.1.0 and above)*: When `true`, the output logging will not be formatted to JSON. Useful during development time. Default to `false`.
- **LOGGING_FRAMEWORK_MIDDLEWARE** *(version 1.5.0 and above)*: Value that defines which middleware will be used. It is possible to choose between the middlewares: EXPRESS, FASTIFY, HAPI and KOA. If empty, the middleware default is `EXPRESS`.
- **LOGGING_TRACE_HEADER** *(version 1.5.0 and above)*: Value indicating the header name that must be obtained from the traceId value in the request. Default is `X-TraceId`.
## Basic Usage (Express users)
## Basic Usage
```js
// ES6 or Typescript
import express from 'express';
import logger, { traceMiddleware } from '@zenvia/logger';
const app = express();
app.use(traceMiddleware);
logger.info('some message');
```
## Basic Usage (FASTIFY users)
```js
// ES5
const logger = require('@zenvia/logger');
// ES6 or Typescript
import fastify from 'fastify'
import logger, { traceMiddleware } from '@zenvia/logger';
fastify.register(traceMiddleware);
logger.info('some message');
```
## Basic Usage (KOA users)
```js
// ES6 or Typescript
import * as logger from '@zenvia/logger';
import Koa from 'koa';
import logger, { traceMiddleware } from '@zenvia/logger';
const app = new Koa();
app.use(traceMiddleware);
logger.info('some message');
```
## Basic Usage (HAPI users)
```js
// ES6 or Typescript
import Koa from 'koa';
import logger, { traceMiddleware } from '@zenvia/logger';
const init = async () => {
const server = Hapi.server({
port: 3000,
host: 'localhost'
});
await server.register({
plugin: traceMiddleware
});
}
logger.info('some message');
```
Output:

@@ -55,3 +100,4 @@

"message": "some message",
"level": "INFO"
"level": "INFO",
"traceID": "123e4567-e32b-12d3-a432-626614174888"
}

@@ -116,3 +162,3 @@ ```

```js
logger.fatal('Ops!', new Error('Something goes wrong'), { keyA: 'value A', keyB: 'value B' });
logger.fatal('Ops!', { new Error('Something goes wrong'), { keyA: 'value A', keyB: 'value B' } });
```

@@ -134,6 +180,48 @@

### Using trace logs
From version 1.5.0 it is possible to track logs. To do traceability, the cls-rTrace package is used. To use it, just add the middleware in the framework you are using. In this way it is possible to propagate the traceId received in a request to the logs throughout your project. If no traceId is passed in the request, Zenvia Logger generates a random traceId for the request being processed.
**Request example sending traceId:**
```bash
curl 'http://localhost/your-application' \
--header 'X-TraceId: dbcdd40e-10cd-40a7-b912-1b0a17483d67' \
```
Log
```javascript
logger.info('message with traceID');
```
Log Output
```json
{
"@timestamp": "2018-06-05T18:20:42.345Z",
"@version": 1,
"application": "application-name",
"message": "message with traceID",
"level": "INFO",
"traceID": "dbcdd40e-10cd-40a7-b912-1b0a17483d67'"
}
```
**Request example without sending traceId:**
```bash
curl 'http://localhost/your-application'
```
Log
```javascript
logger.info('message without traceID');
```
Log Output
```json
{
"@timestamp": "2018-06-05T18:20:42.345Z",
"@version": 1,
"application": "application-name",
"message": "message with traceID",
"level": "INFO",
"traceID": "912c029c-c38f-49e7-9968-e575c5108178'"
}
```
## License
[MIT](LICENSE.md)

@@ -1,11 +0,9 @@

declare module '@zenvia/logger' {
import { Logger, LeveledLogMethod } from 'winston';
import { Logger, LeveledLogMethod } from 'winston';
interface ZenviaLogger extends Logger {
fatal: LeveledLogMethod;
isFatalEnabled(): boolean;
}
interface ZenviaLogger extends Logger {
fatal: LeveledLogMethod;
isFatalEnabled(): boolean;
}
const logger: ZenviaLogger;
export = logger;
}
declare const logger: ZenviaLogger;
export default logger;

@@ -9,2 +9,3 @@ /* eslint-disable prefer-rest-params */

const appRootDir = require('app-root-dir').get();
const rTrace = require('cls-rtracer');

@@ -31,4 +32,11 @@ const appPackage = require(path.join(appRootDir, 'package'));

stack_trace: stack,
traceId: rTrace.id(),
};
Object.keys(info).forEach((key) => {
if (info[key] instanceof Function) {
delete info[key];
}
});
return info;

@@ -38,2 +46,3 @@ });

const customCombineJson = winston.format.combine(
winston.format.splat(),
customFormatJson(),

@@ -40,0 +49,0 @@ winston.format.json(),

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc