protolog
v0.0.5
Published
A global, enhanced logger for Node and the browser
Downloads
7
Readme
Protolog
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()
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
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
|
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
|
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
|
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
|
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.