@yassine-essiyedali/autologging
v0.1.1
Published
A powerful library for cutomizable auto-logging and functions auto-tracing.
Maintainers
Readme
autologging
A powerful TypeScript/JavaScript library for customizable auto-logging and function auto-tracing with support for both synchronous and asynchronous functions.
Features
✨ Function Wrapper - Wrap any function with automatic logging
🎯 Method Decorator - Decorate class methods with @tracable
🏗️ Class Decorator - Trace entire classes with @tracableClass
⚡ Async Support - Works seamlessly with both sync and async functions
⏱️ Execution Time Tracking - Automatically measures and logs execution time
📊 Parameter Logging - Logs function parameters automatically
Installation
npm install @yassine-essiyedali/autologgingUsage
Function Wrapper
Wrap any function to add automatic tracing:
import { trace } from 'autologging';
function add(a: number, b: number): number {
return a + b;
}
const tracedAdd = trace(add);
tracedAdd(5, 3);Output:
Function started
parameters
arg0 = 5
arg1 = 3
Execution time: 0.123
Function endedMethod Decorator
Use @tracable to automatically trace class methods:
import { tracable } from 'autologging';
class Calculator {
@tracable()
multiply(a: number, b: number): number {
return a * b;
}
@tracable()
async divide(a: number, b: number): Promise<number> {
return a / b;
}
}
const calc = new Calculator();
calc.multiply(4, 5);
await calc.divide(10, 2);Class Decorator
Trace specific methods in a class using @tracableClass:
import { tracableClass } from 'autologging';
@tracableClass(['processData', 'calculate'])
class DataProcessor {
processData(data: string): string {
return data.toUpperCase();
}
calculate(x: number): number {
return x * 2;
}
// This method won't be traced
helperMethod(): void {
console.log('Helper');
}
}
const processor = new DataProcessor();
processor.processData('hello'); // Traced
processor.calculate(10); // Traced
processor.helperMethod(); // Not tracedTypeScript Configuration
Enable decorators in your tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}API Reference
trace<T, R>(fn: (...args: T) => R)
Wraps a function with automatic logging and execution time tracking.
Parameters:
fn- The function to wrap
Returns:
- A wrapped version of the function with tracing enabled
@tracable()
Method decorator that adds automatic tracing to class methods.
Usage:
@tracable()
methodName() { ... }@tracableClass(functionsToTrace: string[])
Class decorator that traces specific methods in a class.
Parameters:
functionsToTrace- Array of method names to trace
Usage:
@tracableClass(['method1', 'method2'])
class MyClass { ... }Customization
The library uses constants from LoggingConstants which include:
FUNCTION_STARTED- "Function started"FUNCTION_ENDED- "Function ended"PARAMETERS_LABEL- "parameters"EXECUTION_TIME- "Execution time"
Examples
Async Function Support
import { trace } from 'autologging';
async function fetchData(url: string): Promise<any> {
const response = await fetch(url);
return response.json();
}
const tracedFetch = trace(fetchData);
await tracedFetch('https://api.example.com/data');Multiple Methods
import { tracable } from 'autologging';
class UserService {
@tracable()
async getUser(id: number): Promise<User> {
// ... fetch user
}
@tracable()
async updateUser(id: number, data: UserData): Promise<User> {
// ... update user
}
}Requirements
- TypeScript 5.0+
- Node.js 14+
License
MIT © Yassine ESSIYEDALI
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Issues
Found a bug or have a feature request? Open an issue
