traceperf
Version:
High-performance function execution tracking and monitoring for Node.js
182 lines • 5.41 kB
JavaScript
import { ConfigManager } from './config';
import { ExecutionTracker } from '../trackers/execution';
import { PerformanceMonitor } from '../trackers/performance';
import { CliFormatter } from '../formatters/cli';
/**
* Console transport for outputting logs to the console
*/
class ConsoleTransport {
/**
* Write a log entry to the console
*
* @param entry - The formatted log entry
*/
write(entry) {
console.log(entry);
}
}
/**
* Main logger class that implements the ILogger interface
*/
export class Logger {
/**
* Create a new Logger instance
*
* @param config - Logger configuration
*/
constructor(config = {}) {
this._indentLevel = 0;
this._config = new ConfigManager(config);
// Set up trackers
this._executionTracker = new ExecutionTracker({
defaultThreshold: this._config.getPerformanceThreshold(),
});
this._performanceMonitor = new PerformanceMonitor({
defaultThreshold: this._config.getPerformanceThreshold(),
});
// Set up transports
this._transports = config.transports || [new ConsoleTransport()];
// Set up formatters
this._formatters = config.formatters || [
new CliFormatter({
colorEnabled: this._config.isColorEnabled(),
indentSize: this._config.getIndentSize(),
}),
];
}
/**
* Log an informational message
*
* @param message - The message to log
* @param args - Additional arguments to include in the log
*/
info(message, ...args) {
this.log('info', message, args);
}
/**
* Log a warning message
*
* @param message - The message to log
* @param args - Additional arguments to include in the log
*/
warn(message, ...args) {
this.log('warn', message, args);
}
/**
* Log an error message
*
* @param message - The message to log
* @param args - Additional arguments to include in the log
*/
error(message, ...args) {
this.log('error', message, args);
}
/**
* Log a debug message
*
* @param message - The message to log
* @param args - Additional arguments to include in the log
*/
debug(message, ...args) {
// Skip debug logs in production mode
if (this.getMode() === 'prod') {
return;
}
this.log('debug', message, args);
}
/**
* Start a new log group with the given label
*
* @param label - The label for the group
*/
group(label) {
this.info(label);
this._indentLevel++;
}
/**
* End the current log group
*/
groupEnd() {
if (this._indentLevel > 0) {
this._indentLevel--;
}
}
/**
* Set the operational mode for the logger
*
* @param mode - The mode to set
*/
setMode(mode) {
this._config.setMode(mode);
}
/**
* Get the current operational mode
*
* @returns The current mode
*/
getMode() {
return this._config.getMode();
}
/**
* Track the execution of a function
*
* @param fn - The function to track
* @param options - Options for tracking
* @returns The return value of the tracked function
*/
track(fn, options) {
var _a;
const silent = (_a = options === null || options === void 0 ? void 0 : options.silent) !== null && _a !== void 0 ? _a : false;
// Track the function execution
const result = this._executionTracker.track(fn, options);
// Log the execution flow if not silent
if (!silent) {
const flowChart = this._executionTracker.generateFlowChart();
// Log the flow chart directly to console to preserve ASCII art
// Skip formatters to avoid messing up the ASCII art
console.log(flowChart);
}
return result;
}
/**
* Create a trackable version of a function
*
* This is a helper method to create a tracked version of a function
* that can be used for nested function tracking.
*
* @param fn - The function to make trackable
* @param options - Options for tracking
* @returns A tracked version of the function
*/
createTrackable(fn, options) {
return this._executionTracker.createTrackable(fn, options);
}
/**
* Internal method to log a message
*
* @param level - The log level
* @param message - The message to log
* @param args - Additional arguments
*/
log(level, message, args) {
// Check if this log level should be displayed
if (!this._config.shouldLog(level)) {
return;
}
// Create metadata for the log entry
const meta = {
timestamp: new Date(),
level,
indentLevel: this._indentLevel,
};
// Format the log entry using each formatter
for (const formatter of this._formatters) {
const formattedEntry = formatter.format(level, message, args, meta);
// Send to each transport
for (const transport of this._transports) {
transport.write(formattedEntry);
}
}
}
}
//# sourceMappingURL=logger.js.map