Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

diary

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

diary

Fast logging library for both Node and Browser.

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.9K
decreased by-10.09%
Maintainers
1
Weekly downloads
 
Created
Source

diary

npm add diary makes logging simple


downloads size

⚡ Features

⚙️ Install

npm add diary

🚀 Usage

import { info, diary } from 'diary';

info('this important thing happened');
// ~> ℹ info  this important thing happened

const scopedDiary = diary('my-module', (event) => {
  if (event.level === 'error') {
    Sentry.captureException(event.error);
  }
});

scopedDiary.info('this other important thing happened');
// ~> ℹ info  [my-module] this other important thing happened

Controlling runtime emission of logs, using the code below as an example:

// example.js
import { diary } from 'diary';

const scopeA1 = diary('scopeA:one');
const scopeA2 = diary('scopeA:two');
const scopeB1 = diary('scopeB:one');
const scopeB2 = diary('scopeB:two');

scopeA1.info('message'); // won't log ✗
scopeA2.info('message'); // will log ✔
scopeB1.info('message'); // will log ✔
scopeB2.info('message'); // will log ✔
browser
localStorage.setItem('DEBUG', 'scopeA:two,scopeB:*');

// then your scripts

💡 Tip - Set this via the DevTools, then hit refresh. Saves you having to re-bundle.

node
DEBUG=scopeA:two,scopeB:* node example.js
workers

As of version v0.3.0 to enable log events you must use the enable programmatic api, this is due to Module Workers no longer offering global environment variables, and instead they are injected through an api.

Ambiant logs do however need to be statically enabled. (put a enable() in module scope).

Example
import { diary, enable } from 'diary';

const logger = diary('my-worker');

export default {
  async fetch(req, env, context) {
    enable(env.DEBUG);

    logger.info('request for', req.url);
  },
};
programmatic
import { diary, enable } from 'diary';

enable('scopeA:two,scopeB:*');

const scopeA1 = diary('scopeA:one');
const scopeA2 = diary('scopeA:two');
const scopeB1 = diary('scopeB:one');
const scopeB2 = diary('scopeB:two');

scopeA1.info('message'); // won't log ✗
scopeA2.info('message'); // will log ✔
scopeB1.info('message'); // will log ✔
scopeB2.info('message'); // will log ✔

enable('scopeA:*');

scopeA1.info('message'); // will log ✔
scopeA2.info('message'); // will log ✔
scopeB1.info('message'); // won't log ✗
scopeB2.info('message'); // won't log ✗

🔎 API

diary(name: string, onEmit?: Reporter)

Returns: log functions

A default diary is exported, accessible through simply importing any log function.

Example of default diary
import { info } from 'diary';

info("i'll be logged under the default diary");
name

Type: string

The name given to this diary—and will also be available in all logEvents.

onEmit (optional)

Type: Reporter

A reporter is run on every log message (provided its enabled). A reporter gets given the LogEvent interface:

interface LogEvent {
  name: string;
  level: LogLevels;

  message: string;
  extra: unknown[];
}

Errors (for error and fatal) there is also an error: Error property.

log functions

A set of functions that map to console.error, console.warn, console.debug, console.info and console.info. Aptly named;

  • fatal(message: string | Error, ...extra: any[])

  • error(message: string | Error, ...extra: any[])

    If an Error instance is sent, the error object will be accessible with the error property on the context, this is for both fatal and error.

  • warn(message: string, ...extra: any[])

  • debug(message: string, ...extra: any[])

  • info(message: string, ...extra: any[])

  • log(message: string, ...extra: any[])

All extra parameters are simply spread onto the console function, so node/browser's built-in formatters will format any objects etc.

diary (optional)

Type: Diary

The result of a calling diary;

enable(query: string)

Type: Function

Opts certain log messages into being output. See more here.

💨 Benchmark

via the /bench directory with Node v17.4.0

benchmark :: jit
  bunyan               x   4,741 ops/sec ±1.72% (85 runs sampled)
  debug                x 182,974 ops/sec ±2.27% (85 runs sampled)
  diary                x 582,962 ops/sec ±7.28% (78 runs sampled)
  pino                 x  17,792 ops/sec ±1.87% (85 runs sampled)

benchmark :: aot
  bunyan               x 255,435 ops/sec ±7.42% (78 runs sampled)
  debug                x 551,903 ops/sec ±8.55% (80 runs sampled)
  diary                x 526,972 ops/sec ±16.96% (74 runs sampled)
  pino                 x 451,559 ops/sec ±3.77% (90 runs sampled)

License

MIT © Marais Rossouw

Keywords

FAQs

Package last updated on 18 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc