A minimalistic, structured logging utility for TypeScript projects.
- π Structured logging - JSON output format for easy parsing and analysis
- π Multiple log levels - debug, info, warn, error, and fatal
- π§© Context support - Attach consistent metadata to your logs
- π Environment-aware - Automatically adjusts configuration based on environment
- π¨ Pretty formatting - Human-readable output with optional colors for development
- π§ Customizable - Easily configurable to match your needs
- π± Zero dependencies - Core logging with no external dependencies
- π² Optional transports - Extend with transports like Telegram (opt-in only)
npm install @xenral/logpro
or
yarn add @xenral/logpro
import { logger } from '@xenral/logpro';
// Basic logging
logger.info('Application started');
logger.warn('Connection timeout, retrying...');
logger.error('Failed to connect to database');
// Adding context
logger.info('User logged in', { userId: '123', role: 'admin' });
// Logging errors with stack traces
try {
throw new Error('Something went wrong');
} catch (error) {
logger.error('Operation failed', { operation: 'data-import' }, error);
}
import { getLogger } from '@xenral/logpro';
const dbLogger = getLogger('database');
const authLogger = getLogger('auth');
dbLogger.info('Connected to database');
authLogger.warn('Invalid login attempt', { username: 'user123', ip: '192.168.1.1' });
import { getLogger, LogLevel } from '@xenral/logpro';
const logger = getLogger('app')
.setLevel(LogLevel.DEBUG);
// This will output in development, but not in production
logger.debug('Debugging information');
import { getLogger } from '@xenral/logpro';
// JSON format (default in production)
const jsonLogger = getLogger('api')
.useJsonFormat();
// Pretty format with colors (default in development)
const prettyLogger = getLogger('ui')
.usePrettyFormat(true);
import { getLogger } from '@xenral/logpro';
const logger = getLogger('requestHandler');
// Create a child logger for a specific request
function handleRequest(req) {
const requestLogger = logger.child({
requestId: req.id,
path: req.path,
method: req.method
});
requestLogger.info('Request received');
// All logs from requestLogger will include the request context
requestLogger.debug('Processing request');
}
import { createEnvLogger } from '@xenral/logpro';
// Automatically configures based on NODE_ENV
const logger = createEnvLogger('app');
// In development: Pretty output with colors, DEBUG level
// In test: Default output, WARN level
// In production: JSON output, INFO level
Optionally send important logs directly to a Telegram channel for real-time monitoring:
import { getLogger, LogLevel, TelegramTransport } from '@xenral/logpro';
// First install the required dependency
// npm install node-telegram-bot-api
// Create a logger
const logger = getLogger('app');
// Create a Telegram transport
const telegramTransport = new TelegramTransport({
token: 'YOUR_TELEGRAM_BOT_TOKEN',
chatId: 'YOUR_CHAT_ID',
minLevel: LogLevel.ERROR, // Only send ERROR and FATAL logs
// Optional: Add a filter for specific logs
filter: (entry) => entry.context.important === true
});
// Add the transport to the logger
logger.addTransport(telegramTransport);
// Regular logs will go to console only
logger.info('Application started');
// Error logs will go to both console and Telegram
logger.error('Database connection failed', {
important: true,
dbHost: 'production-db-1'
});
To set up the Telegram integration:
- Install the optional dependency:
npm install node-telegram-bot-api
- Create a bot using BotFather and get the token
- Add the bot to your channel or group
- Get the chat ID (you can use the @username_to_id_bot)
- Configure the TelegramTransport with your token and chat ID
LogLevel.DEBUG
- Detailed information for debuggingLogLevel.INFO
- Informational messages highlighting progressLogLevel.WARN
- Potentially harmful situationsLogLevel.ERROR
- Error events that might still allow the application to continueLogLevel.FATAL
- Severe error events that lead to application terminationLogLevel.SILENT
- Turns off all logging
debug(message, context?, error?)
- Log at debug levelinfo(message, context?, error?)
- Log at info levelwarn(message, context?, error?)
- Log at warn levelerror(message, context?, error?)
- Log at error levelfatal(message, context?, error?)
- Log at fatal level
setLevel(level)
- Set minimum log leveluseJsonFormat()
- Use JSON output formatusePrettyFormat(enableColors?)
- Use human-readable output formatsetFormatter(formatter)
- Set custom formatter functionchild(context)
- Create a child logger with additional context
MIT