@libster/loggy
v1.0.3
Published
A clean and formatted logging library for Node.js applications with redaction and Express middleware support
Maintainers
Readme
Loggy
A clean and formatted logging library for Node.js applications with automatic redaction, Express middleware support, and CLI tools.
Features
- 🎨 Beautiful formatting - Colored, human-readable logs with timestamps and icons
- 🔒 Automatic redaction - Sensitive data like passwords and tokens are automatically redacted
- 📊 Multiple output modes - Pretty format for development, JSON for production
- 🚀 Express middleware - Built-in request logging middleware
- 🛠️ CLI tool - Format and clean logs from files or stdin
- 📝 TypeScript support - Full TypeScript definitions included
- ⚡ Zero dependencies - Lightweight with minimal dependencies
Installation
Using npm
npm install @libster/loggyUsing yarn
yarn add @libster/loggyUsing pnpm
pnpm add @libster/loggyQuick Start
Basic Usage
import { createLogger } from '@libster/loggy';
const logger = createLogger();
logger.info('User logged in', { userId: 123, username: 'john' });
logger.warn('Rate limit approaching', { requests: 95, limit: 100 });
logger.error('Database connection failed', { error: 'Connection timeout' });Output Example
[2025-09-12T10:15:00.000Z] INFO ℹ️ User logged in
Metadata: {
"userId": 123,
"username": "john"
}
[2025-09-12T10:15:01.000Z] WARN ⚠️ Rate limit approaching
Metadata: {
"requests": 95,
"limit": 100
}
[2025-09-12T10:15:02.000Z] ERROR ❌ Database connection failed
Metadata: {
"error": "Connection timeout"
}With Configuration
import { createLogger } from '@libster/loggy';
const logger = createLogger({
mode: 'json', // 'pretty' or 'json'
level: 'debug', // 'debug', 'info', 'warn', 'error'
redact: ['password', 'token', 'secret'], // Custom redaction keys
timestamp: true // Include timestamps
});
logger.info('User login', {
user: 'john',
password: 'secret123' // Will be redacted as "****"
});API Reference
Logger Methods
logger.info(message, metadata?)
Log an info message with optional metadata.
logger.warn(message, metadata?)
Log a warning message with optional metadata.
logger.error(message, metadata?, error?)
Log an error message with optional metadata and Error object.
logger.debug(message, metadata?)
Log a debug message with optional metadata.
Legacy Methods
For backward compatibility, these methods are also available:
logger.logInfo(message, metadata?)logger.logWarn(message, metadata?)logger.logError(message, metadata?, error?)logger.logDebug(message, metadata?)
Configuration Options
interface LoggerConfig {
mode?: 'pretty' | 'json'; // Output format
level?: 'debug' | 'info' | 'warn' | 'error'; // Minimum log level
redact?: string[]; // Custom keys to redact
timestamp?: boolean; // Include timestamps
}Express Middleware
Loggy includes a built-in Express middleware for request logging:
import express from 'express';
import { loggerMiddleware } from '@libster/loggy';
const app = express();
// Basic usage
app.use(loggerMiddleware());
// With configuration
app.use(loggerMiddleware({
mode: 'json',
skip: (req, res) => req.url === '/health' // Skip health checks
}));
app.get('/users', (req, res) => {
res.json({ users: [] });
});This will log requests like:
[2023-12-01T10:15:00Z] INFO GET /users 200 45msMiddleware Configuration
interface MiddlewareConfig extends LoggerConfig {
skip?: (req: Request, res: Response) => boolean;
}CLI Tool
Loggy includes a CLI tool for formatting logs from files or stdin:
Basic Usage
# Format logs from stdin
cat logs.json | npx @libster/loggy --pretty
# Format logs from a file
npx @libster/loggy --file logs.json --pretty
# Output as JSON
npx @libster/loggy --file logs.json --json
# Custom redaction keys
npx @libster/loggy --file logs.json --redact password,token,secret --prettyCLI Options
-p, --pretty- Output in pretty format (default)-j, --json- Output in JSON format-r, --redact <keys>- Comma-separated list of keys to redact-f, --file <path>- Read from file instead of stdin--no-timestamp- Disable timestamps-l, --level <level>- Minimum log level (debug, info, warn, error)
Examples
# Pretty format with custom redaction
cat app.log | npx @libster/loggy --pretty --redact password,api_key
# JSON format for log aggregation
npx @libster/loggy --file app.log --json --level warn
# Process multiple files
for file in logs/*.log; do
npx @libster/loggy --file "$file" --pretty
doneAutomatic Redaction
Loggy automatically redacts sensitive data in your logs. By default, it redacts:
passwordtokensecretapikeyapi_keyauthauthorization
Example
const logger = createLogger();
logger.info('User login', {
username: 'john',
password: 'secret123', // Redacted as "****"
email: '[email protected]',
token: 'abc123' // Redacted as "****"
});Output:
[2023-12-01T10:15:00Z] INFO ℹ️ User login
Metadata: {
"username": "john",
"password": "****",
"email": "[email protected]",
"token": "****"
}Custom Redaction
const logger = createLogger({
redact: ['password', 'token', 'custom_secret']
});Error Handling
Loggy provides enhanced error formatting with stack trace highlighting:
const logger = createLogger();
try {
throw new Error('Something went wrong');
} catch (error) {
logger.error('Operation failed', { operation: 'user_create' }, error);
}This will:
- Highlight your application code in stack traces
- Fade out Node.js internal lines
- Include error name, message, and formatted stack trace
Output Modes
Pretty Mode (Development)
[2023-12-01T10:15:00Z] INFO ℹ️ User logged in
Metadata: {
"userId": 123,
"username": "john"
}JSON Mode (Production)
{"timestamp":"2023-12-01T10:15:00.000Z","level":"info","message":"User logged in","metadata":{"userId":123,"username":"john"}}Log Levels
Loggy supports four log levels with filtering:
debug- Detailed information for debugginginfo- General information about application flowwarn- Warning messages for potentially harmful situationserror- Error events that might still allow the application to continue
Set the minimum log level to filter out less important messages:
const logger = createLogger({ level: 'warn' });
logger.debug('This will not be logged');
logger.info('This will not be logged');
logger.warn('This will be logged');
logger.error('This will be logged');TypeScript Support
Loggy is written in TypeScript and includes full type definitions:
import { createLogger, Logger, LoggerConfig } from '@libster/loggy';
const config: LoggerConfig = {
mode: 'json',
level: 'info'
};
const logger: Logger = createLogger(config);Examples
Express Application
import express from 'express';
import { createLogger, loggerMiddleware } from '@libster/loggy';
const app = express();
const logger = createLogger({ mode: 'json' });
app.use(loggerMiddleware({ mode: 'json' }));
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
logger.info('Fetching user', { userId });
// Your logic here
res.json({ id: userId, name: 'John' });
});
app.listen(3000, () => {
logger.info('Server started', { port: 3000 });
});Error Handling
import { createLogger } from '@libster/loggy';
const logger = createLogger();
async function processUser(userId: string) {
try {
logger.info('Processing user', { userId });
// Simulate some work
if (Math.random() > 0.5) {
throw new Error('Processing failed');
}
logger.info('User processed successfully', { userId });
} catch (error) {
logger.error('Failed to process user', { userId }, error as Error);
throw error;
}
}Custom Redaction
import { createLogger } from '@libster/loggy';
const logger = createLogger({
redact: ['password', 'ssn', 'credit_card', 'api_key']
});
logger.info('Payment processed', {
userId: 123,
amount: 99.99,
credit_card: '4111-1111-1111-1111', // Redacted
api_key: 'sk-1234567890' // Redacted
});Development
Building
npm run buildTesting
npm test
npm run test:coverageLinting
npm run lint
npm run lint:fixFormatting
npm run format
npm run format:checkContributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Setup
git clone https://github.com/vijoy-paul/libster-loggy.git
cd loggy
npm install
npm run build
npm testLicense
MIT License - see LICENSE file for details.
Changelog
1.0.0
- Initial release
- Core logging functionality
- Automatic redaction
- Express middleware
- CLI tool
- TypeScript support
- Comprehensive test suite
