You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Plugins should use dedicated loggers to provide better organization and debugging capabilities. Use `createPluginLogger()` to create plugin-specific loggers that are automatically namespaced.
257
+
258
+
### Plugin-Specific Loggers
259
+
260
+
```typescript
261
+
import {
262
+
defineKoriPlugin,
263
+
createPluginLogger,
264
+
typeKoriEnvironment,
265
+
typeKoriRequest,
266
+
typeKoriResponse,
267
+
typeKoriPlugin,
268
+
} from'@korix/kori';
269
+
270
+
exportfunction authPlugin<
271
+
EnvextendsKoriEnvironment,
272
+
ReqextendsKoriRequest,
273
+
ResextendsKoriResponse,
274
+
>():KoriPlugin<Env, Req, Res> {
275
+
returndefineKoriPlugin({
276
+
name: 'auth',
277
+
apply: (kori) => {
278
+
// Instance-level plugin logger
279
+
const log =createPluginLogger({
280
+
baseLogger: kori.log(),
281
+
pluginName: 'auth',
282
+
});
283
+
284
+
log.info('Auth plugin initialized');
285
+
286
+
returnkori.onRequest((ctx) => {
287
+
// Request-level plugin logger
288
+
const requestLog =createPluginLogger({
289
+
baseLogger: ctx.log(),
290
+
pluginName: 'auth',
291
+
});
292
+
293
+
requestLog.info('Checking authentication', {
294
+
path: ctx.req.url().pathname,
295
+
});
296
+
297
+
// Your authentication logic here...
298
+
});
299
+
},
300
+
});
301
+
}
302
+
```
303
+
304
+
### Benefits of Plugin Loggers
305
+
306
+
Plugin-specific loggers provide several advantages:
307
+
308
+
-**Namespace isolation**: Logs are automatically prefixed with `plugin.{pluginName}`
309
+
-**Better debugging**: Easy to filter logs by specific plugins
310
+
-**Consistent formatting**: Inherits all bindings from the base logger
311
+
-**Channel separation**: Plugin logs use dedicated channels for organization
312
+
313
+
### Logger Output
314
+
315
+
Plugin loggers automatically namespace their output:
316
+
317
+
```json
318
+
{
319
+
"time": 1754201824386,
320
+
"level": "info",
321
+
"channel": "plugin.auth",
322
+
"name": "request",
323
+
"message": "Checking authentication",
324
+
"meta": {
325
+
"path": "/api/users"
326
+
}
327
+
}
328
+
```
329
+
330
+
Compare this with regular context logging:
331
+
332
+
```json
333
+
{
334
+
"time": 1754201824386,
335
+
"level": "info",
336
+
"channel": "app",
337
+
"name": "request",
338
+
"message": "Processing request",
339
+
"meta": {}
340
+
}
341
+
```
342
+
240
343
## Official Plugins
241
344
242
345
Kori provides official plugins for common use cases. See the Extensions section for detailed documentation.
0 commit comments