npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nowarajs/logger

v1.3.3

Published

Type-safe logging library for Bun with advanced TypeScript body intersection, modular sink pattern, transform streams, and immutable API design.

Readme

🎯 NowaraJS Logger

nowarajs-logger-wall

Logging in Bun often means choosing between "fast but dumb" or "smart but blocking". I built NowaraJS Logger because I wanted both: a type-safe, sink-based system that never blocks your main thread.

Why this package?

The goal is simple: Stop your logs from slowing down your app.

Most loggers either block on every write or lose type safety when you need structured logging. This package runs everything in a worker thread, batches automatically, and still gives you full TypeScript inference on what you log.

📌 Table of Contents

✨ Features

  • Zero Blocking: Every log goes through a worker thread – your main loop stays fast.
  • 🔒 Type-Safe: TypeScript infers the shape of your logs. No more any everywhere.
  • 🎯 Sink Pattern: Route logs to console, file, database, or your own custom destination.
  • 🔄 Smart Batching: Logs are grouped automatically for better I/O performance.
  • 🔔 Event-Driven: Listen to flush, close, and error events when you need them.

🔧 Installation

bun add @nowarajs/logger

You'll also need:

bun add @nowarajs/error @nowarajs/typed-event-emitter

⚙️ Usage

Basic Setup

Create a logger, attach a sink, and start logging:

import { Logger } from '@nowarajs/logger';
import { ConsoleLoggerSink } from '@nowarajs/logger/sinks';

// Create a logger and register a console sink
const logger = new Logger().registerSink('console', ConsoleLoggerSink);

// Log messages (always pass an object)
logger.info({ message: 'Application started' });
logger.warn({ message: 'This is a warning' });
logger.error({ message: 'An error occurred', code: 500 });
logger.debug({ action: 'debug_info', data: { foo: 'bar' } });
logger.log({ event: 'generic_log' });

// Close the logger when done
await logger.close();

Multiple Sinks

Need logs going to different places? Register as many sinks as you want:

import { Logger } from '@nowarajs/logger';
import { ConsoleLoggerSink, FileLoggerSink } from '@nowarajs/logger/sinks';

// Register multiple sinks
const logger = new Logger()
	.registerSink('console', ConsoleLoggerSink)
	.registerSink('file', FileLoggerSink, './app.log');

// Log to all sinks
logger.info({ message: 'This goes to console and file' });

// Log to specific sinks only
logger.error({ message: 'Only in file' }, ['file']);
logger.warn({ message: 'Only in console' }, ['console']);

await logger.close();

Custom Sinks

Have a weird logging requirement? Write your own sink:

import type { LoggerSink, LogLevels } from '@nowarajs/logger/types';

// Create a custom sink
class DatabaseSink implements LoggerSink {
	public async log(level: LogLevels, timestamp: number, object: unknown): Promise<void> {
		// Your custom logging logic
		await saveToDatabase({ level, timestamp, object });
	}
}

const logger = new Logger().registerSink('database', DatabaseSink);

logger.info({ event: 'user_created', userId: 42 });
await logger.close();

Type-Safe Logging

This is where it gets interesting. When you define typed sinks, TypeScript knows exactly what shape your logs need. No more guessing, no more runtime surprises.

Single Typed Sink

import type { LoggerSink, LogLevels } from '@nowarajs/logger/types';

// Define your log object type
interface UserLog {
	userId: number;
	action: string;
	timestamp?: Date;
}

// Create a typed sink
class UserLogSink implements LoggerSink<UserLog> {
	public async log(level: LogLevels, timestamp: number, object: UserLog): Promise<void> {
		console.log(`User ${object.userId} performed: ${object.action}`);
	}
}

const logger = new Logger().registerSink('userLog', UserLogSink);

// ✅ TypeScript requires the correct shape
logger.info({
	userId: 123,
	action: 'login'
});

// ❌ TypeScript error: Missing required property 'action'
logger.info({
	userId: 123
	// Error: Property 'action' is missing
});

Multiple Typed Sinks

When logging to multiple sinks at once, TypeScript creates an intersection of all types. You need to satisfy all of them:

interface UserLog {
	userId: number;
	action: string;
}

interface ApiLog {
	endpoint: string;
	method: string;
	statusCode: number;
}

class UserLogSink implements LoggerSink<UserLog> {
	public async log(level: LogLevels, timestamp: number, object: UserLog): Promise<void> {
		await saveUser(object);
	}
}

class ApiLogSink implements LoggerSink<ApiLog> {
	public async log(level: LogLevels, timestamp: number, object: ApiLog): Promise<void> {
		await saveApi(object);
	}
}

const logger = new Logger().registerSink('user', UserLogSink).registerSink('api', ApiLogSink);

// ✅ When using both sinks, you need BOTH types combined
logger.info(
	{
		userId: 123,
		action: 'api_call',
		endpoint: '/users',
		method: 'POST',
		statusCode: 201
	},
	['user', 'api']
); // Logs to both sinks

// ✅ When using only one sink, only that type is required
logger.warn(
	{
		userId: 456,
		action: 'failed_attempt'
	},
	['user']
); // Only UserLog type required

// ❌ TypeScript error: Missing api properties
logger.error(
	{
		userId: 789,
		action: 'error'
	},
	['user', 'api']
);

Mixing Typed and Untyped Sinks

When you mix typed sinks with untyped ones (like ConsoleLoggerSink which accepts unknown), things stay flexible:

interface DatabaseLog {
	query: string;
	duration: number;
}

class DatabaseLogSink implements LoggerSink<DatabaseLog> {
	public async log(level: LogLevels, timestamp: number, object: DatabaseLog): Promise<void> {
		await logToDatabase(object);
	}
}

const logger = new Logger()
	.registerSink('database', DatabaseLogSink)
	.registerSink('console', ConsoleLoggerSink); // Accepts unknown

// ✅ This works - intersection with unknown allows extra properties
logger.info(
	{
		query: 'SELECT * FROM users',
		duration: 123,
		customData: 'anything goes'
	},
	['database', 'console']
);

Error Handling

Things break. When they do, you'll want to know:

const logger = new Logger().registerSink('console', ConsoleLoggerSink);

// Listen for errors
logger.addListener('sinkError', (error) => {
	console.error('Logger error:', error.message);
});

logger.addListener('registerSinkError', (error) => {
	console.error('Failed to register sink:', error.message);
});

logger.info({ message: 'Safe to log' });
await logger.close();

Flushing and Closing

When you need to make sure everything is written before shutting down:

const logger = new Logger().registerSink('console', ConsoleLoggerSink);

logger.info({ message: 'First message' });
logger.info({ message: 'Second message' });

// Wait for all pending logs to be processed
await logger.flush();

// Close the logger and release resources (internally calls flush)
await logger.close();

Configuration

Fine-tune the batching and queue behavior:

const logger = new Logger({
	maxPendingLogs: 5000, // Max queued logs (default: 10,000)
	batchSize: 50, // Logs per batch (default: 50)
	batchTimeout: 100, // Ms before flushing batch (default: 0.1)
	maxMessagesInFlight: 100, // Max batches being processed (default: 100)
	autoEnd: true, // Auto-close on process exit (default: true)
	flushOnBeforeExit: true // Flush before exit (default: true)
});

📚 API Reference

Full docs: nowarajs.github.io/logger

⚖️ License

MIT – Use it however you want.

📧 Contact