Garn Logger is a utility to enhance the default console logging in JavaScript/TypeScript. It allows you to set logging levels and filter logs based on a query.
deno add jsr:@garn/logger
or
npx jsr add @garn/logger
import { better } from "@garn/logger";
You can set the log level to control which logs are displayed. The available
levels are error
, warn
, info
, and debug
.
better(console).level("warn"); console.error("This is an error"); // Will log console.warn("This is a warning"); // Will log console.info("This is an info"); // Will not log console.debug("This is a debug"); // Will not log
You can filter logs based on a string or regular expression.
better(console).filter("test"); console.error("this is a test"); // Will log console.warn("another test"); // Will log console.info("not a match"); // Will not log console.debug("test again"); // Will log
Using a regular expression:
better(console).filter(/test/i); console.error("this is a test"); // Will log console.warn("another test"); // Will log console.info("not a match"); // Will not log console.debug("test again"); // Will log
Using mutliple filters acts as an OR operation:
better(console).filter("test","another"); console.error("this is a test"); // Will log console.warn("another"); // Will log console.info("not a match"); // Will not log
Or use addFilter to add multiple filters:
better(console).addFilter("test").addFilter("another"); console.error("this is a test"); // Will log console.warn("another"); // Will log console.info("not a match"); // Will not log
Reset filter with .resetFilter()
:
better(console).filter("XXX"); better(console).resetFilter(); console.error("this is a test"); // Will log
You can use console.only
to log only this log and filter every log ahead.
better(console) console.only("logs this"); // Will log console.error("this is filtered out"); // Will not log
It's a shortcut for:
better(console).filter("logs this"); console.debug("logs this");
Add Package
deno add jsr:@garn/logger
Import symbol
import * as logger from "@garn/logger";
---- OR ----
Import directly with a jsr specifier
import * as logger from "jsr:@garn/logger";
Add Package
npx jsr add @garn/logger
Import symbol
import * as logger from "@garn/logger";
Add Package
yarn dlx jsr add @garn/logger
Import symbol
import * as logger from "@garn/logger";
Add Package
pnpm dlx jsr add @garn/logger
Import symbol
import * as logger from "@garn/logger";
Add Package
bunx jsr add @garn/logger
Import symbol
import * as logger from "@garn/logger";