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

Wrap pino's Logger in a LoggerService class #2957

Open
Tracked by #2710
victor-yanev opened this issue Sep 10, 2024 · 0 comments
Open
Tracked by #2710

Wrap pino's Logger in a LoggerService class #2957

victor-yanev opened this issue Sep 10, 2024 · 0 comments
Labels
Technical Debt Issue which resolves technical debt
Milestone

Comments

@victor-yanev
Copy link
Contributor

victor-yanev commented Sep 10, 2024

Problem

Currently we are passing around method arguments such as requestIdPrefix, mode, methodName, etc., which are used only for logging and are cluttering the whole code base with unneeded arguments in interfaces.

Solution

Add a new facade class which wraps pino's Logger, we can call it LoggerService (similar to the one in hedera-local-node), which will wrap all methods of Logger (trace, debug, info,warn,error). It will also contain the state of the current requests that are being made to the relay. E.g.:

class LoggerService {

  private _requestId: string;
  // other data which is used only for logs

  set requestId(requestId: string): void {
    this._requestId = requestId;
  }

  get requestIdPrefix(): string {
    if (!this._requestId) {
      return '';
    } 
    return `[Request ID: ${this._requestId}]`;
  }

  constructor(private readonly logger: Logger) {}

  info (msg: string, ...args: any[]): void {
    const formattedMsg = msg ? `${this.requestIdPrefix} ${msg}` : msg;
    this.logger.info(formattedMsg, args);
  }

  // rest of the methods...
}

These methods will format the message with additional context from the stored state it holds inside. In this way, we can remove any redundant arguments from all classes in the relay, and only update the state of the Logger facade class, in a middleware of the KoaApp:

app.getKoaApp().use((ctx, next) => {
 logger.requestId = ctx.state.reqId;
 return next();
});
@victor-yanev victor-yanev added the Technical Debt Issue which resolves technical debt label Sep 10, 2024
@ebadiere ebadiere added this to the 0.56.0 milestone Sep 16, 2024
@quiet-node quiet-node modified the milestones: 0.56.0, 0.57.0 Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Technical Debt Issue which resolves technical debt
Projects
Status: Backlog
Development

No branches or pull requests

3 participants