Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/otherguy/elysia-logging
Logging middleware for ElysiaJS
https://github.com/otherguy/elysia-logging
Last synced: 2 months ago
JSON representation
Logging middleware for ElysiaJS
- Host: GitHub
- URL: https://github.com/otherguy/elysia-logging
- Owner: otherguy
- License: mit
- Created: 2023-10-19T10:11:05.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-18T05:38:59.000Z (4 months ago)
- Last Synced: 2024-10-19T14:43:45.408Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 222 KB
- Stars: 20
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- awesome-elysia - Logging - Logging middleware for a variety of loggers (Pino, Winston, etc.) (Plugins)
README
# @otherguy/elysia-logging
An advanced logging plugin designed for [Elysia.js](https://elysiajs.com), prioritizing structured logging tailored for production environments.
[](https://www.npmjs.com/package/@otherguy/elysia-logging)
[](https://www.npmjs.com/package/@otherguy/elysia-logging)
[](https://snyk.io/test/github/otherguy/elysia-logging)
[](https://github.com/otherguy/elysia-logging/actions/workflows/ci.yml)
[](https://github.com/otherguy/elysia-logging/blob/main/LICENSE.md)
[](https://codeclimate.com/github/otherguy/elysia-logging)
[](https://www.codefactor.io/repository/github/otherguy/elysia-logging/)
[](https://sonarcloud.io/project/overview?id=elysia-logging)---
## ๐ Features
* **Structured Logging** - Log in JSON format for easy parsing and filtering
* **Customizable** - Customize the log level, formatter and logger
* **Production First** - Designed for production environments first## ๐ Installation
```bash
# For bun
bun add @otherguy/elysia-logging# For npm
npm install @otherguy/elysia-logging# For yarn
yarn add @otherguy/elysia-logging
```## ๐ Usage
By default, the plugin will log to the console using the `console` module. The default formatter is `json` and the default log level is `info`.
```ts
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";// Create Elysia app
const app = new Elysia()
.use(ElysiaLogging())
.get("/", () => {
return new Response("Welcome to Bun!");
})
.listen(3000);console.log(`Running at http://${app.server?.hostname}:${app.server?.port}`);
```Using the default settings, the plugin will log the following for each request. Since the `console` module is pretty printing the JSON, the output in the console will be formatted slightly differently.
```json
{
"message": "GET / completed with status 200 in 363.3ยตs",
"request": {
"ip": "127.0.0.1",
"method": "GET",
"url": {
"path": "/",
"params": {}
}
},
"response": {
"status_code": 200,
"time": 363250
}
}
```### Custom Logger
Since the `console` is very limited, you may want to use a custom logger. The recommended logger is [pino](https://github.com/pinojs/pino) but you can use any logger that can implement the `Logger` interface.
See the [examples](examples) directory for implementation examples.
* [Pino](https://github.com/pinojs/pino) (see [example](examples/pino.ts))
* [TSLog](https://tslog.js.org/) (see [example](examples/tslog.ts))
* [Winston](https://github.com/winstonjs/winston) (see [example](examples/winston.ts))
* [Bunyan](https://github.com/trentm/node-bunyan) (see [example](examples/bunyan.ts))## ๐ช Examples
Even more examples are provided in the [`examples`](examples) directory.
* [Basic](examples/basic.ts) - A basic example of using the plugin with the default settings
* [JSON](examples/json.ts) - A basic example of logging in JSON
* [Custom Function](examples/custom-function.ts) - An example of using a function as custom logging formatter
* [On Error](examples/on-error.ts) - An example of logging errors in addition to access logging
* [Pino](examples/pino.ts) - An example of using the [Pino](https://github.com/pinojs/pino) logger
* [Pino Pretty](examples/pino.ts) - An example of using the [Pino](https://github.com/pinojs/pino) logger with [pino-pretty](https://github.com/pinojs/pino-pretty) _(not recommended for production)_
* [TSLog](examples/tslog.ts) - An example of using the [TSLog](https://tslog.js.org/) logger
* [Bunyan](examples/bunyan.ts) - An example of using the [Bunyan](https://github.com/trentm/node-bunyan) logger
* [Winston](examples/winston.ts) - An example of using the [Winston](https://github.com/winstonjs/winston) logger## ๐ To Do
* [ ] Add logger format classes
* [ ] Add whitelist for request parameters## โ๏ธ License
This project is distributed under the [MIT](LICENSE.md) license, allowing for open source distribution and modification, subject to the terms outlined in the [LICENSE.md](LICENSE.md) file.
## ๐ง Contributing
Bug reports, feature requests and Pull Requests are more than welcome on GitHub at [`otherguy/elysia-logging`](https://github.com/otherguy/elysia-logging)! Please remember to add test coverage for your code if you are contributing.
## โฅ๏ธ Acknowledgements
* [SaltyAom](https://github.com/SaltyAom) for the [Elysia.js](https://elysiajs.com) framework.