All packages
Core 234/mo
@munesoft/

logx

A tiny, zero-config logger for Node.js and browser. Structured, beautiful, and fast.

Installation

$ npm install @munesoft/logx

Documentation

@munesoft/logx

A tiny, zero-config logger for Node.js and browser.
Structured, beautiful, and fast โ€” without the complexity.

npm license bundle size


๐Ÿš€ Why logx?

Most logging libraries are too complex, too heavy, or require pages of setup.

logx is:

  • โ—†Simple โ€” feels exactly like console.log
  • โ—†Fast โ€” 1โ€“3M ops/s, zero heavy dependencies
  • โ—†Structured โ€” every log is internally a typed object
  • โ—†Beautiful โ€” colored output, clean stack traces, readable key=val pairs
  • โ—†Universal โ€” Node.js + browser, ESM + CJS

โšก Quick Start

bash
npm install @munesoft/logx
js
import logx from '@munesoft/logx';

logx('User created', { id: 123 });
// โœ” User created  id=123

Zero setup. Just import and go.


๐Ÿ”ฅ Features

| Feature | Description | |---|---| | Structured logging | Every log carries typed metadata | | Log levels | info, warn, error, debug with smart defaults | | Colored output | ANSI colors in Node, %c styling in browser | | JSON mode | Emit newline-delimited JSON for log aggregators | | Child loggers | Bind persistent context to a logger instance | | Transports | Route logs to any destination | | Filters | Programmatically suppress log levels | | Grouping | console.group-style nesting | | Error handling | Auto-detect Error objects, format stack traces | | Circular refs | Safe serialization โ€” never throws | | sprintf formatting | logx('User %s created', name) | | Zero config | Works out of the box | | Tree-shakeable | ESM, pure module | | TypeScript | Full .d.ts included |


๐Ÿ“Š Examples

Basic

js
logx('Hello world');
// โœ” Hello world

With Data

js
logx('User created', { id: 1, role: 'admin' });
// โœ” User created  id=1 role=admin

Log Levels

js
logx.info('Server started', { port: 3000 });
// โœ” Server started  port=3000

logx.warn('Rate limit approaching', { current: 90, limit: 100 });
// โš  Rate limit approaching  current=90 limit=100

logx.error('Database error', new Error('ECONNREFUSED'));
// โœ– Database error
//   Error: ECONNREFUSED
//     at ...

logx.debug('Cache miss', { key: 'users:123' });
// โ—† Cache miss  key=users:123

JSON Output

Perfect for production log aggregation (Datadog, Loki, CloudWatch, etc.):

js
logx.json('User created', { id: 1 });
// {"level":"info","message":"User created","timestamp":1714230000,"id":1}

Enable JSON mode globally:

js
logx.config({ json: true });

Child Loggers

Bind context to a logger โ€” every message from it automatically includes that context:

js
const apiLog = logx.child({ service: 'api', version: '2' });

apiLog('Request received', { path: '/users', method: 'GET' });
// โœ” Request received  service=api version=2 path=/users method=GET

apiLog.warn('Slow response', { ms: 2300 });
// โš  Slow response  service=api version=2 ms=2300

Nest child loggers:

js
const reqLog = apiLog.child({ requestId: 'abc-123' });
reqLog('Processing');
// โœ” Processing  service=api version=2 requestId=abc-123

Error Handling

Pass Error objects directly โ€” logx detects and formats them automatically:

js
try {
  await db.query(sql);
} catch (err) {
  logx.error('Query failed', err, { sql });
}
// โœ– Query failed  sql=SELECT...
//   Error: connection timeout
//     at Database.query (db.js:45:12)
//     at ...

Sprintf-Style Formatting

js
logx('User %s logged in from %s', username, ip);
logx('Processed %d items in %dms', count, duration);

Transports

Route logs to external systems:

js
logx.use((entry) => {
  fetch('/api/logs', {
    method: 'POST',
    body: JSON.stringify(entry),
  });
});

Return false to suppress default console output:

js
logx.use((entry) => {
  myLogger.write(entry);
  return false; // don't also print to console
});

Filters

js
// Suppress debug logs in staging
logx.filter((level) => level !== 'debug');

// Only log errors and warnings  
logx.filter((level) => ['error', 'warn'].includes(level));

Grouping

js
logx.group('Auth Flow', () => {
  logx('Checking credentials');
  logx('Validating token', { userId: 7 });
  logx('Session created');
});

Configuration

js
logx.config({
  level:     'debug',   // 'silent' | 'error' | 'warn' | 'info' | 'debug'
  pretty:    true,      // formatted output (vs raw)
  json:      false,     // force JSON mode
  timestamp: true,      // include timestamp in every log
  silent:    false,     // suppress all output
});

One-call silence:

js
logx({ silent: true });

Async Context

Attach context that will appear on all subsequent logs (useful in request middleware):

js
app.use((req, res, next) => {
  logx.context({ requestId: req.headers['x-request-id'] });
  next();
});

๐Ÿ†š Comparison

| Feature | console.log | pino | winston | logx | |---|---|---|---|---| | Zero config | โœ… | โŒ | โŒ | โœ… | | Structured | โŒ | โœ… | โœ… | โœ… | | Colored output | โŒ | requires plugin | requires plugin | โœ… | | JSON mode | โŒ | โœ… | โœ… | โœ… | | Child loggers | โŒ | โœ… | โœ… | โœ… | | Browser support | โœ… | โŒ | โŒ | โœ… | | TypeScript | โŒ | โœ… | โœ… | โœ… | | Bundle size | 0 kb | ~50 kb | ~100 kb | < 5 kb | | Setup required | none | low | high | none |


๐Ÿ“ฆ Install & Import

bash
npm install @munesoft/logx
# or
yarn add @munesoft/logx

ESM:

js
import logx from '@munesoft/logx';

CJS:

js
const logx = require('@munesoft/logx');

Browser (CDN):

html
<script type="module">
  import logx from 'https://cdn.skypack.dev/@munesoft/logx';
  logx('Hello from browser!');
</script>

โš™๏ธ Environment Awareness

logx automatically adapts:

| Environment | Default Level | Notes | |---|---|---| | NODE_ENV=development | debug | All levels visible | | NODE_ENV=production | info | Debug suppressed | | NO_COLOR=1 | any | ANSI colors disabled | | Browser (localhost) | debug | Dev mode detected | | Browser (production) | info | Minimal output |


๐ŸŽ๏ธ Performance

Benchmarked on Node.js 22, Apple M-series:

logx("Hello world")               1,006,100 ops/s   0.99 ยตs/op
logx("Msg", { id: 1 })            1,178,384 ops/s   0.85 ยตs/op
logx.info("Info", { x: 1 })       2,315,195 ops/s   0.43 ยตs/op
logx.json("Event", { ok: true })  1,507,092 ops/s   0.66 ยตs/op
child("Msg", { id: 1 })           2,290,502 ops/s   0.44 ยตs/op
logx with circular ref            1,582,889 ops/s   0.63 ยตs/op

๐Ÿ—๏ธ Architecture

src/
โ”œโ”€โ”€ core/       # Main logging engine, config, child loggers, transports
โ”œโ”€โ”€ format/     # Pretty (ANSI) + JSON + browser (%c) formatters
โ”œโ”€โ”€ color/      # Zero-dependency ANSI color utilities
โ”œโ”€โ”€ levels/     # Level constants and comparison logic  
โ”œโ”€โ”€ utils/      # sprintf, safe serialization, env detection, timestamps
โ””โ”€โ”€ browser/    # Browser-specific console adapter

No runtime dependencies. Zero external packages.


๐Ÿงช Testing

bash
npm test       # 27 unit tests
npm run bench  # performance benchmarks

๐Ÿ“„ License

MIT ยฉ munesoft


๐Ÿ” Keywords

nodejs logger, javascript logger, browser logger, structured logging, zero-config logger, console.log alternative, lightweight logger, pino alternative, winston alternative, fast logger, json logger, colored terminal output