• Stars
    star
    250
  • Rank 162,397 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“‘ Zero-dependency, fast logging library for Node, Browser and Workers

diary

Dear diary, you make my logging so easy

js downloads gzip size brotli size

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

sponsor me buy me a coffee

⚑ Features

βš™οΈ Install

npm add diary

πŸš€ Usage

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

// 1️⃣ Choose to enable the emission of logs, or not.
enable('*');

// 2️⃣ log something
info('this important thing happened');
// ~> β„Ή info  this important thing happened

// Maybe setup a scoped logger
const scopedDiary = diary('my-module', (event) => {
  if (event.level === 'error') {
    Sentry.captureException(event.error);
  }
});

// 3️⃣ log more things
scopedDiary.info('this other important thing happened');
// ~> β„Ή info  [my-module] this other important thing happened
Node users

The enable function is executed for you from the DEBUG environment variable. And as a drop in replacement for debug.

DEBUG=client:db,server:* node example.js

πŸ”Ž 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;

  messages: any[];
}

Note: you can attach any other context in middleware.

Example
import { diary, default_reporter } from 'diary';
const scope = diary('scope', (event) => {
  event.ts = new Date();
  return default_reporter(event);
});

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, error, warn, debug, info, and log. All of which follow the same api signature:

declare logFunction(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 any objects (by default).

info('hi there'); // β„Ή info  hi there
info('hi %s', 'there'); // β„Ή info  hi there
info('hi %j', { foo: 'bar' }); // β„Ή info hi { "foo": "bar" }
info('hi %o', { foo: 'bar' }); // β„Ή info hi { foo: 'bar' }
info({ foo: 'bar' }); // β„Ή info { foo: 'bar' }

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 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 running process, with a single logger. JIT: The logger is setup right before the log fn is called per op. Simulates setting up a logger per request for example.

Related

License

MIT Β© Marais Rossouw

More Repositories

1

meros

πŸͺ’ A fast utility that makes reading multipart responses simple
TypeScript
185
star
2

workers-logger

πŸͺ΅ Fast effective logging for Cloudflare Workers
TypeScript
50
star
3

rian

πŸ‘£ Effective tracing for the edge and origins
TypeScript
33
star
4

relay-sentry

βš› Relay log function that enriches Sentry with Relay life cycles and GraphQL data
TypeScript
29
star
5

ports-list

28
star
6

piecemeal

πŸͺ€ Send your data piecemeal
TypeScript
24
star
7

swrr

⏩ Stale While Revalidated Resources β€” keeps data fast
TypeScript
24
star
8

mosaic.js

Delaunay triangulation shaded, from a light source based on mouse cursor.
TypeScript
24
star
9

dldr

🧩 A tiny/fast dataloader implementation
TypeScript
20
star
10

storybook-addon-grid

⚜️ Column guides that help you align your stories
TypeScript
20
star
11

configs-webpack-plugin

Runtime Config mangement for webpack projects
TypeScript
15
star
12

tctx

Blazing fast traceparents for use in w3c Trace Context
TypeScript
12
star
13

async-boundary

πŸƒβ€β™‚οΈ A React async-boundary that couples an error-boundary as well as a suspense container
TypeScript
12
star
14

graphql-workers

βš›οΈ graphql-workers puts graphql on the edge
TypeScript
7
star
15

object-identity

#️⃣ Object hashing for structural equality
TypeScript
7
star
16

fgs

Fetch GraphQL Schema from a GraphQL HTTP Endpoint from grphql-config that is Relay (rust) aware.
TypeScript
6
star
17

rollup-plugin-treat

TypeScript
4
star
18

tabling

Let's table this object till a later date
TypeScript
3
star
19

apollo-link-multipart

TypeScript
3
star
20

web-vitals

Cloudflare Workers enabled web-vitals
TypeScript
3
star
21

git.ski

TypeScript
2
star
22

build-header

πŸ™ƒ Logs a pretty header to your build scripts
JavaScript
2
star
23

uni-days

Bits n bobs from my software written whilst studying at QUT
HTML
2
star
24

git-remote-fetcher

A tool to help me keep my git remote's synced
Rust
2
star
25

dotfiles

⚑ my env
Shell
2
star
26

.github

🩺 The community health files and GitHub shared configuration files
2
star
27

posthtml-obfuscate

PostHTML plugin that obfuscates emails.
JavaScript
2
star
28

website

🌏 personal brand, and guinea pig
HTML
2
star
29

obs-count-to-timer

An OBS script to count to a specified time
Lua
1
star
30

configs

my shared config files β€” prettier, tsconfig, ...
1
star
31

posthtml-json

PostHTML plugin that does stuff with JSON.
JavaScript
1
star
32

mar.fyi

βš™οΈ My home brand url shortening service
Rust
1
star
33

treat-repro

JavaScript
1
star
34

auth0-repro

JavaScript
1
star