2.0.2 • Published 6 months ago

logs-service v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Telegram Logger Service

Description

The Telegram Logger is a TypeScript class designed to send application logs to a Telegram bot and optionally to Amazon CloudWatch. This tool is useful for logging important events or errors from your applications and receiving immediate notifications through Telegram. This service connects to the Telegram API to send formatted log messages, providing detailed information about the log type, the service generating it, and the log message. Additionally, if configured correctly, the logs can also be sent to an Amazon CloudWatch log group for storage and analysis.

Installation

npm install logs-service

Creating an Instance of TelegramLogger

To create an instance of TelegramLogger, you need to provide the following parameters:

  • botToken: The Telegram bot token.
  • chatId: The ID of the Telegram chat where logs will be sent.
  • logGroupName (optional): The name of the CloudWatch log group (only if you want to send logs to CloudWatch).
  • logStreamName (optional): The name of the CloudWatch log stream (only if you want to send logs to CloudWatch).

Example:

import { TelegramLogger } from "logs-service";

const logger = new TelegramLogger('YOUR_BOT_TOKEN', 'YOUR_CHAT_ID', 'YOUR_LOG_GROUP_NAME', 'YOUR_LOG_STREAM_NAME');

Sending Logs

To send a log, you can use the sendLog method. This method formats the message and sends it both to Telegram and, optionally, to CloudWatch if the CloudWatch parameters are configured correctly.

Example:

const log = {
  date: 'Date in string format',
  logType: 'INFO',
  service: 'YourServiceName',
  message: 'This is a log message'
};

// Send log to Telegram and to CloudWatch if configured
await logger.sendLog(log, true);  // 'true' activates sending to CloudWatch

Configuring AWS SDK

If you want to send logs to CloudWatch, make sure to configure the AWS SDK with the proper credentials. Here is an example of a basic configuration:

Example:

import { config } from 'aws-sdk';

// Configure AWS credentials (you can use your preferred method)
config.update({
  region: 'us-east-1', // Define your AWS region
  accessKeyId: 'YOUR_AWS_ACCESS_KEY_ID', 
  secretAccessKey: 'YOUR_AWS_SECRET_ACCESS_KEY'
});

Parameters of the TelegramLogger Class

ParameterTypeDescriptionRequired
botTokenstringThe Telegram bot tokenYes
chatIdstringThe Telegram chat ID where the logs will be sentYes
logGroupNamestringThe CloudWatch log group name (optional)No
logStreamNamestringThe CloudWatch log stream name (optional)No

Dependencies

  • pino: ^9.6.0 - For logging and formatting logs.
  • aws-sdk: ^2.1692.0 - For interacting with Amazon CloudWatch.

Methods

sendLog(log: Log, sendMessageLambda: boolean = false): Promise

Sends a log to Telegram and, if configured, to CloudWatch. If sendMessageLambda is set to true, the log will also be sent to CloudWatch.

Parameters:

  • log: The log object to send. It must follow the Log interface.
  • sendMessageLambda (optional): If set to true, logs will also be sent to CloudWatch.

sendToLambda(logGroupName: string, logStreamName: string, message: string): Promise

Sends the log message to CloudWatch if the configuration is enabled.

Full Example

import { TelegramLogger } from './TelegramLogger';
import { config } from 'aws-sdk';

config.update({
  region: 'us-east-1',
  accessKeyId: 'YOUR_AWS_ACCESS_KEY_ID',
  secretAccessKey: 'YOUR_AWS_SECRET_ACCESS_KEY',
});

const logger = new TelegramLogger('YOUR_BOT_TOKEN', 'YOUR_CHAT_ID', 'YOUR_LOG_GROUP_NAME', 'YOUR_LOG_STREAM_NAME');

const log = {
  date: new Date(),
  logType: 'ERROR',
  service: 'MyApp',
  message: 'An error occurred while processing the request.',
};

logger.sendLog(log, true); // Send to Telegram and CloudWatch

License

MIT

2.0.2

6 months ago

2.0.1

6 months ago

2.0.0

6 months ago

1.2.0

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago