@cvo/plugin-logger
v0.0.0
Published
Logger plugin for CVO Framework
Downloads
46
Readme
@cvo/plugin-logger
Structured logging plugin for CVO Framework, providing automatic request logging and leveled logging capabilities.
🚀 Features
- Automatic Request Logging: Records HTTP method, path, status code, and response time for every request.
- Leveled Logging: Supports
DEBUG,INFO,WARN, andERRORlevels. - DI Integration: Easily accessible via
InjectorRegistryor theuseLogger()hook. - Highly Customizable: Supports custom log implementations by implementing the
ILoggerinterface.
🛠 Configuration
Register the plugin in your cvo.config.ts:
import { defineConfig } from '@cvo/core';
import { LoggerPlugin, ConsoleLogger, LogLevel } from '@cvo/plugin-logger';
export default defineConfig({
plugins: [
new LoggerPlugin(new ConsoleLogger(LogLevel.INFO))
]
});🧠 Usage
Inside API Functions
import { useLogger } from '@cvo/server';
export async function myHandler() {
const logger = useLogger();
logger.info('Processing business logic...');
try {
// ...
} catch (err) {
logger.error('An error occurred:', err);
}
}Custom Logger Implementation
You can implement your own logger (e.g., to send logs to a remote server or write to a file):
import { ILogger, LoggerPlugin } from '@cvo/plugin-logger';
class MyRemoteLogger implements ILogger {
debug(msg: string) { /* ... */ }
info(msg: string) { /* ... */ }
warn(msg: string) { /* ... */ }
error(msg: string) { /* ... */ }
}
// Use in configuration
// new LoggerPlugin(new MyRemoteLogger())📊 Sample Output
[2024-01-29T12:00:00.000Z] [INFO] --> GET /api/users
[2024-01-29T12:00:00.005Z] [INFO] <-- GET /api/users 200 (5ms)