All Projects → maraisr → Diary

maraisr / Diary

Licence: mit
📑 Zero-dependency, fast logging library for both Node and Browser.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Diary

Xcglogger
A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.
Stars: ✭ 3,710 (+4596.2%)
Mutual labels:  logging, logging-library, debug
Cocoadebug
iOS Debugging Tool 🚀
Stars: ✭ 3,769 (+4670.89%)
Mutual labels:  logging, logging-library, debug
Logger
HTTP middleware for Go that logs web requests to an io.Writer.
Stars: ✭ 24 (-69.62%)
Mutual labels:  middleware, logging
Trail
Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platform the code is running.
Stars: ✭ 13 (-83.54%)
Mutual labels:  logging, logging-library
Fliplog
fluent logging with verbose insight, colors, tables, emoji, filtering, spinners, progress bars, timestamps, capturing, stack traces, tracking, presets, & more...
Stars: ✭ 41 (-48.1%)
Mutual labels:  logging, debug
Mex Vocabulary
MEX Vocabulary: a lightweight machine learning interchange format
Stars: ✭ 19 (-75.95%)
Mutual labels:  logging, logging-library
Yii2 Telegram Log
Telegram log target for Yii 2
Stars: ✭ 24 (-69.62%)
Mutual labels:  logging, logging-library
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+1111.39%)
Mutual labels:  middleware, fast
Gear
A lightweight, composable and high performance web service framework for Go.
Stars: ✭ 544 (+588.61%)
Mutual labels:  middleware, logging
Evergreen
Most natural Swift logging
Stars: ✭ 75 (-5.06%)
Mutual labels:  logging, logging-library
Log4cplus
log4cplus is a simple to use C++ logging API providing thread-safe, flexible, and arbitrarily granular control over log management and configuration. It is modelled after the Java log4j API.
Stars: ✭ 1,054 (+1234.18%)
Mutual labels:  logging, logging-library
Slog
Structured, contextual, extensible, composable logging for Rust
Stars: ✭ 1,068 (+1251.9%)
Mutual labels:  logging, logging-library
Cabin
🌲 Cabin is the best JavaScript and Node.js logging service and logging npm package
Stars: ✭ 622 (+687.34%)
Mutual labels:  logging, debug
Screenlog.js
Bring console.log on the screen
Stars: ✭ 591 (+648.1%)
Mutual labels:  logging, logging-library
Yii2 Slack Log
Pretty Slack log target for Yii 2
Stars: ✭ 24 (-69.62%)
Mutual labels:  logging, logging-library
Nlog
NLog - Advanced and Structured Logging for Various .NET Platforms
Stars: ✭ 5,296 (+6603.8%)
Mutual labels:  logging, logging-library
Eliot
Eliot: the logging system that tells you *why* it happened
Stars: ✭ 874 (+1006.33%)
Mutual labels:  logging, logging-library
Ios Sdk
AppSpector is a debugging service for mobile apps
Stars: ✭ 56 (-29.11%)
Mutual labels:  logging, debug
Logstash Logger
Ruby logger that writes logstash events
Stars: ✭ 442 (+459.49%)
Mutual labels:  logging, logging-library
Fern
Simple, efficient logging for Rust
Stars: ✭ 524 (+563.29%)
Mutual labels:  logging, logging-library

diary

yarn add diary makes logging simple


downloads size

⚡ Features

⚙️ Install

yarn add diary

🚀 Usage

import { info, diary, after } from 'diary';

after((logEvent) => {
  if (logEvent.level === 'error') {
    Sentry.captureException(logEvent.extra[0]);
  }
});

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

const scopedDiary = diary('my-module');
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

🔎 API

diary(name: string)

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, will appear in the middleware under the name property as well as in console messages.

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 through the first item in the extra's array in a middleware. 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.

{before,after}(callback: function, diary?: Diary)

Returns: Dispose

Middlewares are function handlers that run for every log function. They allow for modifying the log event object, or simply returning false to bailout. Executing in a forwards direction, meaning middlewares will be run sequentially as they were defined.

When the return is called, it will remove the middleware from the diary instance.

handler

Type: Function

Which gets given a single argument of:

interface LogEvent {
  name: string;
  level: LogLevels;
  message: string;
  extra: unknown[];
}
Example
import { before, after, info } from 'diary';

before((logEvent) => {
  logEvent.context = {
    hello: 'world',
  };
});

after((logEvent) => {
  if (logEvent.level === 'error') {
    fetch('/api/errors', {
      method: 'POST',
      body: JSON.stringify({
        error: logEvent.extra[0],
        context: logEvent.context,
      }),
    });
  }
});

info('something informative');

This method isn't a Promise, so won't be awaited. It's a fire and forget kinda deal.

diary (optional)

Type: Diary

The result of a calling diary;

A middleware without the optional second parameter, will run for all diaries.

setLevel(level: LogLevel)

Returns: void

Sets the level form which log's will get output to the console.

level

Type: string

One of the log functions's name.

Example
import { setLevel, info } from 'diary';
setLevel('error');

info('something informative');

Note; this will also filter the events from all middlewares.

💨 Benchmark

Validation
✔ diary
✔ ulog
✔ roarr
✔ bunyan
✔ debug
✔ pino
✔ winston

Benchmark
  diary      x 840,954 ops/sec ±1.18% (89 runs sampled)
  ulog       x 25,409 ops/sec ±40.24% (10 runs sampled)
  roarr      x 1,205,786 ops/sec ±1.37% (93 runs sampled)
  bunyan     x 16,343 ops/sec ±0.78% (95 runs sampled)
  debug      x 35,392 ops/sec ±0.91% (89 runs sampled)
  pino       x 47,908 ops/sec ±1.36% (95 runs sampled)
  winston    x 13,790 ops/sec ±2.33% (93 runs sampled)

Ran with Node v15.5.0

License

MIT © Marais Rossouw

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].