besonders-logger
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

besonders-logger

Why?

Simple & powerful syntax

const { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(
    Logger.INFO, 
    { prefix: '[DB]' } // (optional)
);

DEBUG(`test=${1}`, extra, {objects})
LOG(`test=${1}`)
if (VERBOSE.isEnabled) VERBOSE(`won't be run:`, heavyComputation())
WARN(`test=${1}`, extra, {objects})
throw ERROR(`msg logged & returned as Error, plus formatted:`, extra, {objects})

... while still preserving correct log location

  • in devtools, you see the correct line as origin
  • in node.js you can print the source location in grey (optional)

(optional) Concise relative times

Screenshot (only with custom devtoolsFormatters enabled)

Installation

Add besonders-logger to a Javascript/Typescript project:

pnpm add besonders-logger
yarn add besonders-logger
npm add besonders-logger

# (optional) install vscode snippets:
# Linux script, source: .vscode/install-snippets.sh
pnpm run besonders-install-snippets
# or copy manually from the source repo

Usage

And in any file you want to use logger:

// protip: use `logsetup` snippet

import { Logger } from "@ztax/logger/src";

const { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(Logger.INFO); // eslint-disable-line no-unused-vars

Change log-level

const { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(Logger.DEBUG); // eslint-disable-line no-unused-vars
const { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(Logger.WARN); // eslint-disable-line no-unused-vars
//... or any other level

console.groupCollapsed mdn

LOG.group('init', () => {
	LOG('do something')
	doSomething()
	DEBUG('something done')
})

The inner function will always be executed - also when the loglevel would be filtered, in which case there is no group around the inner logs.

Temporarily override loglevel for a single line

Pro tip: add a pre-commit hook to disallow committing

VERBOSE.force("foo") // will be printed in any loglevel

Temporarily log everything

window.FORCE_DISABLE_LOGLEVEL = true

Readme

Keywords

none

Package Sidebar

Install

npm i besonders-logger

Weekly Downloads

37

Version

1.0.1

License

MIT

Unpacked Size

77.7 kB

Total Files

20

Last publish

Collaborators

  • tennox