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

http-track

v1.1.3

Published

A simple and lightweight HTTP Logger for an Express.js application.

Downloads

12

Readme

A simple and lightweight HTTP Logger for an Express.js application.

Installation

In a NodeJS project using Express, run npm install http-track to install the package.

How to setup

The package is created so that it can run out of the box simply by letting the application use it. Example shown below:

const express = require("express")
const { httpLogger } = require('./dist/index.js');

const app = express();

app.use(httpLogger());

The logger has default settings which make it incredibly simple to setup. All that is left to do is create some endpoint with Express.

app.get('/', (req, res) => {
    res.send('Hello, World!');
});

app.listen(3000, () => {
    console.log('Server is running on http://localhost:3000');
});

Upon performing a GET request to http://localhost:3000 a message will be displayed in the console.

Example GET request

Customizing the logs

Parameters

The logger has some simple parameters which can be shown or reordered:

  • timestamp - the time when the request was made
  • method - the type of method called (GET, POST, PUT, etc)
  • url - the endpoint which was called
  • status - the response status of the server
  • response-time - the time taken for the response to be made (in ms)

By default the structure of the logs is the following: ':timestamp :method :url :status - :response-time ms'

You can change this by modifying the format parameter when creating the logger. The parameters can be removed or reordered.

app.use(httpLogger({ format: ':timestamp :method' }));

Color

The color of the Logs can also be modified. By default the package uses the following object:

const defaultColors: Record<number, FontColor> = {
    500: FontColor.Red,
    400: FontColor.Yellow,
    300: FontColor.Blue,
    200: FontColor.Green,
}

This means that the logs will be displayed in Red for all the 5XX codes, in yellow for all the 4XX codes, etc.

If you wish to change this default behavior you need to provide a colorOptions parameter when initializing the package. It needs to have the same structure as the above-shown example. If any status code is missing the log will be displayed with the default white color.

The available colors which you can use can be found in the FontColor enum. They are the following:

export enum FontColor {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    Gray,
}

Example usage:

const { httpLogger, FontColor } = require('./dist/index.js');

const app = express();

const colors = {
    500: FontColor.Gray,
    400: FontColor.Blue,
    300: FontColor.Cyan,
    200: FontColor.Magenta
}

app.use(httpLogger({ colorOptions: colors }));

Disable colorful logs by simply providing the color parameter as false when initializing the logger: app.use(httpLogger({ color: false }));

NOTE: If color is set to false, the colorOptions parameter gets ignored.

Output directory

The logs can also be saved to an external txt file. In order to enable this functionality simply add the outDir parameter when initializing the logger. The outDir parameter is the path to the directory where the logs will be stored. You can also specify the outFile to specify a file name (defaults to logs.txt)

NOTE: If the provided outDir or outFile do not exist, the application will automatically create them!

Example usage:

/* Stores data in the logs.txt file in the logs directory */
app.use(httpLogger({ outDir: './logs' }));


/* Creates a customLogs.txt file in the logs directory */
app.use(httpLogger({ outDir: './logs', outFile: 'customLogs.txt' }));

LogFile creationm frequency

The logs can be saved automatically to new files either daily, weekly or monthly. By default this feature is not enabled and all the logs will be stored in a single file.

Example usage:

const { httpLogger, FontColor, ExportFreq } = require('./dist/index.js');

/* Stores data in the logs.txt file in the logs directory */
app.use(httpLogger({ outDir: './logs', exportFreq: ExportFreq.daily }));

Outputs the logs in the ./logs directory in a file called {year}{month}{day}-logs.txt

Upcoming Features

NOTE: This package is still in development and has a number of upcoming features. You can read more about the features which will be added in the future:

  • Further customization of the logs format
  • Ability to upload logs on third-party platforms