- Notifications
You must be signed in to change notification settings - Fork7
📑 Zero-dependency, fast logging library for Node, Browser and Workers
License
maraisr/diary
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Dear diary, you make my logging so easy
This is free to use software, but if you do like it, consisder supporting me ❤️
- Nodependencies
- Outstandingperformance
- Support for
debug
's filter
npm add diary
import{info,diary,enable}from'diary';// 1️⃣ Choose to enable the emission of logs, or not.enable('*');// 2️⃣ log somethinginfo('this important thing happened');// ~> ℹ info this important thing happened// Maybe setup a scoped loggerconstscopedDiary=diary('my-module',(event)=>{if(event.level==='error'){Sentry.captureException(event.error);}});// 3️⃣ log more thingsscopedDiary.info('this other important thing happened');// ~> ℹ info [my-module] this other important thing happened
Node users
Theenable
function is executed for you from theDEBUG
environment variable. And as a drop in replacement fordebug
.
DEBUG=client:db,server:* node example.js
Returns:log functions
A default diary is exported, accessible through simply importing anylog function.
Example of default diary
import{info}from'diary';info("i'll be logged under the default diary");
Type:string
The name given to thisdiary—and will also be available in all logEvents.
Type:Reporter
A reporter is run on every log message (provided itsenabled). A reporter gets given theLogEvent
interface:
interfaceLogEvent{name:string;level:LogLevels;messages:any[];}
Note: you can attach any other context in middleware.
Example
import{diary,default_reporter}from'diary';constscope=diary('scope',(event)=>{event.ts=newDate();returndefault_reporter(event);});
Errors (forerror
andfatal
) there is also anerror: Error
property.
A set of functions that map toconsole.error
,console.warn
,console.debug
,console.info
andconsole.info
.Aptly named;
fatal
,error
,warn
,debug
,info
, andlog
. All of which follow the same api signature:
declarelogFunction(message:object|Error|string, ...args: unknown[]):void;
All parameters are simply spread onto the function and reported. Node/browser's built-in formatters will format anyobjects (by default).
info('hi there');// ℹ info hi thereinfo('hi %s','there');// ℹ info hi thereinfo('hi %j',{foo:'bar'});// ℹ info hi { "foo": "bar" }info('hi %o',{foo:'bar'});// ℹ info hi { foo: 'bar' }info({foo:'bar'});// ℹ info { foo: 'bar' }
Type:Diary
The result of a callingdiary;
Type:Function
Opts certain log messages into being output. See morehere.
via the
/bench
directory with Node v20.2.0
JIT✔ diary ~ 1,434,414 ops/sec ± 0.16%✔ pino ~ 47,264 ops/sec ± 0.02%✔ bunyan ~ 9,644 ops/sec ± 0.01%✔ debug ~ 444,612 ops/sec ± 0.22%AOT✔ diary ~ 1,542,796 ops/sec ± 0.29%✔ pino ~ 281,232 ops/sec ± 0.03%✔ bunyan ~ 588,768 ops/sec ± 0.16%✔ debug ~ 1,287,846 ops/sec ± 0.24%
AOT: The logger is setup a head of time, and ops/sec is the result of calling the log fn. Simulates long runningprocess, with a single logger. JIT: The logger is setup right before the log fn is called per op. Simulates setting upa logger per request for example.
- workers-logger — fast and effective logging forCloudflare Workers
MIT ©Marais Rossouw
About
📑 Zero-dependency, fast logging library for Node, Browser and Workers