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

protolog

v0.0.5

Published

A global, enhanced logger for Node and the browser

Downloads

7

Readme

Protolog

NPM

Introduction

Protolog as a global .log method to all variables in JavaScript. It basically monkeypatches console.log into the Object prototype. The idea is to add a simple shorcut to logging by making all variables loggable, like this:

'a'.log()

Protolog In the Browser

The .log method returns the object itself. In this way, you can plug .log statements anywhere in your code without it interfering with the flow of your code.

var a = 5;

5 === a.log() // true
a.log() === a // true

Showing equality in the browser

if (a === 5) {
  doSomething();
}

Is the same as:

if (a.log() === 5) {
  doSomething();
}

Alternatively, you can just require the module and use it as a replacement for console.log:

var log = require('protolog/local')();

log('hello');

Install

If in node, use npm:

npm install protolog

If in the browser, use bower or the CDN:

bower install protolog

Options

| name | type | default | |--------|----------|---------| | name | String | 'log' |

Change the name of the the method to be used for logging. This will be the name of the property to be appended to the Object prototype.

| name | type | default | |---------------------|-----------|---------| | appendToPrototype | Boolean | true |

Specify wether the .log method will be appended to the Object prototype and hence be available in all variables. If false, you can require this function and then use the library as a normal function. This will be useful once the library has unique methods that enhance logging.

Methods

| Method Name | |--------| | color | | bold | | underline | | background | | table |

color(colorName <String>)

Change the text color of your logs using one of the available colors.

“hello”.color(‘green’).log();

| name | type | default | |---------------------|-----------|---------| | color | String | black |

See available colors

bold(colorName <String>)

Make the text bold and change the text color of your logs and using one of the available colors.

“hello”.bold(‘green’).log();

| name | type | default | |---------------------|-----------|---------| | bold | String | black |

See available colors

underline(colorName <String>)

Underline the text bold and change the text color using one of the available colors.

“hello”.underline(‘green’).log();

| name | type | default | |---------------------|-----------|---------| | underline | String | black |

See available colors

background(colorName <String>)

Change the background color of your logs using one of the available colors.

“hello”.background(red).log();

| name | type | default | |---------------------|-----------|---------| | background | String | black |

See available colors

table()

Display the values of a variable as a table.

Strings, numbers, booleans, null and undefined get displayed as a single column in a single row:

“hello”.table().log();

Displays:

+-------+
| hello |
+-------+

Arrays of a single dimension get displayed as a table with multiple rows with two columns: an index and a value.

['hello', 'goodbye', 'wow'].table().log();

Displays:

+---+---------+
| 0 | hello   |
+---+---------+
| 1 | goodbye |
+---+---------+
| 2 | wow     |
+---+---------+

Arrays of two dimensions (where all values inside the arrays are also arrays) get displayed as a table with multiple rows and multiple columns. The first column is the index of the first dimension and the first row is the index for the second dimension.

[[1, 2], [4, 5, 6], [7]].table().log();

Displays:

+---+---+---+---+
|   | 0 | 1 | 2 |
+---+---+---+---+
| 0 | 1 | 2 |   |
+---+---+---+---+
| 1 | 4 | 5 | 6 |
+---+---+---+---+
| 2 | 7 |   |   |
+---+---+---+---+

Method Chaining

You can chain methods together to log something in different ways.

Making a log green, bold and underlined.

var l = require(‘protolog’)();
log.color(‘hello’, ‘green’).bold().underline().log();

Creating a green table:

require(‘protolog’)();
[1, 2, 3].log.color(‘green’).table().log();

Available Colors

Available colors are:

| color | |--------| | Black | | Red | | Green | | Yellow | | Blue | | Purple | | Cyan | | Pink | | Grey | | White |

Is This A Terrible Idea?

Perhaps. But, thankfully, you don't have to use it :).

Monkey-patching is definitely questionable, but the behavior provided by this library is both: very useful and universal. The .log method is nothing more than a thin wrapper over console.log. The method also implemented responsibly, taking inspiration with should.js's implementation.

The Future

In the future, this library will include other features for logging in the console/REPL. The two main features included will be transformations: transforming the data you want to log, and display methods: changing the way in which the data is displayed.