@bogeychan/elysia-logger
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "@bogeychan/elysia-logger", | ||
"version": "0.0.2", | ||
"description": "A plugin for elysia for logging using the pino library", | ||
"version": "0.0.3", | ||
"description": "A plugin for Elysia.js for logging using the pino library", | ||
"author": { | ||
@@ -30,8 +30,8 @@ "name": "bogeychan", | ||
"peerDependencies": { | ||
"elysia": ">= 0.4.9" | ||
"elysia": ">= 0.6.0" | ||
}, | ||
"devDependencies": { | ||
"bun-types": "^0.5.8", | ||
"elysia": "^0.4.9", | ||
"typescript": "^5.0.4" | ||
"bun-types": "^0.7.2", | ||
"elysia": "^0.6.0", | ||
"typescript": "^5.1.6" | ||
}, | ||
@@ -38,0 +38,0 @@ "dependencies": { |
# @bogeychan/elysia-logger | ||
A plugin for [elysia](https://elysiajs.com) for logging using the [pino](https://getpino.io) library | ||
A plugin for [Elysia.js](https://elysiajs.com) for logging using the [pino](https://getpino.io) library | ||
@@ -54,2 +54,64 @@ ## Installation | ||
### Include additional request context info for debugging tools | ||
```ts | ||
import { logger } from '@bogeychan/elysia-logger'; | ||
const myPlugin = () => (app: Elysia) => app.decorate('myProperty', 42); | ||
// ... | ||
app = app.use(myPlugin()); | ||
app | ||
.use( | ||
logger({ | ||
/** | ||
* This function will be invoked for each `log`-method called with `context` | ||
* where you can pass additional properties that need to be logged | ||
*/ | ||
customProps( | ||
ctx: ElysiaContextForInstance<InferElysiaInstance<typeof app>> | ||
) { | ||
return { | ||
params: ctx.params, | ||
query: ctx.query, | ||
myProperty: ctx.myProperty | ||
}; | ||
} | ||
}) | ||
) | ||
.get('/', (ctx) => { | ||
ctx.log.info(ctx, 'Context'); | ||
return 'with-context'; | ||
}) | ||
.listen(8080); | ||
``` | ||
You can find the entire example in the [examples](./examples/with-context) folder. | ||
### Customize the logger name in the request context | ||
```ts | ||
import { Elysia } from 'elysia'; | ||
import { logger } from '@bogeychan/elysia-logger'; | ||
const app = new Elysia() | ||
.use( | ||
logger({ | ||
contextKeyName: 'myLogger' | ||
}) | ||
) | ||
.get('/', (ctx) => { | ||
// property "myLogger" is available instead of "log" | ||
ctx.myLogger.info(ctx.request, 'Request'); | ||
return 'Hello World'; | ||
}) | ||
.listen(8080); | ||
console.log(`Listening on http://${app.server!.hostname}:${app.server!.port}`); | ||
``` | ||
Checkout the [examples](./examples) folder on github for further use cases such as the integration of [pino-pretty](https://github.com/pinojs/pino-pretty) for readable console outputs. | ||
@@ -64,1 +126,2 @@ | ||
[MIT](LICENSE) | ||
@@ -0,0 +0,0 @@ import type { Context } from 'elysia'; |
export * from './formatters'; | ||
export * from './serializers'; |
@@ -0,0 +0,0 @@ import { LoggerOptions, stdSerializers } from 'pino'; |
@@ -57,4 +57,22 @@ import pino from 'pino'; | ||
function plugin(options: FileLoggerOptions | StreamLoggerOptions) { | ||
const log = createPinoLogger(options); | ||
return (app: Elysia) => app.derive(() => ({ log })); | ||
if (!options.contextKeyName) { | ||
options.contextKeyName = 'log'; | ||
} | ||
const { contextKeyName, ...loggerOptions } = options; | ||
return (app: Elysia) => | ||
app.derive((ctx) => { | ||
let log = createPinoLogger(loggerOptions); | ||
if (typeof options.customProps === 'function') { | ||
// @ts-ignore | ||
log = log.child(options.customProps(ctx)); | ||
} | ||
return { | ||
[contextKeyName]: log | ||
}; | ||
}); | ||
} | ||
@@ -0,1 +1,2 @@ | ||
import type { Context, Elysia, ElysiaInstance } from 'elysia'; | ||
import type { | ||
@@ -9,3 +10,3 @@ DestinationStream, | ||
*/ | ||
export type StreamLoggerOptions = PinoLoggerOptions & { | ||
export type StreamLoggerOptions = BaseLoggerOptions & { | ||
stream?: DestinationStream; | ||
@@ -17,3 +18,3 @@ }; | ||
*/ | ||
export type FileLoggerOptions = PinoLoggerOptions & { | ||
export type FileLoggerOptions = BaseLoggerOptions & { | ||
file: PathLike; | ||
@@ -26,1 +27,33 @@ }; | ||
export type LoggerOptions = StreamLoggerOptions | FileLoggerOptions; | ||
type BaseLoggerOptions = PinoLoggerOptions & { | ||
/** | ||
* Customize the logger name in the request context | ||
* | ||
* @example | ||
* const app = new Elysia() | ||
* .use(logger({ contextKeyName: 'myLogger' })) | ||
* .get('/', (ctx) => { | ||
* // property "myLogger" is available instead of "log" | ||
* ctx.myLogger.info(ctx.request, 'Request'); | ||
* // ... | ||
* }).listen(8080); | ||
*/ | ||
contextKeyName?: string; | ||
/** | ||
* This function will be invoked for each `log`-method called with `context` | ||
* where you can pass additional properties that need to be logged | ||
*/ | ||
customProps?: <Instance extends ElysiaInstance>( | ||
ctx: ElysiaContextForInstance<Instance> | ||
) => object; | ||
}; | ||
export type InferElysiaInstance<T> = T extends Elysia<infer U> ? U : never; | ||
export type ElysiaContextForInstance<Instance extends ElysiaInstance> = Context< | ||
Instance['schema'], | ||
Instance['store'] | ||
> & | ||
Partial<Instance['request']>; | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
126
9549
8
146