A highly configurable logger middleware for ElysiaJS.
MIT License
A highly configurable logger middleware for ElysiaJS.
Named after Raymond Holt from Brooklyn Nine-Nine
bun add @tlscipher/holt
import { HoltLogger } from "@tlscipher/holt";
import { Elysia } from "elysia";
new Elysia()
.use(new HoltLogger().getLogger())
.get("/", () => {})
.listen(3000);
The constructor for HoltLogger
function accepts an optional parameter of type HoltConfig
.
interface HoltConfig {
format: string;
colorful: boolean;
}
format
(Default: ":date | :method :path - :status (:request-duration ms)"
)
format
parameter allows you to customize the log output that is written to the console using tokens.colorful
(Default: true
)
colorful
parameter allows to specifiy a boolean value of wether or not you would like the logger output to be color-coded based on the HTTP response status code.:date
The time at which the response was sent.
2023-09-16T21:15:04.516Z
:method
The HTTP method that was used for the inbound request.
GET
:path
The path of the inbound HTTP request
/health
:request-duration
The difference in milliseconds from the time the request was received, to the time the response was sent
4.28
This package allows you to log any of the available incoming headers.
:header[<header-key-here>]
:header[user-agent]
PostmanRuntime/7.33.0
:header[accept]
application/json
:header[authorization]
Bearer auth_xxxxxx...
You can now add custom tokens to your logger using the HoltLogger.token
function. The function accepts the following parameters:
token
(required string)
extractFn
(required function)
import { HoltLogger } from "@tlscipher/holt";
import { Elysia } from "elysia";
new Elysia()
.use(
new HoltLogger({
format: ":method :path | :is-admin",
})
.token("is-admin", ({ headers }) => {
return headers["x-admin-api-key"] === "admin-api-key-here"
? "Admin Request"
: "User Request";
})
.getLogger()
)
.get("/", () => {})
.listen(3000);