brian-assistant / tests

0 stars 0 forks source link

Implement task #151 - logger #152

Open brian-ai-assistant[bot] opened 10 months ago

brian-ai-assistant[bot] commented 10 months ago

Logger Web App Architecture

To develop the secure logger web app with the ability to save logs to AWS and file, I propose the following architecture:

  1. Front-end: The front-end of the web app will be developed using HTML, CSS, and JavaScript. It will provide a user interface for logging messages and selecting the transport type.

  2. Back-end: The back-end of the web app will be developed using Node.js and Express.js. It will handle the API requests from the front-end and interact with the AWS service and file system.

  3. AWS Integration: We will use the AWS SDK to integrate with AWS CloudWatch Logs service. This will allow us to securely send logs to AWS using credentials and log groups.

  4. File System Integration: For the file transport, we will use the built-in fs module of Node.js to write log messages to a file on the server.

Code Implementation

logger.ts

import * as AWS from 'aws-sdk';
import * as fs from 'fs';

// Configuration for AWS SDK
AWS.config.update({
  region: '<AWS_REGION>',
  accessKeyId: '<AWS_ACCESS_KEY>',
  secretAccessKey: '<AWS_SECRET_ACCESS_KEY>'
});

export class Logger {
  private transport: string;
  private cloudwatch: AWS.CloudWatchLogs;

  constructor(transport = 'console') {
    this.transport = transport;
    this.cloudwatch = new AWS.CloudWatchLogs();
  }

  public error(message: string): void {
    this.log('error', message);
  }

  public debug(message: string): void {
    this.log('debug', message);
  }

  public info(message: string): void {
    this.log('info', message);
  }

  public warn(message: string): void {
    this.log('warn', message);
  }

  private log(level: string, message: string): void {
    const logMessage = `[${level.toUpperCase()}] ${message}`;

    if (this.transport === 'console') {
      console.log(logMessage);
    } else if (this.transport === 'aws') {
      // Send log message to AWS CloudWatch Logs
      const params = {
        logGroupName: '<LOG_GROUP_NAME>',
        logStreamName: '<LOG_STREAM_NAME>',
        logEvents: [{
          message: logMessage,
          timestamp: Date.now()
        }]
      };

      this.cloudwatch.putLogEvents(params, (err, data) => {
        if (err) {
          console.error(err);
        } else {
          console.log(data);
        }
      });
    } else if (this.transport === 'file') {
      // Write log message to a file
      fs.appendFileSync('<LOG_FILE_PATH>', logMessage + '\n');
    }
  }
}

Explanation:

Commit Message: Implemented secure logger web app architecture and code

Files: