Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bogeychan/elysia-logger
A plugin for Elysia.js for logging using the pino library
https://github.com/bogeychan/elysia-logger
elysia pino
Last synced: 11 days ago
JSON representation
A plugin for Elysia.js for logging using the pino library
- Host: GitHub
- URL: https://github.com/bogeychan/elysia-logger
- Owner: bogeychan
- License: mit
- Created: 2023-04-21T23:25:56.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-09T19:43:11.000Z (8 months ago)
- Last Synced: 2024-05-10T06:21:24.179Z (7 months ago)
- Topics: elysia, pino
- Language: TypeScript
- Homepage: https://npmjs.com/package/@bogeychan/elysia-logger
- Size: 138 KB
- Stars: 79
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-elysia - Logger - Pino logging middleware. (Plugins)
README
# @bogeychan/elysia-logger
A plugin for [Elysia.js](https://elysiajs.com) for logging using the [pino](https://getpino.io) library.
## [Migration Guide](./MIGRATION.md)
## Installation
```bash
bun add @bogeychan/elysia-logger
```## Usage
```ts
import { Elysia } from "elysia";
import { logger } from "@bogeychan/elysia-logger";const app = new Elysia()
.use(
logger({
level: "error",
})
)
.get("/", (ctx) => {
ctx.log.error(ctx, "Context");
ctx.log.info(ctx.request, "Request"); // noopreturn "Hello World";
})
.listen(8080);console.log(`Listening on ${app.server!.url}`);
```### Log to a file, or
```ts
import { fileLogger } from "@bogeychan/elysia-logger";fileLogger({
file: "./my.log",
});
```### Pipe the log entries into a stream
```ts
import { logger } from '@bogeychan/elysia-logger';logger({
stream: ... // default -> console output
});
```### Include additional request context info
```ts
import { logger, type InferContext } from "@bogeychan/elysia-logger";const myPlugin = () => new Elysia().decorate("myProperty", 42);
// ...
class MyError extends Error {
constructor(message: string, public myValue: string) {
super(message);
}
}app = app.error("myError", MyError).use(myPlugin());
app
.use(
logger({
/**
* This function will be invoked for each `log`-method called
* where you can pass additional properties that need to be logged
*/
customProps(ctx: InferContext) {
if (ctx.isError && ctx.code === "myError") {
return {
myValue: ctx.error.myValue,
};
}return {
params: ctx.params,
query: ctx.query,
myProperty: ctx.myProperty,
};
},
})
)
.get("/", (ctx) => {
ctx.log.info(ctx, "Context");return "with-context";
})
.get("/error", () => {
throw new MyError("whelp", "yay");
});
```### Use the logger instance both `Standalone` and inside `Context`
```ts
import { createPinoLogger } from "@bogeychan/elysia-logger";const log = createPinoLogger(/* ... */);
app
.use(log.into(/* ... */))
.onError((ctx) => {
log.error(ctx, ctx.error.name);
return "onError";
})
.get("/", (ctx) => {
ctx.log.info(ctx, "Context");throw new Error("whelp");
});
```### Automatic `onAfterResponse` & `onError` logging by `default`; based on [pino-http](https://github.com/pinojs/pino-http)
```ts
import { logger } from "@bogeychan/elysia-logger";app
.use(
logger({
autoLogging: true, // default
autoLogging: false, // disabled
autoLogging: {
ignore(ctx) {
return true; // ignore logging for requests based on condition
},
},
})
)
.get("/", (ctx) => "autoLogging");
```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.
## License
[MIT](LICENSE)