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

logpersist

v1.0.4

Published

A message logging library for server-side JavaScript

Downloads

2

Readme

Logpersist is a JavaScript logger library for error and log messages with provisions for console, file and remote logging of program status information. It also makes provision for grouping logs, assigning severity as well as triggering notifications based on severity levels.

Motivation

Logpersist was built because I needed a persistent way to record and query error and log messages while my program is in production environment and I believe other developers would have same issues too.

What you get

  • The ability to persistently log messages to any of console, file or remote API.
  • Grouping of messages and assigning of severity levels
  • Possibility of triggering notifications based on specified severity levels
  • Recording of stack trace from exceptions thrown within the program

How to use

npm install --save logpersist
const Logger = require('logpersist');
const sendGrid = require('sendGrid')
let configOptions = {
	destination:{type:"file", address:"messages.log"},
	group:"basics",
	environment: "dev",
	notifications : {
		"1":[],
		"2":[],
		"3":[],
		"4":[],
		"5":[]
	},
	notificationService:sendGrid.email

}
const consoleLogger = new Logger(configOptions);
consoleLogger.log("User just signed up"); // logs the message to the file messages.log
let err = new Error("Failed to authenticate user");
consoleLogger.log(err); // logs the error to the file messages.log

let logOptions = {
	destination:{type:"file", address:"messages.log"},
	group: "basics",
	from: Date.now(),
	to: Date.now(),
	severity: 2,
	source: "index.js",
}
consoleLogger.getLogs(logOptions); // returns an array of message objects
/*
	[
		{
			source:String,
			message:String,
			time:Date,
			name:String,
			group:String,
			severity:Number,
			trace:String
		}
	]
*/

You can create multiple logger objects and if an option argument is not provided at instantiation time, it uses the default configuration which is:

	let configOptions = {
		destination:{type:"file", address:"errors.log"},
		group:"general",
		environment: "dev",
		notifications : {
			"1":[],
			"2":[],
			"3":[],
			"4":[],
			"5":[]
		}
	}
	// notification keys are severity levels and the value is an array of email addresses of people to notify

API

Methods on the logger object

  • Logger.log()
  • Logger.getLogs()

Example usage for the API

The log method can take a second options argument which can be used to specify any of the group, severity, name and destination of the log message. If any of the options list of properties is absent, the defaults from the Logger object are used and if the options argument is absent, the defaults from the logger object at instantiation time are used.

	let fileLogger = new Logger();
	fileLogger.log("User just signed in");

	function findUser(userId, function(err,user){
		if(err){
			fileLogger.log(err, {
				group:"userSearchErrors",
				severity:2,
				name:"user search",
				destination:{type:"file", address:"usersearch.log"}
			})
			return err;
		}
		return user;
	});

	

The getLogs method can take no argument wherein in uses the object instantiation configuration options to search for logs or it takes a single argument which can be a number (in this case it get logs based on the specified severity), a string (in this case it get logs based on their group name) or an object (in this case it gets the logs based on a set of retrieval options)

	fileLogger.getLogs(); // returns an array of logged messages
	let retrievalOptions = {
		destination:{type:"file", address:"messages.log"},
		source:"index.js",
		severity: 2,
		group:"userSearchErrors",
		from: Date.now(),
		to: (new Date("2/2/2020")).getTime()
	}
	fileLogger.getLogs("userSearchErrors");
	fileLogger.getLogs(2);
	fileLogger.getLogs(retrievalOptions)

Contributing

In case you have any ideas, features you would like to be included or any bug fixes, you can send a PR.

(Requires Node v6 or above)

  • Clone the repo
git clone https://github.com/ChukwuEmekaAjah/logpersist.git