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

activity-logger

v1.4.0

Published

track time to complete activities to any output

Downloads

96

Readme

activity-logger npm version Build Status ghit.me

Log activities, showing the time it takes to complete them.

Track how long certain activities take in your application.

Think of it is a more powerful and expressive console.time, one that lets you customize not only what is logged out but also where it goes. You can pipe messages to console.log, process.stdout, or write to the file system however you like!

Super inspired by react-native's Activity module.

Install

$ npm install --save activity-logger

Usage

Basic usage.

const activity = require('activity-logger');

var ovenActivity = activity.start('Heating up the oven.');
// logs -> 'Heating up the oven';

setTimeout(function() {
	activity.end(ovenActivity);
	// logs -> 'Heating up the oven (1234ms)'
}, 1234);

You can customize what the output looks like.

activity.setStartFormatter(function(activity) {
	return '[' + activity.id + '] ' + activity.message + ' @ ' +
		new Date(activity.timestamps[0]).toLocaleTimeString();
});

var activityId = activity.start('Booting up');
// logs -> '[2] Booting up @ 9:51:26 AM'

activity.setEndFormatter(function(activity) {
	return '[' + activity.id + '] ' + activity.message + ' @ ' +
		new Date(activity.timestamps[0]).toLocaleTimeString() +
		' - COMPLETED (' + (activity.timestamps[1] - activity.timestamps[0]) + 'ms)';
});

setTimeout(function() {
	activity.end(activityId);
	// logs -> '[2] Booting up @ 9:51:26 AM - COMPLETED (23ms)'
}, 23);

And you can change where the output is sent!

activity.setOutputHandlers(function(message) {
	fs.appendFileSync('./log.txt', message, 'utf8');
});

// Now every message is appended to our log file.

API

activity.start(activityMessage)

Returns the activityId you can later use to end the activity. Logs out to console that the activity has started.

activityMessage

Type: string

The message you want outputted.

activity.end(activityId)

Ends the activity and returns the Activity object. Logs out to the console the activity ended, showing the time it took to complete.

activityId

Type: number

The id of the activity we want to end.

activity.create(activityMessage)

Returns the activityId you can later use to end the activity. Does not create any timestamp, just registers a new activity.

activityMessage

Type: string

The message you want outputted.

activity.mark(activityId)

Adds a new timestamp to the Activity's array of timestamps.

activityId

Type: number

The id of the activity.

activity.destroy(activityId)

Returns Activity object after removing it from internal cache of activities. Does not modify Activity object at all.

activityId

Type: number

The id of the activity.

activity.disable()

Disable output.

activity.enable()

Enable output.

activity.setStartFormatter(formatter)

Change how start events are outputted.

formatter

Type: function

Formatter is given an activity object. It must return a string as that is given to each function in outputHandlers array.

activity

{
	"id": 21,
	"message": "Message string given from activity.start",
	"timestamps": [1453692902523]
}

activity.setEndFormatter(formatter)

Change how end events are outputted.

formatter

Type: function

Formatter is given an activity object. It must return a string as that is given to each function in the outputHandlers array. In the end event the timestamps array has a second value to calculate the time spread.

activity

{
	"id": 21,
	"message": "Message string given from activity.start",
	"timestamps": [1453692902523, 1453692905599]
}

activity.getOutputHandlers()

Returns all outputHandlers.

activity.setOutputHandlers(...handlers)

Overwrite the existing outputHandlers array.

handlers

Type: ...function

A variable number of new functions to set as outputHandlers.

activity.addOutputHandler(handler)

Add one handler to the outputHandlers array.

handlers

Type: function

License

MIT © Harry Wolff