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

logger-express

v1.1.0

Published

logger for in node and express requests

Downloads

140

Readme

NPM Downloads

⚠️ !new built-in feature, more information at the bottom of the page¡. ⚠️

logger-express

A simple logger for Node and Express.

Installation

To install the package, execute the following command:

npm install logger-express

Usage

To use the logger, import the package into your code:

import { logger } from "logger-express";
const loggerOption = {
  logToFile: true, // If you need to log information to a file
  colorize: true, // enable console colors
  infoColor: "magenta", // set a color for information messages
  errorColor: "red", // set a color for error messages:
};

Finally, add the logger middleware to your application:

app.use(logger(loggerOption));

Options

The logger has the following options:

🗃️ logToFile: Indicates whether information should be logged to a file. The default value is true.

🌈 colorize: Indicates whether console colors should be used. The default value is true.

💡 infoColor: Color for information messages. The default value is magenta.

errorColor: Color for error messages. The default value is red.

If no options are provided, the logger will use its default configuration.

Example

The following example demonstrates how to use the logger to log the request and response information of an application:

import express from "express";
const { logger } = require("logger-express");

const app = express();
const loggerOption = {
  logToFile: true, // If you need to log information to a file
  colorize: true, // enable console colors
  infoColor: "magenta", // set a color for information messages
  errorColor: "red", // set a color for error messages:
};

app.use(express.json());
app.use(logger(loggerOption));

app.get("/", (req, res) => {
  res.send("Hello, world.");
});

app.listen(3000, () => {
  console.log("The application is listening on port 3000.");
});

When you run the application and visit the route /, the logger will record the following information in the console:

Started GET /api/1 for :: ::ffff:127.0.0.1 at Sat Oct 14 2023 17:32:19 GMT-0300 (Chile Summer Time)
Parameters in body: {}

🚨 To display the body information, you should use the express.json() middleware before the logger.

If you visit a route and an error occurs, the logger will log the following message in the console with the corresponding error code:

Completed: 500 message: Internal Server Error Time response: 10 ms Request ID: [34273466-07c3-470d-8099-146236c93e02]

On the other hand, if the query is successful, the following will be displayed:

Completed: 200 message:  Time response: 1 ms Request ID: [92d17d54-5a62-4618-815a-18dae2bc7b00]

The logger will also log information to a file if the logToFile option is set to true. The log file will be created in the logs folder and will be named with the current date, creating a new file for each day.

  • Colors available:
    • black
    • red
    • green
    • yellow
    • blue
    • magenta
    • cyan
    • white
    • blackBright(alias: gray, grey)
    • redBright
    • greenBright
    • yellowBright
    • blueBright
    • magentaBright
    • cyanBright
    • whiteBright

Issues

If you have any suggestions or want to report any errors, you can visit the project's home page.

New built-in feature

This new version incorporates a new functionality for you to set a size for the log folder (this applies only if you have the logToFile option set to true). When the folder size exceeds the set limit, files will be deleted, leaving only the last 5 days of records. This cleanup will occur every 10 days. To add a size, you must include the sizeLogFile property and set the size in megabytes, as I will show you below.

const loggerOption = {
  logToFile: true, // If you need to log information to a file
  sizeLogFile: 5, // Set the size of the log folder in megabytes
  colorize: true, // enable console colors
  infoColor: "magenta", // set a color for information messages
  errorColor: "red", // set a color for error messages:
};

Note: the default size is 5 megabytes.

https://github.com/FabianPinoP/logger-express-documentation