workers-logger icon indicating copy to clipboard operation
workers-logger copied to clipboard

Also log to console

Open RichiCoder1 opened this issue 3 years ago β€’ 2 comments

Howdy! Loving this minimalist lib for logging. On feedback I had is it'd still be nice if it logged to console by default and/or in addition to a report for Web Console and Wrangler scenarios where it'd still be reading logs from that source in real time.

RichiCoder1 avatar Feb 02 '22 21:02 RichiCoder1

Hey πŸ‘‹πŸ» thank you so much β€”Β glad its been useful for you. Yeah that's super helpful! And console logs are largely logged into the void, so no harm in doing it by default.

But in saying that, I don't really want to impose that for everyone, the library is designed to "bring your own reporter".

So can this work for you?

const reporter: Reporter = (events, { req, res }) => {
   console.log(events); // πŸ‘ˆ just add this line in your reporter.

   // send events to your logging framework
};

addEventListener('fetch', (event) => {
  const { request } = event;
  const log = track(request, 'my-worker', reporter);

  log.info('gearing up to make a response');

  const res = new Response('hi there');

  event.waitUntil(log.report(res));

  return res;
});

Sorry πŸ˜… going back and forth on this idea. I think what makes sense is when there is no reporter, to just have a default one that logs to the console.

Ill action this one πŸš€

maraisr avatar Feb 02 '22 23:02 maraisr

Admitting that workers should be very short lived and therefore it shouldn't matter that much, the only concern I have with that is it posts logs as a bundle rather than when they're immediately.

RichiCoder1 avatar Feb 03 '22 01:02 RichiCoder1