BioDrop icon indicating copy to clipboard operation
BioDrop copied to clipboard

[OTHER] LinkFree Logging Improvements

Open raviraj-github opened this issue 3 years ago • 11 comments

What would you like to share?

During my analysis of the LinkFree issue https://github.com/EddieHubCommunity/LinkFree/issues/7065, I have observed that our current approach to debug logging is not implemented properly. In most cases, console logging has been used, which I believe is not ideal.

To address this issue, I suggest the following solutions in a polite manner:

  1. Update the logger to provide more comprehensive log-level support. It appears that the logger we are currently using already supports this functionality.

  2. Make debug logging configurable, with the default setting being disabled in the Production environment. This will help prevent unnecessary log output in production and ensure a cleaner log file.

  3. Instead of relying solely on console logging, consider utilizing a file appender for logging purposes. This will make log analysis easier and could also be valuable for future analytics purposes.

  4. Create a documentation page specifically dedicated to proper logging practices and make it mandatory for contributors to read before making any contributions. It is important to enforce adherence to these logging standards while addressing open issues.

Based on my knowledge and analysis, it seems that PinoLogger(the one we are using) supports all the mentioned requirements. However, if it does not meet our needs, we can explore other open-source libraries available in the marketplace.

Additional information

No response

raviraj-github avatar Jun 07 '23 09:06 raviraj-github

To reduce notifications, issues are locked until they are https://github.com/EddieHubCommunity/LinkFree/labels/%F0%9F%8F%81%20status%3A%20ready%20for%20dev and to be assigned. You can learn more in our contributing guide https://github.com/EddieHubCommunity/LinkFree/blob/main/CONTRIBUTING.md

github-actions[bot] avatar Jun 07 '23 09:06 github-actions[bot]

Thank you @raviraj-github for the detailed issue 👍

Pino is recommended by NextJS, do you have an alternative in mind?

eddiejaoude avatar Jun 07 '23 12:06 eddiejaoude

I am satisfied with using Pino(it claims that it is 5 times faster than other loggers) to address these issues, and I believe it already covers all of them. I would prefer not to make any changes in this regard.

However, for the purpose of analysis, if someone wishes to explore alternative loggers, the following options can be considered:

  • Winston
  • Pino
  • Bunyan
  • Loglevel
  • Npmlog

raviraj-github avatar Jun 08 '23 07:06 raviraj-github

The issue has been unlocked and is now ready for dev. If you would like to work on this issue, you can comment to have it assigned to you. You can learn more in our contributing guide https://github.com/EddieHubCommunity/LinkFree/blob/main/CONTRIBUTING.md

github-actions[bot] avatar Jun 10 '23 23:06 github-actions[bot]

@eddiejaoude Could you please recommend me files or locations that I might wanna check to solve the issue

SuperGrut avatar Jun 12 '23 07:06 SuperGrut

I have updated the Logger..js file

Logger.js

import { serverEnv } from "@config/schemas/serverSchema";
import * as PinoLogger from "pino";
import { join } from "path";

const logsPath = join(__dirname, "..", "logs", "app.log"); 

const config = {
  transport: {
    target: "pino-pretty",
    options: {
      colorize: true,
    },
  },
  level: serverEnv.NODE_ENV === "development" ? "debug" : "info", 
  enabled: serverEnv.NODE_ENV !== "production", 
  timestamp: true,
  redact: ['req.headers.authorization'],
  pinoFileOptions: {
    appendFile: logsPath,
    prettyPrint: true,
    timestamp: true,
    create:true,
  },
  streams: [
    { stream: process.stdout }, 
    { stream: PinoLogger.destination(logsPath), level: "debug", enabled: serverEnv.NODE_ENV !== "production" }, 
  ],
};

let logger;

logger = PinoLogger(config);

export default logger;

SuperGrut avatar Jun 12 '23 10:06 SuperGrut

The current setup appears acceptable. However, I suggest transitioning to the Winston Library since Pino lacks support for a File Appender. This means that the logging will continuously append to the "app.log" file, whereas we require a solution that can split the file after reaching a certain memory limit and begin writing to a new file.

Additionally, it is essential to generate a log file in the production environment. However, the log level should be set to "info" by default, while still allowing the flexibility to change it to "debug" if necessary.

Let us know, if you want us to assign this issue to you.

Thanks for your support!

raviraj-github avatar Jun 12 '23 13:06 raviraj-github

@raviraj-github Sure I can work on it. Please assign it to me. BTW here is what I have done so far reading the Winston Documentation.

import { serverEnv } from "@config/schemas/serverSchema";
import winston from "winston";
import { join } from "path";

const logsPath = join(__dirname, "..", "logs", "app.log");

const logger = winston.createLogger({
  level: serverEnv.NODE_ENV === "development" ? "debug" : "info",
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  ),
  transports: [
    new winston.transports.Console({
      level: serverEnv.NODE_ENV === "development" ? "debug" : "info",
      format: winston.format.combine(
        winston.format.colorize(),
        winston.format.simple()
      ),
    }),
    new winston.transports.File({
      level: "info", 
      filename: logsPath,
      maxsize: 10485760, 
      maxFiles: 5,
      format: winston.format.combine(
        winston.format.uncolorize(),
        winston.format.simple()
      ),
    }),
  ],
});

export default logger;

SuperGrut avatar Jun 12 '23 15:06 SuperGrut

@akash19coder I have assigned this to you

SaraJaoude avatar Jun 13 '23 12:06 SaraJaoude

@akash19coder : I was trying to test your logger code but it doesn't seem to be working for me. It is throwing an exception for the log file. Have a look at the screenshot.

image

raviraj-github avatar Jun 21 '23 09:06 raviraj-github

@raviraj-github I am not sure but it is working on my machine(I have attached a screenshot below)...I just ran the code on my local machine it was logging the messages in the app.log file properly. Anyway, I am going to work through it properly tomorrow because my end sem exams end tomorrow.

[Screenshot] Screenshot (14)

I have also added the mkdir field as true. makdir

SuperGrut avatar Jun 21 '23 10:06 SuperGrut