Context-carrying logger with conditional methods.

Overview

logger

Provides a simple logging interface as well as a LogContext class which carries a context around.

Installation

npm install @rocicorp/logger

Usage

LogContext

LogContext includes a context that gets included in the log message.

import {LogContext} from '@rocicorp/logger';

const lc = new LogContext('info');
lc.info('hello'); // prints "hello"

const lc2 = new LogContext('info', 'name');
lc.info('hello'); // prints "name hello"

const lc3 = lc2.addContext('bbb');
lc3.info('hello'); // prints "name bbb hello"

const lc4 = lc3.addContext('ccc');
lc4.info('hello'); // prints "name bbb ccc hello"

const lc5 = lc4.addContext('ddd', 'eee');
lc5.info('hello'); // prints "name bbb ccc ddd=eee hello"

// Or get a context logger appropriate for the Node environment.
const nlc = newNodeLogContext('debug');
nlc.info('fff'); // prints "INF fff"

OptionalLogger

interface OptionalLogger { ... }

This interface is used to provide conditional logging. It is intended to be used with conditional method calling ?.().

import {ConsoleLogger, type OptionalLogger} from '@rocicorp/logger';

const l: OptionalLogger = new ConsoleLogger('info');
l.info?.('hello'); // prints "hello"
l.debug?.('hello'); // does not print anything

But more importantly it does not evaluate the arguments.

import {ConsoleLogger, type OptionalLogger} from '@rocicorp/logger';
const alwaysThrows = () => {
  throw new Error();
};
const l: OptionalLogger = new ConsoleLogger('info');
l.debug?.(alwaysThrows()); // does not print anything, but does not throw

Implementing Your Own LogSink

There is also the LogSink interface which is very minimal. Its intended use is for custom log implementations.

class ExampleLogSink implements LogSink {
  log(level: LogLevel, ...args: unknown[]): void {
    //...
  }
}

You can get an OptionalLogger from a LogSink using OptionalLoggerImpl.

import {
  OptionalLoggerImpl,
  type LogSink,
  type OptionalLogger,
} from '@rocicorp/logger';

const logSink: LogSink = new ExampleLogSink();
const l: OptionalLogger = new OptionalLoggerImpl(logSink);
You might also like...

ZxCDDoS for education with LAYER 7, LAYER 4, AMP METHODS

ZxCDDoS for education with LAYER 7, LAYER 4, AMP METHODS

๐Ÿš€ ZxCDDoS: Release v1.0 - Free DDoS Panel ๐Ÿš€ Terminal only accepts ANSI color. Username: admin Password: admin Language Logs Fixed L7 methods (crash,

Jan 3, 2023

Elections in Russia, Saint Petersburg, 2021: results, turnout, and commission data. With image charts and brief introduction to election fraud methods used.

Elections in Russia, Saint Petersburg, 2021: results, turnout, and commission data. With image charts and brief introduction to election fraud methods used.

2021 Elections in Saint Petersburg, Russia ๐Ÿ‡ท๐Ÿ‡บ ะŸะตั€ะตะบะปัŽั‡ะธั‚ัŒัั ะฝะฐ ั€ัƒััะบะธะน ๐Ÿ“ˆ Jump to charts ๐Ÿค– Telegram bot In Russia, Saint Petersburg from 17th to 19

Dec 26, 2022

A small (~600B gzip), useful set of methods for lazy iteration of iterables.

@ricokahler/lazy ยท A small (~600B gzip*), useful set of methods for lazy iteration of iterables. Why this lazy lib? Do I even need a lazy lib? Install

Sep 10, 2022

It's a javascript Class which contains utility methods that simplify working with google maps web SDK

About GoogleMapsJSHelper It's a javascript Class which contains utility methods that simplify working with google maps web SDK Note: i used ES7 Class

Jul 23, 2022

A library with different methods to encode and decode data.

encryption_lib for Deno A library with different methods to encode and decode data. Usage Example: Caesar Cipher import { Caesar } from "https://deno.

Feb 3, 2022

Repository trying to recreate Javascript (node) methods.

Javascript Polyfills This is an open-source project, where we try to do a polyfill for every (or almost all of them) Javascript Methods that currently

Oct 8, 2022

Provides simple and the most useful methods to string operations in JavaScript / Node.js

๐Ÿ”ช Strops (String Operations) Provides simple methods for the most useful operations with substrings: - remove, replace, get from A to B, get from A t

May 20, 2022

๐Ÿงฉ TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

๐Ÿงฉ TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types such as number or boolean (not Value Objects)

๐Ÿงฉ TypeScript Primitives type TypeScript utility type in order to ensure to return only properties (not methods) containing values in primitive types

Dec 7, 2022

A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages (https://developers.cloudflare.com/pub-sub/)

pubsub A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages. This includes: A isValidBrokerRequest helper for au

Dec 4, 2022
Easy conditional if-else logic for your Cypress testsDo not use

cypress-if Easy conditional if-else logic for your Cypress tests Tested with cy.get, cy.contains, cy.find, .then, .within commands in Cypress v9 and v

Gleb Bahmutov 36 Dec 14, 2022
A NodeJS Console Logger with superpowers

LogFlake LogFlake is a NodeJS console logger with superpowers. It has the same API as the usual Console but with beautified output, a message header w

Felippe Regazio 57 Oct 9, 2022
Another logger in JS. This one offers a console.log-like API and formatting, colored lines and timestamps (or not if desired), all that with 0 dependencies.

hellog Your new logger ! hellog is a general-purpose logging library. It offers a console.log-like API and formatting, extensible type-safety colored

Maxence Lecanu 4 Jan 5, 2022
Base provides advanced Promise Queue Manager, Custom Console Logger and other utilities.

Base Base provides frequently used functionality like cutome logger, response helper, Custom Promise and Instance composer. These are used in almost a

PLG Works 14 Jun 14, 2022
The JSON logger you always wanted for Lambda.

MikroLog The JSON logger you always wanted for Lambda. MikroLog is like serverless: There is still a logger ("server"), but you get to think a lot les

Mikael Vesavuori 11 Nov 15, 2022
An elegant console logger.

kons An elegant console logger. Features Tiny (Minified + Gzipped โ‰ˆ 0.1kB). Beautiful. Easy to use. Customizable. TypeScript type declarations include

ส€แด€ส 6 Aug 13, 2022
Complete JavaScipt Array methods Cheatsheet ๐Ÿš€

Javascript Array Cheatsheet ?? Click to download โ˜ Important ?? There is no way this is perfect or include all the methods. I'll try to fix/add more m

Ayush 91 Dec 7, 2022
EasyMod.js, a library that adds multiply methods into JS!

EasyMod.js EasyMod.js by BonesYT, a library that adds multiple methods into JS! This adds a lot of methods to: strings, numbers, booleans, bigint, fun

null 3 May 16, 2022
This branch is created to make receive and send data to api using async and await methods

Microverse-Leader-Board Project from module 2 week 4 This branch is created to make receive and send data to api using async and await methods Screens

Akshitha Reddy 6 Apr 22, 2022
The invoker based on event model provides an elegant way to call your methods in another container via promisify functions

The invoker based on event model provides an elegant way to call your methods in another container via promisify functions. (like child-processes, iframe, web worker etc).

ๅฐนๆŒš 7 Dec 29, 2022