Skip to main content
Home

On top of `console.log`.

This package works with Node.js, Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (2.2.3)

@marianmeres/clog

On top of console.log. Mostly.

Tiny namespaced console wrapper with configurable silence switches (global or local). And with custom writer support.

Installation

Deno

deno add jsr:@marianmeres/clog

Node

npm i @marianmeres/clog
pnpm add @marianmeres/clog

Usage

// create logger
const clog = createClog("foo");

// use any of the debug, info, log, warn, error (or none) methods
clog("bar", "baz"); // same as clog.log
clog.debug("bar", "baz");
clog.log("bar", "baz");
clog.info("bar", "baz");
clog.warn("bar", "baz");
clog.error("bar", "baz");
// 6 x output: [foo] bar baz

// example for errors only output (configured locally)
const clog = createClog("foo", { error: true });
clog("debug"); // ignored
clog.error("error");
// output: [foo] error

// example for global config silence
createClog.DISABLE = true;
clog("bar", "baz");
// output none

// to use clog without namespace, use `false`
createClog(false)("foo", "bar");
// output: foo bar

// to color the namespace label use `color` or the %c modifier
// these examples work in browser or deno (not in node)
clog("%cbar", "color:red");
clog.color("red")("bar");
// outputs "[foo] bar" where "[foo]" is in red (but not bar)

// you can automatically prepend time or dateTime
const clog = createClog("foo", { time: true });
clog("bar");
// outputs: [HH:MM:SS.mmm] [foo] bar

const clog = createClog("foo", { dateTime: true });
clog("bar");
// outputs: [YYYY-MM-DD HH:MM:SS.mmm] [foo] bar

Custom writer setup example

This creates colored output based on log level

import { gray, green, red, yellow } from "kleur/colors";

// somewhere in app bootstrap
const _setup = (k, c) => (...a) => console[k].apply(null, a.map((v) => c(v)));
createClog.WRITER = {
	debug: _setup("debug", gray),
	log: _setup("log", gray),
	info: _setup("info", green),
	warn: _setup("warn", yellow),
	error: _setup("error", red),
};

// somewhere later in app
const clog = createClog("my-module");
clog("foo"); // output "[my-module] foo" in gray
clog.info("success"); // output "[my-module] success" in green
clog.error("alert!"); // output "[my-module] alert!" in red

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@marianmeres/clog

Import symbol

import * as clog from "@marianmeres/clog";
or

Import directly with a jsr specifier

import * as clog from "jsr:@marianmeres/clog";

Add Package

pnpm i jsr:@marianmeres/clog
or (using pnpm 10.8 or older)
pnpm dlx jsr add @marianmeres/clog

Import symbol

import * as clog from "@marianmeres/clog";

Add Package

yarn add jsr:@marianmeres/clog
or (using Yarn 4.8 or older)
yarn dlx jsr add @marianmeres/clog

Import symbol

import * as clog from "@marianmeres/clog";

Add Package

npx jsr add @marianmeres/clog

Import symbol

import * as clog from "@marianmeres/clog";

Add Package

bunx jsr add @marianmeres/clog

Import symbol

import * as clog from "@marianmeres/clog";