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

poc_node_logging_tool

v1.0.2

Published

node logging tool

Downloads

1

Readme

POC Node Logging Prototype

Overview and Scope

The logging is a dedicated component in the software project responsible for recording and managing log entries for NodeJS projects. It separates the concerns of logging from the core application code and offers several advantages like clean code, customisation configurations of levels, formats etc and it can can be easily integrated with log analysis tools and external services. It will give us a reusable and consistent format and structure throughout the application/s, and thanks to that we can improve performance monitoring and security auditing.

The logger module integrates other best practice configurations and tools, such as Prettier for code formatting, a linter for code quality, and includes a Makefile for task automation, and other tools. These tools help maintain code quality, consistency, and automate routine development tasks.

Files Structure

Directory Path | Description --- | --- ./formatting/ | This folder contains formatting available for the Winston module, humanFormat.ts and jsonFormat.ts  as basic format. ./config/ | This folder contains configuration settings for the logger module. It is essential for customising the behaviour of the logger according to the application's requirements. The configuration options are (levels, colours, level, humanReadable and others) ./type/ | This folder defines data types, includes TypeScript interfaces that describe the structure and shape of various objects used in the module, for example LoggerOptions, LogMetaData and others ./util/ | This folder contains utility functions that are used throughout the logger module ./ApplicationLogger.ts | ApplicationLogger is the only class on the logging module. It is a class that wraps a Logger instance and provides default and request logging methods for each defined set levels (error, info and debug). ./index.ts | Contains the createLogger(namespace: string): ApplicationLogger, and it is a function used by the application to initialise the logger and create the log methods, and it returns an instance of ApplicationLogger ./test | Jest Test files (*.spec.ts, setup.ts, and *.mocks.ts) Others files | Other files related to modules dependency, CI/CD, *git, dockerization, lint, test/typescript configs …

Formatting

The createFormat(options: LoggerOptions): Logger function is used to create a logging format for the logger module, and it can generate logs in either human-readable or JSON format, depending on the provided LoggerOptions.

  • options (parameter): An object of type LoggerOptions that defines the format and behavior of the logger.

  • Logger (return type): The function returns an instance of a Logger that is configured based on the provided options. The Logger would have methods for various log levels (e.g., debug, info, error) and be responsible for creating log entries in the specified format.

Logger Class

ApplicationLogger is the only class on the logging module. It is a class that wraps a Logger instance and provides default and request logging methods for each defined set levels (error, info and debug).

It has a constructor that takes a Logger instance as a parameter to initialise the logger attribute. It provides methods for different logging levels, including debug(), info(), and error(), which take a message as input. We could add more levels but I think those three will cover all our scenario. It also provides methods for request-based logging at each level, such as debugRequest(), infoRequest(), and errorRequest(), which take a Request (express object) and a message as input.

Integration

To integrate the module for testing purposes into your Node.js project, use the following command: npm install username/repo#branchName --save. Once it is installed, you need to create a logger.ts utility, as shown below, and use it when required. Obviously we are going to use the singleton pattern.

// logger.ts
import { ApplicationLogger } from "logging/dist/ApplicationLogger";
import { createLogger } from "logging";

import { APPLICATION_NAME } from "../config";

let logger: ApplicationLogger | undefined = undefined;

export const log = (!logger) ? (logger = createLogger(APPLICATION_NAME)) : logger;

// Middleware example
import { NextFunction, Request, Response } from 'express';
import { log } from '../utils/logger';

export const logger = (req: Request, _res: Response, next: NextFunction) => {
    log.infoRequest(req, `${req.method} ${req.path}`);
    next();
};

ToDo

  • Create Repo template
  • Publishes the SDK on npm package registry