Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Structured Logging #2

Open
MathyouMB opened this issue Jun 17, 2024 · 0 comments
Open

Setup Structured Logging #2

MathyouMB opened this issue Jun 17, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@MathyouMB
Copy link
Member

MathyouMB commented Jun 17, 2024

As a Developer, I should be able to create logs for errors, info, etc with a single line.

Here is some sample code I've written in the past using winston:

import winston from "winston";

const base = winston.createLogger({
  level: "info",
  format: winston.format.printf(({ level, message, label, timestamp }) => {
    return `[LOGGER] ${level}: ${message}`;
  }),
  transports: [
    new winston.transports.File({
      filename: "./logs/error.log",
      level: "error",
    }),
    new winston.transports.File({ filename: "./logs/combined.log" }),
  ],
});

if (process.env.NODE_ENV !== "production") {
  base.add(
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.colorize(),
        winston.format.printf((info) => {
          const { level, message, label, timestamp } = info;
          const colorizer = winston.format.colorize().colorize;
          return `${colorizer("info", `[LOGGER]`)} ${level}: ${message}`;
        }),
      ),
    }),
  );
}

interface log {
  message: string;
  body: string;
}

class Logger {
  private format = (log: log): string => {
    return `{message: "${log.message}", body: ${log.body}}`;
  };
  public info(log: log): void {
    const formattedLog = this.format(log);
    base.info(formattedLog);
  }

  public error(log: log): void {
    const formattedLog = this.format(log);
    base.error(formattedLog);
  }

  public warn(log: log): void {
    const formattedLog = this.format(log);
    base.warn(formattedLog);
  }
}

const logger = new Logger();

export default logger;

Add this code or implement your own logger and update the console.log statements on src/jobs/dailyTaskReminder.ts to use the logger.

Additionally, to any error blocks or try catch statements, add error log statements.

Here is a resource to read about structured logging and it's importance: https://stackify.com/what-is-structured-logging-and-why-developers-need-it/

@MathyouMB MathyouMB changed the title Setup Strucured Loggin Setup Structured Logging Jun 17, 2024
@ryangchung ryangchung added the enhancement New feature or request label Jun 17, 2024
@karimaljundi karimaljundi self-assigned this Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Unassigned - Todo
Development

No branches or pull requests

3 participants