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

Log handler setup function is inconsistent #3538

Closed
ross-weir opened this issue Aug 13, 2023 · 2 comments · Fixed by #3543
Closed

Log handler setup function is inconsistent #3538

ross-weir opened this issue Aug 13, 2023 · 2 comments · Fixed by #3543

Comments

@ross-weir
Copy link

ross-weir commented Aug 13, 2023

Is your feature request related to a problem? Please describe.

When creating logger handlers some of the handler setup functions are sync while others are async.

For example, I have a function that creates a logger for my modules like so:

export function createLogger(
  config: ErgomaticConfig,
  name: string,
): Logger {
  const logHandlers = [];
  const level = config.logLevel;
  const fileHandler = new handlers.FileHandler(level, {
    filename: join(logsDir(), "file.log"),
  });

  fileHandler.setup();

  logHandlers.push(
    new handlers.ConsoleHandler(level, {
      formatter,
    }),
    fileHandler,
  );

  return new Logger(name, level, {
    handlers: logHandlers,
  });
}

That I was using in the constructor of my modules:

class Module {
  constructor() {
    this.logger = createLogger(...)
  }
}

I decided I wanted to move over to RotatingFileHandler for log file rotation. My previous setup no longer works because RotatingFileHandler.setup is async whereas FileHandler.setup is sync, so this requires me to refactor all my classes if I want to use it.

Describe the solution you'd like

Make the API consistent for log handlers, either all async or all sync.

IMO there could have been setup & setupSync like other Deno APIs but this probably can't be changed while preserving backwards compatibility.

Alternatively add a SyncRotatingFileHandler that can be used in sync contexts

@ross-weir
Copy link
Author

ross-weir commented Aug 14, 2023

Looking into this a bit it looks like RotatingFileHandler.setup being async might be an oversight, it appears the log.setup method was made sync a while ago ebeecac

Additionally all the documentation still incorrectly has the setup function as async:

* await log.setup({

@lino-levan
Copy link
Contributor

Seems like a reasonable change. I'll pick it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants