@xenral/logpro
v0.4.1
Published
A minimal, structured logging utility for TypeScript projects
Downloads
4
Maintainers
Readme
LogPro
A minimalistic, structured logging utility for TypeScript projects.
Features
- 🔍 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)
Installation
npm install @xenral/logproor
yarn add @xenral/logproQuick Start
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);
}Advanced Usage
Creating Named Loggers
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' });Customizing Log Level
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');Changing Output Format
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);Creating Child Loggers with Context
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');
}Environment-Aware Logger
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 levelSending Logs to Telegram (Optional)
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
API Reference
Log Levels
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
Core Methods
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
Configuration Methods
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
License
MIT
