Skip to content
New issue

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

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

Already on GitHub? # to your account

Consider optional onRequest format hook #13

Closed
gitblit opened this issue Mar 26, 2024 · 6 comments · Fixed by #19
Closed

Consider optional onRequest format hook #13

gitblit opened this issue Mar 26, 2024 · 6 comments · Fixed by #19
Labels
feature request New feature or request good first issue Good for newcomers

Comments

@gitblit
Copy link

gitblit commented Mar 26, 2024

With my custom logging, I have an onRequest hook that logs start of request processing. This is handy in the case where Elysia (or a plugin) does something unexpected and the request does not make it all the way through to onResponse. It would be convenient if there was an optional format override for onRequest, maybe by default it does nothing.

This is a hybrid of what I am using and what I'd like to have.

// exports a Logestic plugin instance
export const logger = new Logestic()
  .use(["time", "method", "path", "status", "duration"])
  .format({
    onRequest({ time, method, path }) {
      const grayTime = chalk.gray(`${time!!.toISOString()}`);
      const methodPath = chalk.cyan(`${method} ${path}`);

      return `[${grayTime}] 🟢 ${methodPath}`;
    },    
    onSuccess({ time, method, path, status, duration }) {
      const grayTime = chalk.gray(`${time!!.toISOString()}`);
      const methodPath = chalk.cyan(`${method} ${path}`);
      let statusColor = chalk.white;

      if (200 <= status && status < 300) statusColor = chalk.green;
      if (400 <= status && status < 500) statusColor = chalk.yellow;
      if (500 <= status) statusColor = chalk.red;

      const durationInMsecs = chalk.magenta(`${(duration ?? 0n) / 1000n}`);

      return `[${grayTime}] 🛑 ${methodPath} | ${statusColor(status)} | ${durationInMsecs} ms\n`;
    },
    onFailure({ request, error, code, datetime }) {
      const log = chalk.red(`[${datetime.toISOString()}]: ${request.method} ${request.url} | ERROR | ${code}`);
      return `${log}\n${error}`;
    },
  });
@cybercoder-naj
Copy link
Owner

Sounds like a reasonable addition. Will focus on it once the stable version has been released. Are there any major/minor bugs you've encountered?

@cybercoder-naj cybercoder-naj added feature request New feature or request planned Feature to be implemented later good first issue Good for newcomers labels Mar 26, 2024
@gitblit
Copy link
Author

gitblit commented Mar 26, 2024

I did encounter a bug when first using the sample for a custom logger. Your example logs to a file. I just copied & pasted what you had and ran it. This caused errors because the destination log file did not exist, which seemed a surprising error. I didn't chase that, I just removed the destination file config.

I am liking your library very much. 👍 It's a needed option for the Elysia community.

@cybercoder-naj cybercoder-naj linked a pull request Mar 30, 2024 that will close this issue
@cybercoder-naj
Copy link
Owner

cybercoder-naj commented Mar 30, 2024

Implemented. Ready to publish

@cybercoder-naj cybercoder-naj removed the planned Feature to be implemented later label Mar 30, 2024
@gitblit
Copy link
Author

gitblit commented Apr 1, 2024 via email

@gitblit
Copy link
Author

gitblit commented Apr 6, 2024

onRequest works great! Unfortunately, onSuccess breaks for me with the above customized logger with the change in 1.0.1.

@gitblit
Copy link
Author

gitblit commented Apr 6, 2024

Reported in #20

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature request New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants