Skip to content

🪵 Fast effective logging for Cloudflare Workers

License

Notifications You must be signed in to change notification settings

maraisr/workers-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

workers-logger licenses

Fast and effective logging for Cloudflare Workers.



This is free to use software, but if you do like it, consider supporting me ❤️

sponsor me buy me a coffee

⚡️ Features

⚙️ Install

npm add workers-logger

🚀 Usage

import { track } from 'workers-logger';

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

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

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

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

	return res;
});

to see more visit examples

🔎 API

track(request: Request, name?: string, reporter?: Reporter)

Returns log functions and our .report method.

report(response: Response)

Returns a promise with intended usage with event.waitUntil. And thus in terns runs your reporter defined on track.

Reporters

A reporter is a single function ran at then end of .report. And gives you the ability to send that data somewhere, or merely into dashboard logs.

import type { Reporter } from 'workers-logger';
import { track } from 'workers-logger';

const reporter: Reporter = (events, { req, res }) => {
	// do whatever you want
};

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;
});

example when sending into Logflare at /examples/workers/logflare

License

MIT © Marais Rossouw