|
| 1 | +--- |
| 2 | +title: Overview |
| 3 | +description: Flexible and extensible logging system for Vercube applications |
| 4 | +--- |
| 5 | + |
| 6 | +The Logger module provides a powerful, flexible logging system for Vercube applications. Built around a provider-based architecture, it allows you to log messages at different levels (debug, info, warn, error) and output them to multiple destinations simultaneously through pluggable drivers. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +::code-group |
| 11 | + ```bash [pnpm] |
| 12 | + $ pnpm add @vercube/logger |
| 13 | + ``` |
| 14 | + ```bash [npm] |
| 15 | + $ npm install @vercube/logger |
| 16 | + ``` |
| 17 | + ```bash [bun] |
| 18 | + $ bun install @vercube/logger |
| 19 | + ``` |
| 20 | +:: |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +::steps |
| 25 | +### Register Logger in Container |
| 26 | + |
| 27 | +Set up the logger in your DI container and configure providers. This is typically done once during application bootstrap. |
| 28 | + |
| 29 | +```ts [src/container.ts] |
| 30 | +import { Container } from '@vercube/di'; |
| 31 | +import { Logger, BaseLogger, ConsoleProvider } from '@vercube/logger'; |
| 32 | + |
| 33 | +export function setupContainer(container: Container): void { |
| 34 | + // Bind BaseLogger implementation to Logger interface |
| 35 | + container.bind(Logger, BaseLogger); |
| 36 | + |
| 37 | + // Configure the logger |
| 38 | + container.get(Logger).configure({ |
| 39 | + logLevel: 'info', |
| 40 | + providers: [ |
| 41 | + { |
| 42 | + name: 'console', |
| 43 | + provider: ConsoleProvider, |
| 44 | + logLevel: 'debug' // This provider logs everything |
| 45 | + } |
| 46 | + ] |
| 47 | + }); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Inject Logger into Services |
| 52 | + |
| 53 | +Use dependency injection to access the logger in your services. Log important operations and errors for debugging. |
| 54 | + |
| 55 | +```ts [src/services/UserService.ts] |
| 56 | +import { Inject } from '@vercube/di'; |
| 57 | +import { Logger } from '@vercube/logger'; |
| 58 | + |
| 59 | +export class UserService { |
| 60 | + @Inject(Logger) |
| 61 | + private logger!: Logger; |
| 62 | + |
| 63 | + async createUser(data: CreateUserDto) { |
| 64 | + this.logger.debug('Creating user with data:', data); |
| 65 | + |
| 66 | + try { |
| 67 | + const user = await this.database.createUser(data); |
| 68 | + this.logger.info('User created successfully', { userId: user.id }); |
| 69 | + return user; |
| 70 | + } catch (error) { |
| 71 | + this.logger.error('Failed to create user', error); |
| 72 | + throw error; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + async findUser(id: string) { |
| 77 | + this.logger.debug(`Looking up user: ${id}`); |
| 78 | + const user = await this.database.findUser(id); |
| 79 | + |
| 80 | + if (!user) { |
| 81 | + this.logger.warn(`User not found: ${id}`); |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + return user; |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Use Logger in Controllers |
| 91 | + |
| 92 | +Inject the logger into controllers to track incoming requests. This helps with monitoring and debugging API endpoints. |
| 93 | + |
| 94 | +```ts [src/controllers/UserController.ts] |
| 95 | +import { Controller, Get, Post } from '@vercube/core'; |
| 96 | +import { Inject } from '@vercube/di'; |
| 97 | +import { Logger } from '@vercube/logger'; |
| 98 | + |
| 99 | +@Controller('/users') |
| 100 | +export class UserController { |
| 101 | + @Inject(Logger) |
| 102 | + private logger!: Logger; |
| 103 | + |
| 104 | + @Inject(UserService) |
| 105 | + private userService!: UserService; |
| 106 | + |
| 107 | + @Post('/') |
| 108 | + async create(req: Request) { |
| 109 | + this.logger.info('POST /users - Creating new user'); |
| 110 | + |
| 111 | + const data = await req.json(); |
| 112 | + const user = await this.userService.createUser(data); |
| 113 | + |
| 114 | + return Response.json(user); |
| 115 | + } |
| 116 | + |
| 117 | + @Get('/:id') |
| 118 | + async findOne(req: Request, params: { id: string }) { |
| 119 | + this.logger.debug(`GET /users/${params.id}`); |
| 120 | + |
| 121 | + const user = await this.userService.findUser(params.id); |
| 122 | + |
| 123 | + if (!user) { |
| 124 | + return new Response('Not found', { status: 404 }); |
| 125 | + } |
| 126 | + |
| 127 | + return Response.json(user); |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | +:: |
| 132 | + |
| 133 | + |
| 134 | +## Core Concepts |
| 135 | + |
| 136 | +### Logger |
| 137 | + |
| 138 | +The Logger is the main interface for logging messages throughout your application. It provides four logging methods corresponding to different severity levels: |
| 139 | + |
| 140 | +- **debug()** - Detailed information useful for debugging |
| 141 | +- **info()** - General operational information |
| 142 | +- **warn()** - Warnings about potentially harmful situations |
| 143 | +- **error()** - Error messages for serious problems |
| 144 | + |
| 145 | +### Providers |
| 146 | + |
| 147 | +Providers (also called drivers) are responsible for processing and outputting log messages. Each provider can output logs to a different destination (console, file, external service, etc.) with its own formatting and configuration. |
| 148 | + |
| 149 | +### Log Levels |
| 150 | + |
| 151 | +Vercube supports four hierarchical log levels: |
| 152 | + |
| 153 | +``` |
| 154 | +debug (1) → info (2) → warn (3) → error (4) |
| 155 | +``` |
| 156 | + |
| 157 | +When you set a log level, only messages at that level or higher are processed: |
| 158 | + |
| 159 | +- Set to `debug` - All messages are logged |
| 160 | +- Set to `info` - Info, warn, and error are logged |
| 161 | +- Set to `warn` - Only warn and error are logged |
| 162 | +- Set to `error` - Only error messages are logged |
| 163 | + |
| 164 | +### IOC Container Integration |
| 165 | + |
| 166 | +The Logger integrates seamlessly with Vercube's dependency injection system. You register it in the IOC container and inject it wherever needed using the `@Inject` decorator. |
| 167 | + |
| 168 | +## Configuration |
| 169 | + |
| 170 | +### Basic Configuration |
| 171 | + |
| 172 | +Configure the logger when setting up your application: |
| 173 | + |
| 174 | +```ts |
| 175 | +container.get(Logger).configure({ |
| 176 | + // Global log level - applies to all providers unless overridden |
| 177 | + logLevel: 'info', |
| 178 | + |
| 179 | + // List of providers to use |
| 180 | + providers: [ |
| 181 | + { |
| 182 | + name: 'console', |
| 183 | + provider: ConsoleProvider, |
| 184 | + // Optional: Override global log level for this provider |
| 185 | + logLevel: 'debug' |
| 186 | + } |
| 187 | + ] |
| 188 | +}); |
| 189 | +``` |
| 190 | + |
| 191 | +### Multiple Providers |
| 192 | + |
| 193 | +You can use multiple providers simultaneously to output logs to different destinations: |
| 194 | + |
| 195 | +```ts |
| 196 | +import { ConsoleProvider, JSONProvider } from '@vercube/logger'; |
| 197 | + |
| 198 | +container.get(Logger).configure({ |
| 199 | + logLevel: 'info', |
| 200 | + providers: [ |
| 201 | + // Console output for development |
| 202 | + { |
| 203 | + name: 'console', |
| 204 | + provider: ConsoleProvider, |
| 205 | + logLevel: 'debug' // Show everything in console |
| 206 | + }, |
| 207 | + // JSON output for log aggregation |
| 208 | + { |
| 209 | + name: 'json', |
| 210 | + provider: JSONProvider, |
| 211 | + logLevel: 'warn' // Only log warnings and errors to JSON |
| 212 | + } |
| 213 | + ] |
| 214 | +}); |
| 215 | +``` |
| 216 | + |
| 217 | +### Environment-Specific Configuration |
| 218 | + |
| 219 | +Configure different logging behavior for different environments: |
| 220 | + |
| 221 | +```ts |
| 222 | +const isDevelopment = process.env.NODE_ENV === 'development'; |
| 223 | +const isProduction = process.env.NODE_ENV === 'production'; |
| 224 | + |
| 225 | +container.get(Logger).configure({ |
| 226 | + logLevel: isDevelopment ? 'debug' : 'info', |
| 227 | + providers: isDevelopment |
| 228 | + ? [ |
| 229 | + // Development: colorful console output |
| 230 | + { |
| 231 | + name: 'console', |
| 232 | + provider: ConsoleProvider, |
| 233 | + logLevel: 'debug' |
| 234 | + } |
| 235 | + ] |
| 236 | + : [ |
| 237 | + // Production: structured JSON for log aggregation |
| 238 | + { |
| 239 | + name: 'json', |
| 240 | + provider: JSONProvider, |
| 241 | + logLevel: 'info' |
| 242 | + } |
| 243 | + ] |
| 244 | +}); |
| 245 | +``` |
| 246 | + |
| 247 | +### Per-Provider Log Levels |
| 248 | + |
| 249 | +Each provider can have its own log level, independent of the global setting: |
| 250 | + |
| 251 | +```ts |
| 252 | +container.get(Logger).configure({ |
| 253 | + logLevel: 'warn', // Global: only warnings and errors |
| 254 | + providers: [ |
| 255 | + { |
| 256 | + name: 'console', |
| 257 | + provider: ConsoleProvider, |
| 258 | + logLevel: 'debug' // Console shows everything |
| 259 | + }, |
| 260 | + { |
| 261 | + name: 'file', |
| 262 | + provider: FileProvider, |
| 263 | + logLevel: 'error' // File only records errors |
| 264 | + } |
| 265 | + ] |
| 266 | +}); |
| 267 | +``` |
0 commit comments