@otherguy/elysia-logging
v0.0.17
Published
A flexible logging library for Elysia.js
Downloads
535
Readme
@otherguy/elysia-logging
An advanced logging plugin designed for Elysia.js, prioritizing structured logging tailored for production environments.
🌈 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
# 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
.
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.
{
"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 but you can use any logger that can implement the Logger
interface.
See the examples directory for implementation examples.
🪜 Examples
Even more examples are provided in the examples
directory.
- Basic - A basic example of using the plugin with the default settings
- JSON - A basic example of logging in JSON
- Custom Function - An example of using a function as custom logging formatter
- On Error - An example of logging errors in addition to access logging
- Pino - An example of using the Pino logger
- Pino Pretty - An example of using the Pino logger with pino-pretty (not recommended for production)
- TSLog - An example of using the TSLog logger
- Bunyan - An example of using the Bunyan logger
- Winston - An example of using the Winston logger
📜 To Do
- [ ] Add logger format classes
- [ ] Add whitelist for request parameters
⚖️ License
This project is distributed under the MIT license, allowing for open source distribution and modification, subject to the terms outlined in the LICENSE.md file.
🚧 Contributing
Bug reports, feature requests and Pull Requests are more than welcome on GitHub at otherguy/elysia-logging
! Please remember to add test coverage for your code if you are contributing.