Package Exports
- missionlog
Readme
missionlog
π missionlog is a lightweight, structured logging package designed for performance, flexibility, and ease of use. It works as a drop-in replacement for console.log
or ts-log
, and offers both log level filtering, optional tag filtering, and customizable output handlingβall in a tiny (~1KB) package.
β Fully Typed (TypeScript) β’ β ESM & CJS Support β’ β Zero Dependencies β’ β 100% Coverage
β¨ Why Use missionlog
?
- Drop-in Replacement for
console.log
&ts-log
β Start using it instantly! - Seamless Upgrade to Tagged Logging β Reduce log clutter and focus on what's important.
- Configurable Log Levels β Adjust visibility for log level and tags at runtime.
- Customizable Output β Send logs anywhere: console, JSON, cloud services.
- Structured Logging Support β Enhanced callbacks with timestamp and typed message data.
- Blazing Fast Performance β O(1) log level lookups with advanced level caching.
- TypeScript-First β Full type safety with LogMessage and LogConfig interfaces.
- Chainable API β All methods return the logger instance for method chaining.
- Works Everywhere β Browser, Node.js, Firebase, AWS Lambda etc.
π¦ Installing
npm i missionlog
π Getting Started
Basic Usage
Missionlog works as a drop-in replacement for console.log:
import { log } from 'missionlog';
// Works just like console.log
log.info('Hello, world!');
log.warn('Warning message');
log.error('Error occurred!');
// Chainable API for fluent logging
log.debug('Starting process').info('Process step 1 complete').warn('Process running slowly');
π‘ Usage Examples
Using Tags for Categorization
import { log, tag, LogLevel, DEFAULT_TAG } from 'missionlog';
// Configure logging levels for different tags
log.init({
network: LogLevel.DEBUG,
ui: LogLevel.INFO,
[DEFAULT_TAG]: LogLevel.WARN, // Default level for uncategorized logs
});
// Log with tags
log.debug(tag.network, 'Connection established');
log.info(tag.ui, 'Component rendered');
// Untagged logs use the DEFAULT_TAG level
log.debug("This won't be logged because DEFAULT_TAG is WARN");
log.error('This will be logged because ERROR > WARN');
Custom Log Handler (with Chalk)
import { log, LogLevel, LogLevelStr, LogCallbackParams } from 'missionlog';
import chalk from 'chalk';
// Create a custom log handler
function createCustomHandler() {
const logConfig: Record<LogLevelStr, { color: (text: string) => string; method: (...args: unknown[]) => void }> = {
ERROR: { color: chalk.red, method: console.error },
WARN: { color: chalk.yellow, method: console.warn },
INFO: { color: chalk.blue, method: console.log },
DEBUG: { color: chalk.magenta, method: console.log },
TRACE: { color: chalk.cyan, method: console.log },
OFF: { color: () => '', method: () => {} },
};
return (level: LogLevelStr, tag: string, message: unknown, params: unknown[]) => {
const { method, color } = logConfig[level];
const logLine = `[${color(level)}] ${tag ? tag + ' - ' : ''}${message}`;
method(logLine, ...params);
};
}
// Initialize with custom handler
log.init({ network: LogLevel.INFO, [DEFAULT_TAG]: LogLevel.INFO }, createCustomHandler());
// Enhanced structured logging with timestamps and typed data
log.setEnhancedCallback((params: LogCallbackParams) => {
const { level, tag, message, timestamp, params: extraParams } = params;
console.log(`[${timestamp.toISOString()}] [${level}] ${tag ? tag + ' - ' : ''}${message}`, ...extraParams);
});
// Check if specific levels are enabled before performing expensive operations
if (log.isDebugEnabled('network')) {
// Only perform this expensive operation if DEBUG logs for 'network' will be shown
const stats = getNetworkStatistics(); // Example of an expensive operation
log.debug(tag.network, 'Network statistics', stats);
}
// Similarly for TRACE level
if (log.isTraceEnabled('ui')) {
// Avoid expensive calculations when trace logging is disabled
const detailedMetrics = calculateDetailedRenderMetrics();
log.trace(tag.ui, 'UI rendering detailed metrics', detailedMetrics);
}
// The general method is still available for other log levels
if (log.isLevelEnabled(LogLevel.WARN, 'security')) {
const securityCheck = performSecurityAudit();
log.warn(tag.security, 'Security audit results', securityCheck);
}
π API Reference
Log Methods
log.trace(messageOrTag?, ...params)
- Lowest verbosity levellog.debug(messageOrTag?, ...params)
- Detailed debugging informationlog.info(messageOrTag?, ...params)
- Notable but expected eventslog.log(messageOrTag?, ...params)
- Alias for info()log.warn(messageOrTag?, ...params)
- Potential issues or warningslog.error(messageOrTag?, ...params)
- Error conditions
Configuration
log.init(config?, callback?)
- Configure log levels and custom handlerlog.setEnhancedCallback(callback)
- Set structured logging callback with extended parameterslog.isLevelEnabled(level, tag?)
- Check if a specific level would be logged for a taglog.isDebugEnabled(tag?)
- Convenience method to check if DEBUG level is enabled for a taglog.isTraceEnabled(tag?)
- Convenience method to check if TRACE level is enabled for a taglog.reset()
- Clear all tag registrations and configurations
Log Levels (in order of verbosity)
LogLevel.TRACE
- Most verboseLogLevel.DEBUG
LogLevel.INFO
- Default levelLogLevel.WARN
LogLevel.ERROR
LogLevel.OFF
- No logs
πΌοΈ Example Output
π€ Contributing
Contributions, issues, and feature requests are welcome! Feel free to check issues page or submit a pull request.
π License
MIT License Β© 2019-2025 Ray Martone
π Install missionlog
today and make logging clean, structured, and powerful!