bark-logger
v1.0.2
Published
Simple log framework for browser and server
Readme
bark-logger
A small, zero-dependency logger for Node.js and the browser. It prints colored, timestamped lines to the console, filters by level, and lets you plug in your own outputs.
[14:37:39] INFO (main) Server started on port 3000
[14:37:41] WARN (db) Slow query: 1250ms
[14:37:42] ERROR (api) Request failed: ECONNRESETInstall
npm install bark-loggerTypeScript types are included. The package works with both import and require.
Quick start
import { getLogger } from 'bark-logger';
// or: const { getLogger } = require('bark-logger');
const log = getLogger();
log.info('Server started on port 3000');
log.warn('Disk space is getting low');
log.error('Something went wrong');Log levels
There are six levels, from least to most severe:
| Level | Method | Value |
| ------- | --------------- | ----- |
| TRACE | log.trace(msg) | 0 |
| DEBUG | log.debug(msg) | 1 |
| INFO | log.info(msg) | 2 |
| WARN | log.warn(msg) | 3 |
| ERROR | log.error(msg) | 4 |
| FATAL | log.fatal(msg) | 5 |
You can also pass the level explicitly with log.log(msg, Levels.WARN). Without a level, log.log(msg) logs at TRACE.
Named loggers
Give each part of your app its own name so you can tell where a line came from. The name appears in parentheses after the level.
const dbLog = getLogger('db');
const apiLog = getLogger('api');
dbLog.warn('Slow query: 1250ms'); // [14:37:41] WARN (db) Slow query: 1250ms
apiLog.error('Request failed'); // [14:37:42] ERROR (api) Request failedgetLogger(name) returns the same instance every time you ask for the same name, so you can call it from any file without passing loggers around. The default name is main. Use new Logger(name) instead if you want a fresh, uncached instance.
The level methods are bound to their logger, so destructuring works:
const { info, error } = getLogger('worker');
info('Job queued');Choosing which levels to show
Messages below the threshold are ignored. The default threshold is INFO, so TRACE and DEBUG messages are hidden unless you lower it.
With an environment variable (Node.js)
Set LOGLEVEL to a level name (any case) or its number:
LOGLEVEL=debug node app.jsLOGLEVEL=4 node app.jsAn unrecognized value falls back to INFO. The variable is read once, when the library is first loaded.
In code
import { config, Levels } from 'bark-logger';
config.threshold = Levels.DEBUG;The config is shared by every logger in the process, including loggers created in other modules or by other packages that use bark-logger. Changing it in one place changes it everywhere, at any time.
Custom outputs (actions)
Every message that passes the threshold is handed to each function in config.actions. By default the list holds a single console printer. Add your own functions to send logs somewhere else:
import { config, Levels } from 'bark-logger';
config.actions.push((timestamp, level, name, message) => {
if (level >= Levels.ERROR) {
errorList.push(`${timestamp.toISOString()} [${Levels[level]}] ${name}: ${message}`);
}
});Each action receives:
| Argument | Type | Description |
| ----------- | -------- | -------------------------------------------------------- |
| timestamp | Date | When the message was logged |
| level | Levels | The level number; Levels[level] gives its name |
| name | string | The logger's name |
| message | string | The message text |
Actions can be async, for example to post logs to a server:
config.actions.push(async (timestamp, level, name, message) => {
await fetch('https://logs.example.com/ingest', {
method: 'POST',
body: JSON.stringify({ timestamp, level: Levels[level], name, message })
});
});A failing action never breaks your app. If an action throws or its promise rejects, the error is reported with console.error and the remaining actions still run.
To replace the console output entirely, assign a new list. Keep a copy of the defaults if you might want them back:
const defaultActions = [...config.actions];
config.actions = [myAction]; // only your action
config.actions = [...defaultActions]; // console output restoredIn TypeScript, the action type is available as:
type LogAction = typeof config.actions[number];Browser use
bark-logger runs in the browser through any bundler. There's no LOGLEVEL environment variable there, so set config.threshold in code. Output uses ANSI color codes, which most terminals render. Support in browser developer consoles varies, and unsupported consoles show the codes as raw text.
Development
npm test| Script | What it does |
| --------------- | ----------------------------------------------------------------- |
| npm test | Compiles with tsc, then runs the Jest tests in build/tests |
| npm run build | Compiles to build/ |
| npm run local | Compiles in watch mode |
| npm run link | Builds the package into out/ and runs npm link for local testing |
| npm run clean | Deletes build/ and out/ |
| npm run prerelease | Builds the package into out/ and publishes it to npm under the next tag |
Pushing to main runs the tests on Node 22, 24 and 26. If the version in package.json isn't on npm yet, CI publishes it and creates a matching GitHub release.
