0.1.6 • Published 8 months ago

logs-datalake v0.1.6

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

CustomLogger

CustomLogger is a lightweight logging library for Node.js that allows you to incrementally add log information and generate custom log outputs. It provides a fluent API for building log data and supports various log components such as configuration values, feature flags, request and response data, third-party service interactions, user data, and custom information.

Installation

Install the library using npm:

npm install @dtvgo-be/pkg-dtvgo-log

Usage

Importing the Library

To use CustomLogger, import it into your Node.js application:

const CustomLogger = require('@dtvgo-be/pkg-dtvgo-log');

Creating an Instance

Create a new instance of CustomLogger:

const logger = new CustomLogger();

Using custom fields to remove sensitive information

const logger = new CustomLogger(['documentID', 'cpf', 'tokenX']);

This way you could remove sensitive information no mapped by default on lib in all functions that using logs, configuration, request and responses

Adding Log Data

Adding Configuration Values

Use the addConfig method to add configuration values to the log data:

logger.addConfig({
  environment: 'production',
  serverPort: 8080,
});

or

logger.addConfig({ environment: 'production' });
logger.addConfig({ serverPort: 8080 });

The resulting config data object will contain all the expected properties:

{
  "environment": "production",
  "serverPort": 8080
}

Adding Feature Flags

Add feature flags to the log data using the addFeatureFlags method:

logger.addFeatureFlags({
  enableFeatureA: true,
  enableFeatureB: false,
});

or

logger.addFeatureFlags({ enableFeatureA: true });
logger.addFeatureFlags({ enableFeatureB: false });

The resulting Feature Flags data object will contain all the expected properties:

{
  "enableFeatureA": true,
  "enableFeatureB": false
}

Adding Request and Response Data

Include request and response data using the addRequest and addResponse methods:

logger.addRequest({
  method: 'GET',
  path: '/api/users',
});

logger.addResponse({
  statusCode: 200,
  body: { message: 'Success' },
});

Adding Third-Party Service Interactions

For third-party service interactions, use the addThirdPartyRequest and addThirdPartyResponse methods:

logger.addThirdPartyRequest('analytics', {
  method: 'POST',
  url: 'https://api.analytics.com/event',
  data: { eventName: 'Login', userId: '123' },
});

logger.addThirdPartyResponse('analytics', {
  statusCode: 200,
  body: { success: true },
});

Adding User Data

To include user data, use the addUser method:

logger.addUser({ userId: 123 });
logger.addUser({ deviceType: 'mobile', deviceVersion: '1.0' });
logger.addUser({ userToken: 'abc123' });

or

logger.addUser({
  userId: 123,
  deviceType: 'mobile',
  deviceVersion: '1.0',
  userToken: 'abc123'
});

The resulting user data object will contain all the expected properties:

{
  "userId": 123,
  "deviceType": "mobile",
  "deviceVersion": "1.0",
  "userToken": "abc123"
}

Adding Custom Information

Include custom information using the addCustomInfo method:

logger.addCustomInfo({
  additionalInfo: 'Lorem ipsum dolor sit amet',
  timestamp: new Date(),
});

Adding service error

Include custom information using the addCustomInfo method:

logger.addInternalError(new Error('Internal server error'));

Finishing and Logging

To finish building the log data and generate the log output, call the finish method:

logger.finish();

The log output will be displayed in the console as a formatted JSON string.

Complete Example

Here's a complete example demonstrating how to use CustomLogger with different log components:

const CustomLogger = require('custom-logger');

const logger = new CustomLogger();

logger
  .addConfig({ environment: 'production', serverPort: 8080 })
  .addFeatureFlags({ enableFeatureA: true, enableFeatureB: false })
  .addRequest({ method: 'GET', path: '/api/users' })
  .addResponse({ statusCode: 200, body: { message: 'Success' } })
  .addThirdPartyRequest('analytics', {
    method: 'POST',
    url: 'https://api.analytics.com/event',
    data: { eventName: 'Login', userId: '123' },
  })
  .addThirdPartyResponse('analytics', {
    statusCode: 200,
    body: { success: true },
  })
  .addUser({
    userId: '123',
    userIp: '192.168.0.1',
    deviceType: 'mobile',
    deviceVersion: '1.0.0',
    userToken: 'abc123',
  })
  .addCustomInfo({
    additionalInfo: 'Lorem ipsum dolor sit amet',
    timestamp: new Date(),
  })
  .finish();

The above code will output the formatted log data in the console.

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.1.6

8 months ago

0.1.5

8 months ago

0.1.4

8 months ago