A high-performance, NestJS-style logger plugin for ElysiaJS with beautiful console output and flexible configuration.
Author: KrataiB
- ๐จ NestJS-Style UI - Beautiful, colorful console output
- โก High Performance - Built on pino, one of the fastest Node.js loggers
- ๐ Hybrid Logging - Console (pretty) + File (JSON) simultaneously
- ๐ง Fully Configurable - Customize everything from log levels to formats
- ๐ Request Details - Log query params, path params, and request bodies
- ๐ฏ Type-Safe - Full TypeScript support with proper type inference
- ๐ Zero Dependencies Overhead - Only
pinoandpicocolors
# Using bun (recommended)
bun add elysia-logger
# Using npm
npm install elysia-logger
# Using yarn
yarn add elysia-logger
# Using pnpm
pnpm add elysia-loggerimport { Elysia } from "elysia";
import { logger } from "elysia-logger";
const app = new Elysia()
.use(logger())
.get("/", () => "Hello World")
.listen(3000);That's it! You'll now see beautiful logs like:
[Elysia] 12345 - 11/21/2025, 2:00:00 PM INFO [Elysia] ๐ฆ Elysia is running at http://localhost:3000
[Elysia] 12345 - 11/21/2025, 2:00:00 PM INFO [Elysia] Routes:
[Elysia] 12345 - 11/21/2025, 2:00:00 PM INFO [Router] GET /
[Elysia] 12345 - 11/21/2025, 2:00:05 PM INFO [Router] GET / +2ms
import { logger } from "elysia-logger";
app.use(
logger({
// Transport type: 'console' (pretty) or 'json'
transport: "console", // default
// Enable/disable all logging
enabled: true, // default
// Log level: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'
level: "info", // default
// Context name for logs
context: "MyApp", // default: 'Elysia'
// Custom server name for logs
name: "MyServer", // default: 'Elysia'
})
);Log to a file in JSON format (perfect for production):
app.use(
logger({
file: "./app.log",
})
);Hybrid Mode - Both console (pretty) AND file (JSON):
app.use(
logger({
transport: "console", // Pretty console output
file: "./app.log", // JSON file output
})
);app.use(
logger({
// Auto-log all requests/responses
autoLogging: true, // default
// Log request start (before processing)
logRequestStart: false, // default: true
// Log request details (params, query, body)
logDetails: true, // default: false
})
);Example with logDetails: true:
[Router] POST /user/123 +5ms {"params":{"id":"123"},"body":{"name":"John","email":"[email protected]"}}
Define your own log format:
app.use(
logger({
formatter: (level, message, context) => {
return `[${level.toUpperCase()}] ${context}: ${message}`;
},
})
);Access the logger instance via ctx.log:
app.get("/custom", (ctx) => {
ctx.log.log("This is an info message", "MyContext");
ctx.log.error("Something went wrong!", "Error stack trace", "ErrorContext");
ctx.log.warn("Warning message");
ctx.log.debug("Debug info");
ctx.log.verbose("Verbose logging");
return "Done";
});Use the Logger class independently:
import { Logger } from "elysia-logger";
const log = new Logger({
transport: "console",
level: "debug",
});
log.log("Hello from standalone logger!");
log.error("Error message", "Stack trace");Elysia Logger comes with a built-in HttpError class (similar to elysia-http-error) for easy error handling:
import { Elysia } from "elysia";
import { logger, HttpError } from "elysia-logger";
const app = new Elysia()
.use(logger())
.get("/not-found", () => {
throw HttpError.NotFound("Resource not found");
})
.get("/bad-request", () => {
throw HttpError.BadRequest("Invalid input");
})
.get("/custom-error", () => {
throw new HttpError(418, "I'm a teapot");
});Available static methods:
HttpError.BadRequest(message?)(400)HttpError.Unauthorized(message?)(401)HttpError.Forbidden(message?)(403)HttpError.NotFound(message?)(404)HttpError.MethodNotAllowed(message?)(405)HttpError.Conflict(message?)(409)HttpError.PreconditionFailed(message?)(412)HttpError.UnprocessableEntity(message?)(422)HttpError.TooManyRequests(message?)(429)HttpError.InternalServerError(message?)(500)
import { Elysia } from "elysia";
import { logger } from "elysia-logger";
const app = new Elysia()
.use(
logger({
transport: "console",
file: "./logs/app.log",
level: "info",
logDetails: true,
logRequestStart: false,
})
)
.get("/", () => "Hello World")
.get("/user/:id", ({ params, log }) => {
log.log(`Fetching user ${params.id}`, "UserController");
return { user: params.id };
})
.post("/create", ({ body, log }) => {
log.log("Creating new item", "CreateController");
return { created: true };
})
.onError(({ error, log }) => {
log.error("Unhandled error", error.stack, "GlobalError");
return { error: error.message };
})
.listen(3000);
console.log("Server running on http://localhost:3000");| Option | Type | Default | Description |
|---|---|---|---|
transport |
'console' | 'json' |
'console' |
Output format |
enabled |
boolean |
true |
Enable/disable logging |
level |
'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' |
'info' |
Minimum log level |
context |
string |
'Elysia' |
Default context name |
name |
string |
'Elysia' |
Custom server name |
file |
string |
undefined |
File path for JSON logs |
autoLogging |
boolean |
true |
Auto-log requests/responses |
logRequestStart |
boolean |
true |
Log when request starts |
logDetails |
boolean |
false |
Log request params/query/body |
formatter |
(level, message, context?) => string |
undefined |
Custom format function |
class Logger {
log(message: string, context?: string): void;
error(message: string, trace?: string, context?: string): void;
warn(message: string, context?: string): void;
debug(message: string, context?: string): void;
verbose(message: string, context?: string): void;
}Logs are color-coded by level:
- ๐ข INFO - Green
- ๐ด ERROR - Red
- ๐ก WARN - Yellow
- ๐ต DEBUG - Blue
- ๐ท VERBOSE - Cyan
app.use(
logger({
transport: "json", // Structured logs for log aggregators
file: "./logs/app.log", // Persist to file
level: "info", // Filter out debug/verbose
logDetails: false, // Avoid logging sensitive data
logRequestStart: false, // Reduce log noise
})
);For production, use a log rotation tool like logrotate or pm2.
bun testMIT ยฉ KrataiB
Contributions are welcome! Please feel free to submit a Pull Request.
Found a bug? Have a feature request? Please open an issue.
Give a โญ๏ธ if this project helped you!
Built with โค๏ธ for the Elysia community
