Pretty Debug JS

A minimal debug library written in JavaScript

Posted by Shadly on July 1, 2019

logo

npm package

forthebadge forthebadge

-----------------------------------------------------

Intro

A highly configurable & lightweight debug library that prints debug messages beautifully. It works in Node.js and Web browser environments with very low memory footprint.

Features

  • Beautifully colored and formatted output texts
  • Pretty prints current file name, function and line number
  • Pretty prints date, time with timezone and formatting
  • Attach multiple streams and pipe the debug outputs
  • Compatible with Node.js stream objects
  • Node process and System process memory monitor
  • Schedule health check
  • Alarm on crossing memory usage thresholds
  • System memory high watermark
  • Highly configurable
  • Very low memory footprint
  • No third party library dependency

Screenshot

Sample Output

Install

1
$ npm install pretty-debug

Usage

Pretty Debug exposes a debug object. Just attach any stream you want to the module to pipe debug messages. process.stdout is the default steam which prints output to console. Any other streams like TCP socket or request to an HTTP server can be attached on runtime to pipe the debug output.

API Documentation

For detail information, please see API Documentation.

Basic usage

  • Create debug instance :
    1
    
      const debug = require('pretty-debug');
    
  • Set options :
    1
    2
    3
    4
    5
    6
    7
    
      debug.setOptions({ 
              enable: true,		// Turning on debug print
              enableColor: true,	// Enabling color output
              enableGC: true,		// Enabling autometic gerbage collection
              debugLevel: 6, 		// Setting debug level 6 (Upto INFO)
          }  
      });
    
  • Attach stream :
    1
    
      debug.attachStream(sock); 	// Attaching new stream to serve debug messages
    
  • Detach stream :
    1
    
      debug.detachStream(sock); 	// Detaching stream
    
  • Print in different debug levels :
    1
    2
    3
    4
    5
    6
    
      debug.log('The quick brown fox jumps over the lazy dog');
      debug.info('The quick brown fox jumps over the lazy dog');
      debug.alert('The quick brown fox jumps over the lazy dog');
      debug.warn('The quick brown fox jumps over the lazy dog');
      debug.error('The quick brown fox jumps over the lazy dog');
      debug.critical('The quick brown fox jumps over the lazy dog');
    
  • Show highest RAM usage :
    1
    
      debug.memoryWatermark();	// Shows highest RAM usage in MB.
    
  • Show memory used by OS :
    1
    
      debug.sysMemoryMonitor();	// Shows memory information of operating system
    
  • Show memory used by Node.js :
    1
    
      debug.nodeMemoryMonitor(();	// Shows memory information of Node.js system
    
  • Set alarm policy for RAM usage :
    1
    2
    3
    4
    5
    6
    
      debug.nodeMemoryMonitor({
          heapTotal: { upperLimit : 5 } // Trigger alarm when heap usage is greater than 5 MB
      }, function(){
          debug.critical('Memory Usage Alarm : Total heap usage is above 5 MB');
          // Do other things like send email!
      });
    

Example

In this example a debug instance is created. For multiple stream demonstration, a TCP server is created. TCP socket is attached to the debug module. So when a client connects to that TCP server, color debug output will be shown on the client console. Health check scheduler is also demonstrated in this example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const net = require('net');
const debug = require('pretty-debug');

var PORT = 6969;
var HOST = '0.0.0.0';

debug.setOptions({
	nodeMemoryMonitor:{
		fields: {
			rss: false,
			external: false
		}
	}
});

debug.info('Hello Pretty World');

debug.scheduleHealthCheck(function(){
	debug.memoryWatermark();
	debug.sysMemoryMonitor();
	debug.nodeMemoryMonitor({
			heapTotal: { upperLimit : 5 }
		}, function(){
			debug.critical('Memory Usage Alarm : Total heap usage is above 5 MB');
			// Do other things like send email!
		});
}, .02);

net.createServer(function(sock){
	debug.attachStream(sock);
	sock.on('end', function() {
		debug.detachStream(sock);
	}); 
}).listen(PORT, HOST);

function generateRandomLog(){
	debug.log('The quick brown fox jumps over the lazy dog');
	debug.info('The quick brown fox jumps over the lazy dog');
	debug.alert('The quick brown fox jumps over the lazy dog');
	debug.warn('The quick brown fox jumps over the lazy dog');
	debug.error('The quick brown fox jumps over the lazy dog');
	debug.critical('The quick brown fox jumps over the lazy dog');
}

setInterval(function(){
	generateRandomLog();
}, 678)

Run Example

To run the example above, simply put this command and press Enter!

1
$ node Examples/example.js

Sample Output

Sample Output

Tutorials

All the tutorials and examples can be found in the link below :

Customization

As it was mentioned earlier, this library is highly configurable. For more details, please see Default Options.

Contributing

If you are interested in requesting a feature, fixing issues and contributing directly to the code base, please do not hesitate.

License

Licensed under the MIT License.