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

your-logger-lite

v1.1.5

Published

A simple logging package with built-in traceability for managing requests, handlers, and controllers in ExpressJS applications

Downloads

4

Readme

Introduction

Your-Logger-Lite is a simple tool that provides your ExpressJS applications with basic logging capabilities and traceability by leveraging middleware.

Your-Logger-Lite is built using typescript and ships with type support. It is configured to allow for easy implementation both with commonJS and ES6.

Table of Contents

Installation

Uisng yarn:

$ yarn add your-logger-lite 

Using npm:

$ npm install your-logger-lite 

Getting Started:

Environment Variables

your-logger-lite depends on an environment variable named LOG_LEVEL. The concept of log-level will be discussed in further detail a little later. In brief, log-level allows some basic, global, environment-bound control over whether or not certain actions are logged. your-logger-lite's loggerMiddleware accesses the LOG_LEVEL by calling process.env.LOG_LEVEL, as such it is important to configure this variable. The default level is set to 4, so that if you fail to provide this environment variable, the logger will still function and will show all log levels.

Examples

Initialize your NodeJS + ExpressJS application as you normally would. Import both loggerMiddleware and traceMiddleware from 'your-logger-lite'. Add these pieces of middleware using app.use. It is important that you initialize them in this order:

app.use(traceMiddleware)
app.use(loggerMiddleware)

Here is an arbitrary example of a basic express server set-up, shown both using ES6 and commonJS:

ES6

// index.js

import express from 'express';
import cors from 'cors;
import { loggerMiddleware, traceMiddleware } from 'your-logger-lite'

	// any other imports & configs such as database connections

const app = express()

app.use(express.json());
app.use(cors()); 
app.use(express.urlencoded({ extended: true }));

app.use(traceMiddleware)
app.use(loggerMiddleware)

	// your endpoints 
	// your app.listen()

CommonJS

// index.js

var express = require('express')
var cors = require('cors')
var logger = require('your-logger-lite')

	// any other imports & configs such as database connections

var app = express()

app.use(express.json());
app.use(cors());
app.use(express.urlencoded({ extended: true }));

app.use(logger.traceMiddleware)
app.use(logger.loggerMiddleware)

	// your endpoints
	// your app.listen()
	

With this basic setup, all of your routes will already have some level of automatic logging. For example, consider the following arbitrary end-point:

app.get('/health', (req, res) => {
	res.send('OK')
});

app.listen(process.env.PORT || 3000, () => {
	console.log(`App running on port ${process.env.PORT}`)
});

With the middleware properly initialized, the console should show the following:

App running on port 3000
IP: ::1, HOST: localhost, METHOD: GET

While logging this kind of information when in development on a local server is somewhat contrived, this kind of information can be helpful when the application / API is accessible to multiple clients when in production.

General Use

Your-Logger-Lite was designed with the handler-controller paradigm in mind, but it can be injected virtually anywhere. Here is an example of how you might use it to add logging to an arbitrary handler:

// foo.route.js
import { Router } from 'express'
import { handleCreateFoo } from '../handlers/foo.handler'

const fooRouter = Router()

fooRouter.post('/', handleCreateFoo)

---
// foo.handler.js

export const handleCreateFoo = async (req, res) => {
	const { context } = req;
	const { logger } = context;
	const self = handleCreateFoo.name;
	logger.start(self);
	let newFoo;

	try {
		newFoo = await createFoo({ req.body, context })
	} catch (e) {
		logger.error(e, 500, self)
		return res.status(500).send(`Could not create newFoo with error: ${e})
	}

	logger.end(self)
	return res.status(200).send(newFoo)
}
---
// foo.contollers.js

	// createFoo controller implementation

All that logger.start() does is call logger.info() and logger.time() (while logger.end() calls logger.info() and logger.timeEnd()) which can be called independently, depending on your use-case, giving you further glanularity of control.