fn-logger-decorator
v1.0.3
Published
A lightweight decorator-based logger for error tracking and debugging
Readme
fn-logger-decorator
A lightweight TypeScript/JavaScript decorator to automatically log function errors and metadata to a file, console, or custom transport — ideal for observability and debugging in both development and production environments.
✨ Features
✅ Works with async/sync functions
🧠 Automatically logs:
Timestamp
Function name
Duration of execution
Error message
Stack trace (only in development)
Function arguments (optional)
🔐 Environment-aware (safe logs in production)
🧩 Pluggable transport: file, console, or custom
🛠️ Built with TypeScript, supports JavaScript (via Babel)
📦 Installation
npm install fn-logger-decorator
-- npm i node-ts to run this package
For TypeScript projects:
npm install reflect-metadata
Ensure experimentalDecorators and emitDecoratorMetadata are enabled in tsconfig.json.
🚀 Usage
- Basic Usage
import 'reflect-metadata'; import { LogErrors } from 'fn-logger-decorator';
class DemoService { @LogErrors() async riskyOperation(input: string) { if (input === 'fail') throw new Error('Something went wrong'); return input.toUpperCase(); } }
- Customize Options
@LogErrors({ logDir: 'custom-logs', // default is "logs" includeArgs: true, // include function args (default: true) transport: 'console', // 'file' | 'console' })
- Custom Transport
@LogErrors({ customTransport: (entry) => { sendToMonitoringService(entry); // your logic here } })
📁 Output Example (file log)
Each log line is a JSON object:
{ "timestamp": "2025-07-10T09:45:23.123Z", "function": "riskyOperation", "durationMs": 12, "environment": "production", "args": ["fail"], "error": { "message": "Something went wrong" } }
🌐 Environment Support
Set NODE_ENV=production to strip stack traces.
Use cross-env for cross-platform scripts:
npx cross-env NODE_ENV=production ts-node yourfile.ts
✅ Requirements
Node.js v14+
TypeScript 4.0+
🧪 Testing
npm run test
🔒 Best Practices
Avoid logging PII in production
Use custom transport for sending logs to remote servers (e.g., Sentry, Datadog)
Wrap decorators around meaningful boundaries (e.g., API routes, service methods)
🧱 Future Roadmap
📃 License
MIT © 2025 Gowtham SSR
Feel free to contribute or submit feature requests on GitHub!
