@tlscipher/holt
v1.2.0
Published
A highly configurable logger middleware for ElysiaJS.
Downloads
116
Maintainers
Readme
@tlscipher/holt
A highly configurable logger middleware for ElysiaJS.
Named after Raymond Holt from Brooklyn Nine-Nine
Installation
bun add @tlscipher/holt
Usage
import { HoltLogger } from "@tlscipher/holt";
import { Elysia } from "elysia";
new Elysia()
.use(new HoltLogger().getLogger())
.get("/", () => {})
.listen(3000);
Configuration
HoltLogger
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)"
)- The
format
parameter allows you to customize the log output that is written to the console using tokens.
- The
colorful
(Default:true
)- The
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.
- The
Possible Tokens for Format
:date
The time at which the response was sent.- Example output:
2023-09-16T21:15:04.516Z
- Example output:
:method
The HTTP method that was used for the inbound request.- Example output:
GET
- Example output:
:path
The path of the inbound HTTP request- Example output:
/health
- Example output:
:request-duration
The difference in milliseconds from the time the request was received, to the time the response was sent- Example output:
4.28
- Example output:
Adding Headers to the Format
This package allows you to log any of the available incoming headers.
Format for header tokens:
:header[<header-key-here>]
Examples:
:header[user-agent]
- Example output:
PostmanRuntime/7.33.0
- Example output:
:header[accept]
- Example output:
application/json
- Example output:
:header[authorization]
- Example output:
Bearer auth_xxxxxx...
- Example output:
Custom Tokens
You can now add custom tokens to your logger using the HoltLogger.token
function. The function accepts the following parameters:
token
(required string)- The string to be tokenized and used and the replacer in the format
extractFn
(required function)- The function will be given access to select properties of ELysia's context and must return the string value to replace the token in the format.
Example Custom Token Usage
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);