Skip to content

Commit

Permalink
Merge pull request #28 from OmgImAlexis/master
Browse files Browse the repository at this point in the history
lint code, fix syntax highlighting and change var -> const
  • Loading branch information
starak authored Sep 28, 2017
2 parents 6259131 + 43a9e24 commit 899de3c
Showing 1 changed file with 115 additions and 111 deletions.
226 changes: 115 additions & 111 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ This module enables you to patch the console's methods in Node.js, to add timest
## Usage ##

### Install

npm install console-stamp
```console
npm install console-stamp
```

### Patching the console

require("console-stamp")(console, [options]);
```js
require('console-stamp')(console, [options]);
```

#### console
The global console or [custom console](#customconsole).
Expand All @@ -27,7 +29,7 @@ The global console or [custom console](#customconsole).

From version 2.0 the second parameter is an object with several options. As a backward compatibillity feature this parameter can be a string containing the pattern.

* **options.pattern** {String}<br>A string with date format based on [Javascript Date Format](http://blog.stevenlevithan.com/archives/date-time-format)<br>**Default**: "ddd mmm dd yyyy HH:MM:ss"
* **options.pattern** {String}<br>A string with date format based on [Javascript Date Format](http://blog.stevenlevithan.com/archives/date-time-format)<br>**Default**: 'ddd mmm dd yyyy HH:MM:ss'

* **options.formatter** {Function}<br>A custom date formatter that should return a formmatted date string.

Expand All @@ -43,7 +45,7 @@ From version 2.0 the second parameter is an object with several options. As a ba

* **options.disable** {Array}<br>An array containing the methods to disable in the patch<br>**Default**: [] \(none)

* **options.level** {String}<br>A string choosing the most verbose logging function to allow. Ordered/grouped as such: "log dir", "info", "warn assert", "error"<br>**Default**: log
* **options.level** {String}<br>A string choosing the most verbose logging function to allow. Ordered/grouped as such: 'log dir', 'info', 'warn assert', 'error'<br>**Default**: log

* **options.extend** {Object}<br>An object describing methods and their associated log level, to extend the existing `method <-> log level` pairs.<br>For an example see [Custom methods](#custommethods).

Expand All @@ -65,70 +67,70 @@ From version 2.0 the second parameter is an object with several options. As a ba

* **options.dateSuffix** {String}<br>A custom suffix for the datestamp.<br>For an example see [Custom prefix and suffix example](#custom-pre-and-suffixes)<br>**Default:** "]"
Note: To combine colors, bgColors and style, set them as an array like this:

...
stamp: ["black", "bgYellow", "underline"]
...

```js
...
stamp: ['black', 'bgYellow', 'underline']
...
```

Or chain Chalk functions like this:

...
stamp: require("chalk").red.bgYellow.underline;
...

```js
...
stamp: require('chalk').red.bgYellow.underline;
...
```

Note also that by sending the parameter `--no-color` when you start your node app, will prevent any colors from console.

$ node my-app.js --no-color

```console
$ node my-app.js --no-color
```
### Example
```js
// Patch console.x methods in order to add timestamp information
require('console-stamp')(console, { pattern: 'dd/mm/yyyy HH:MM:ss.l' });

// Patch console.x methods in order to add timestamp information
require( "console-stamp" )( console, { pattern : "dd/mm/yyyy HH:MM:ss.l" } );

console.log("Hello World!");
// -> [26/06/2015 14:02:48.062] [LOG] Hello World!

var port = 8080;
console.log("Server running at port %d", port);
// -> [26/06/2015 16:02:35.325] [LOG] Server running at port 8080
console.log('Hello World!');
// -> [26/06/2015 14:02:48.062] [LOG] Hello World!

const port = 8080;
console.log('Server running at port %d', port);
// -> [26/06/2015 16:02:35.325] [LOG] Server running at port 8080
```
&nbsp;

console.log( "This is a console.log message" );
console.info( "This is a console.info message" );
console.warn( "This is a console.warn message" );
console.error( "This is a console.error message" );
console.dir( {bar: "This is a console.dir message"} );

```js
console.log('This is a console.log message');
console.info('This is a console.info message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');
console.dir({bar: 'This is a console.dir message'});
```
Result:

[26/06/2015 12:44:31.777] [LOG] This is a console.log message
[26/06/2015 12:44:31.777] [INFO] This is a console.info message
[26/06/2015 12:44:31.779] [WARN] This is a console.warn message
[26/06/2015 12:44:31.779] [ERROR] This is a console.error message
[26/06/2015 12:44:31.779] [DIR] { bar: 'This is a console.dir message' }

```console
[26/06/2015 12:44:31.777] [LOG] This is a console.log message
[26/06/2015 12:44:31.777] [INFO] This is a console.info message
[26/06/2015 12:44:31.779] [WARN] This is a console.warn message
[26/06/2015 12:44:31.779] [ERROR] This is a console.error message
[26/06/2015 12:44:31.779] [DIR] { bar: 'This is a console.dir message' }
```
and
```js
require('console-stamp')(console, {
metadata: function () {
return ('[' + process.memoryUsage().rss + ']');
},
colors: {
stamp: 'yellow',
label: 'white',
metadata: 'green'
}
});

require( "console-stamp" )( console, {
metadata: function () {
return ("[" + process.memoryUsage().rss + "]");
},
colors: {
stamp: "yellow",
label: "white",
metadata: "green"
}
} );

console.log( "This is a console.log message" );
console.info( "This is a console.info message" );
console.warn( "This is a console.warn message" );
console.error( "This is a console.error message" );
console.dir( {bar: "This is a console.dir message"} );

console.log('This is a console.log message');
console.info('This is a console.info message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');
console.dir({bar: 'This is a console.dir message'});
```
Result:

![Console](gfx/console.png)
Expand All @@ -138,17 +140,16 @@ Result:

As of version 0.2.4 you can also create a custom console with its own `stdout` and `stderr` like this:

```js
const fs = require('fs');
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
const logger = new console.Console(output, errorOutput);

```
var fs = require( 'fs' );
var output = fs.createWriteStream( './stdout.log' );
var errorOutput = fs.createWriteStream( './stderr.log' );
var logger = new console.Console( output, errorOutput );
console_stamp( logger, {
stdout: output,
stderr: errorOutput
} );
console_stamp(logger, {
stdout: output,
stderr: errorOutput
});
```

Everything is then written to the files.
Expand All @@ -159,39 +160,40 @@ Everything is then written to the files.
### Custom Formatter Example

Custom formatter using moment.js
```js
const moment = require('moment');
moment.locale('ja');

var moment = require('moment');
moment.locale('ja');

require( "console-stamp" )( console, {
formatter:function(){
return moment().format("LLLL");
}
} );
require('console-stamp')(console, {
formatter: function() {
return moment().format('LLLL');
}
});

console.log( "This is a console.log message" );
console.info( "This is a console.info message" );
console.warn( "This is a console.warn message" );
console.error( "This is a console.error message" );
console.dir( {bar: "This is a console.dir message"} );
console.log('This is a console.log message');
console.info('This is a console.info message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');
console.dir({bar: 'This is a console.dir message'});
```

Result:

[2016年5月12日午前11時10分 木曜日] [LOG] This is a console.log message
[2016年5月12日午前11時10分 木曜日] [INFO] This is a console.info message
[2016年5月12日午前11時10分 木曜日] [WARN] This is a console.warn message
[2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error message
[2016年5月12日午前11時10分 木曜日] [DIR] { bar: 'This is a console.dir message' }

```console
[2016年5月12日午前11時10分 木曜日] [LOG] This is a console.log message
[2016年5月12日午前11時10分 木曜日] [INFO] This is a console.info message
[2016年5月12日午前11時10分 木曜日] [WARN] This is a console.warn message
[2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error message
[2016年5月12日午前11時10分 木曜日] [DIR] { bar: 'This is a console.dir message' }
```
<a name="custommethods"></a>
### Custom Methods

The **option.extend** option enables the extention or modification of the logging methods and their associated log levels:
The **option.extend** option enables the extension or modification of the logging methods and their associated log levels:

The default logging methods and their log levels are as follows:

```javascript
var levelPriorities = {
```js
const levelPriorities = {
log: 4,
info: 3,
warn: 2,
Expand All @@ -203,7 +205,7 @@ var levelPriorities = {

Combined with the **include** option, the **extend** option enables the usage of custom console logging methods to be used with this module, for example:

```javascript
```js
// Extending the console object with custom methods
console.debug = function(msg) {
console.log(msg);
Expand All @@ -215,13 +217,13 @@ console.fatal = function(msg) {

// Initialising the output formatter
require('console-stamp')(console, {
pattern: "HH:MM:ss",
pattern: 'HH:MM:ss',
extend: {
debug: 5,
fatal: 0,
},
include: ["debug", "info", "warn", "error", "fatal"],
level: "debug",
include: ['debug', 'info', 'warn', 'error', 'fatal'],
level: 'debug',
});
```

Expand All @@ -233,27 +235,29 @@ Types can be string, object (interpreted with util.inspect), or function.
See the [test-metadata.js](https://github.com/starak/node-console-stamp/blob/master/test-metadata.js) for examples.

#### String example
```js
require('console-stamp')(console, {
pattern: 'HH:MM:ss.l',
metadata: '[' + process.pid + ']'
});

require("console-stamp")(console, {
pattern:"HH:MM:ss.l",
metadata:'[' + process.pid + ']'
});

console.log('Metadata applied.');

console.log('Metadata applied.');
```
Result:

[26/06/2015 12:44:31.779] [LOG] [7785] Metadata applied.
```console
[26/06/2015 12:44:31.779] [LOG] [7785] Metadata applied.
```

#### Function example
```js
const util = require('util');

var util = require("util");

require("console-stamp")(console, {
pattern:"HH:MM:ss.l",
metadata: function(){ return '[' + (process.memoryUsage().rss) + ']'; });
require('console-stamp')(console, {
pattern: 'HH:MM:ss.l',
metadata: function(){ return '[' + (process.memoryUsage().rss) + ']'; });

console.log('Metadata applied.');
console.log('Metadata applied.');
```
Result:
Expand Down

0 comments on commit 899de3c

Please sign in to comment.