npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lenoux01/lean-logs

v1.1.0

Published

Versatile and colorful logger for Bun's Elysia framework

Downloads

33

Readme

lean-logs: A Lightweight Logging Tool for Bun and Elysia

lean-logs is a powerful, efficient, and lightweight logging tool specifically designed for use with Bun and Elysia in Node.js environments. It offers a straightforward API and customizable log levels, making it an ideal choice for developers looking to enhance debugging and monitoring capabilities in both development and production environments without introducing significant overhead.

Lean Logs Coloring

Features

  • Colorized Output: Utilizes color-coding for log messages based on HTTP methods or log levels, improving readability and quick scanning.
  • Performance Timing: Measures and logs the duration of code execution, database queries, or HTTP requests in a human-readable format.
  • API Response Logging: Provides structured logging of API responses, facilitating better tracking of outgoing data and potential issues.
  • Extensive Testing: Includes a comprehensive test suite to ensure reliability and stability across updates.
  • Customization Options: Allows tailoring the logger to fit specific project needs.
  • WebSocket Support: Supports logging of WebSocket communications for comprehensive monitoring of real-time applications.
  • Custom Message Logging: Enables creation and logging of custom messages for application-specific requirements.

Installation

To install lean-logs using Bun:

bun add @lenoux01/lean-logs

Usage

Basic Setup

import { Elysia } from "elysia";
import { logger } from "@lenoux01/lean-logs";

const app = new Elysia()
  .use(logger())
  .get("/", () => "Hello World!")
  .listen(3000);

console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`);

Customization

import { Elysia } from "elysia";
import { logger } from "@lenoux01/lean-logs";

const app = new Elysia()
  .use(logger({
    logIP: true,
    logPath: "./custom-logs/app.log"
  }))
  .get("/", () => "Hello World!")
  .listen(3000);

Logging WebSocket Communications

import { Elysia } from "elysia";
import { logger, logWebSocket } from "@lenoux01/lean-logs";

const app = new Elysia()
  .use(logger())
  .ws("/ws", {
    message(ws, message) {
      logWebSocket(message);
      ws.send("Message received");
    },
  })
  .listen(3000);

How It Works

lean-logs operates by intercepting requests and responses in your Elysia application:

  1. Request Logging: Captures the start time and relevant information when a request is received.
  2. Response Logging: Logs details such as HTTP method, path, status code, and response time after request handling.
  3. Error Logging: Captures and logs error details if an error occurs during request processing.
  4. File Logging: Writes all logs to both the console and a file (default: logs/server.log).
  5. WebSocket Logging: Provides a separate logging function for WebSocket-specific information.

Configuration Options

  • logIP: Boolean flag to enable/disable logging of caller IP addresses.
  • logPath: Custom path for the log file.

Performance Considerations

lean-logs is designed to be lightweight and efficient, using process.hrtime.bigint() for accurate timing measurements. Logging operations are optimized to minimize impact on application performance.

Testing

lean-logs includes a comprehensive test suite covering various scenarios including HTTP method logging, WebSocket logging, and error handling. To run the tests:

bun test

Best Practices

  1. Environment-based Configuration: Use different logging configurations for development and production environments.
  2. Log Rotation: Implement log rotation to manage log file sizes, especially in production.
  3. Sensitive Data: Be cautious about logging sensitive information, especially in request bodies or headers.
  4. Performance Monitoring: Use timing information to identify and optimize slow endpoints or operations.

Conclusion

lean-logs provides a robust, easy-to-use logging solution for Bun and Elysia applications. Its colorized output, performance timing, and WebSocket support make it valuable for improving application observability and debugging capabilities. With both console and file logging out of the box, lean-logs ensures comprehensive logging coverage for applications of all sizes.

License

This project is licensed under the MIT License - see the LICENSE file for details.