logger
Middleware for logging incoming HTTP requests and responses. Logs the method, pathname, status code, and execution time for each request. Useful for debugging and monitoring your TezX server.
Import
import { logger } from "tezx/middleware";Options (LoggerOptions)
export interface LoggerOptions {
/** Enable or disable logging. Default: true */
enabled?: boolean;
}-
enabled– Boolean flag to enable or disable logging.true– logs requests (default)false– disables logging for silent operation
Usage
Basic Usage
import { logger } from "tezx/middleware";
app.use(logger()); // logging enabled by default
app.get("/api/data", async (ctx) => {
return ctx.json({ message: "Hello World" });
});Disable Logging
app.use(logger({ enabled: false }));Behavior
- Logs Incoming Request When a request comes in, it logs:
<-- METHOD /pathnameExample:
<-- GET /api/data-
Measures Execution Time The middleware measures how long the downstream middleware or handler takes to complete.
-
Logs Response After the request is processed, it logs:
--> METHOD /pathname STATUS_CODE TIMEExample:
--> GET /api/data 200 3.45ms- Error Logging If an error occurs during request processing, it logs the stack trace:
Error: <stack trace>Middleware Type
function logger(options?: LoggerOptions): Middleware- Returns a
Middlewarefunction compatible with TezX. - Can be used globally or per-route.
Example with TezX Routes
import logger from "tezx/middleware";
// Enable logger globally
app.use(logger());
// Sample route
app.get("/api/users", async (ctx) => {
return ctx.json({ users: [] });
});
// Disable logger for a specific route
app.post("/api/secret", logger({ enabled: false }), async (ctx) => {
return ctx.json({ secret: "hidden" });
});Console Output Example
<-- GET /api/users
--> GET /api/users 200 2.14ms
<-- POST /api/secret
--> POST /api/secret 200 1.07ms<--indicates incoming request-->indicates response completed- Execution time shows performance of route handler
i18n
The i18n middleware adds multi-language support to TezX applications. It detects the user's preferred language, loads translation files, optionally caches them, and provides a translation function (`ctx.t`) in every request.
paginationHandler
The paginationHandler middleware provides automatic pagination handling for API endpoints. It simplifies extracting pagination parameters, calculating offsets, generating metadata, and optionally integrates with a dynamic data source (e.g., database, ORM).