Giter Club home page Giter Club logo

diary's Introduction

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:

browser

import { diary } from 'diary';

localStorage.setItem('DEBUG', '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 βœ”

node

// 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 βœ”

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

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

Validation
βœ” @graphile/logger
βœ” bunyan
βœ” debug
βœ” diary
βœ” pino
βœ” roarr
βœ” ulog
βœ” winston

Benchmark
  @graphile/logger     x 21,801,529 ops/sec Β±0.88% (93 runs sampled)
  bunyan               x 109,073 ops/sec Β±0.71% (94 runs sampled)
  debug                x 228,734 ops/sec Β±1.28% (88 runs sampled)
  diary                x 6,962,434 ops/sec Β±0.50% (93 runs sampled)
  pino                 x 48,998 ops/sec Β±0.93% (91 runs sampled)
  roarr                x 927,402 ops/sec Β±0.64% (94 runs sampled)
  ulog                 x 25,681 ops/sec Β±27.59% (17 runs sampled)
  winston              x 12,314 ops/sec Β±5.01% (83 runs sampled)

Ran with Node v16.2.0

License

MIT Β© Marais Rossouw

diary's People

Contributors

gajus avatar lukeed avatar maraisr avatar maxmilton avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.