diff --git a/README.md b/README.md index a2f6ea1..641ab78 100755 --- a/README.md +++ b/README.md @@ -31,38 +31,48 @@ npm install acho ## Usage +### Logging levels + +

+ + Examples + + +

+ +

+
+ acho +
+

+ The first thing you need to do is create a new log instance: ```js +const acho = require('acho') const log = acho() ``` -Then you can print one of the defaults logging levels: +Then you can print a log based on the level: -``` +```js +const acho = require('acho') const log = acho() -const types = Object.keys(log.types) -types.forEach(type => { - log[type]('hello world') -}) -``` -All public methods are chainable: +acho.info('hello world') +``` -

-
- acho -
-

+All methods are chainables: ```js +const acho = require('acho') +const log = acho() + acho .info('hello world') -.error('something bad happens'); +.error('something bad happens') ``` -### Logging level - Establishing the loglevel is a good way to filter out undesired information from output. The available levels by default are: - `fatal` : Display calls to `.fatal()` messages. @@ -79,22 +89,16 @@ Additionally exists two special levels: The default log level is `all`. You can define it in the constructor: ```js +const acho = require('acho') const log = acho({level: 'debug'}) ``` or at runtime: ```js -log.level = 'debug'; +log.level = 'debug' ``` -See more at [examples/levels](https://github.com/achohq/acho/blob/master/examples/levels.js). - -### Customize logging levels - -### skin-cli -### skin-syslog - ### Internal Store Sometimes, when you are interacting with a logger you need to store the logs to be used later instead of print all of them. @@ -108,11 +112,14 @@ We define `.push` as accumulator for store the log internally:

```js -acho.push('success', 'good job', 'well done', 'great!'); -console.log(acho.messages.success); +const acho = require('acho') +const log = acho() + +log.push('success', 'good job', 'well done', 'great!') +console.log(log.messages.success) ``` -If you want to print previously stored messages, just call the method `print`: +If you want to print previously stored messages, just call the method `.print`:


@@ -120,10 +127,6 @@ If you want to print previously stored messages, just call the method `print`:

-```js -acho.print() -``` - or you can retrieve the logs programatically from the internal storage at `acho.messages` The method `.add` combine `.push` and `.print` actions in one: It store the message internally but also print the log. @@ -135,12 +138,19 @@ The method `.add` combine `.push` and `.print` actions in one: It store the mes

```js -acho.add('info', 'this message is printed and stored'); +log.add('info', 'this message is printed and stored') console.log(acho.messages.info) ``` ## Formatters +

+ + Examples + + +

+


acho @@ -161,14 +171,20 @@ We use [printf-style](https://wikipedia.org/wiki/Printf_format_string) formattin By default, the `%j` is applied when you pass an object to be logged: ```js -acho.info({hello: 'world', foo: 'bar'}) +const acho = require('acho') +const log = acho() + +log.info({hello: 'world', foo: 'bar'}) // => 'info hello=world foo=bar' ``` If you want to use a different formatter, use printf markup: ```js -acho.info('formatting with object interpolation %J', { +const acho = require('acho') +const log = acho() + +log.info('formatting with object interpolation %J', { hello: 'world', foo: 'bar', deep: { @@ -176,29 +192,20 @@ acho.info('formatting with object interpolation %J', { arr: [1, 2, 3, 4, 5] } }) - -// info formatting with object interpolation -// hello: "world" -// foo: "bar" -// deep: -// foo: "bar" -// arr: -// 0: 1 -// 1: 2 -// 2: 3 -// 3: 4 -// 4: 5 ``` -See more at [examples/formatter](https://github.com/achohq/acho/blob/master/examples/formatter.js). - ### Customization -You can completely customize the library to your requirements: changes colors, add more types, sort the priorities... the internal structure of the object is public and you can edit it dynamically. **You have the power**. +

+ + Examples + + +

-By default the messages structure is brief: Just the message type followed by the message itself. +One of the **acho** compromise is be easy to adapt. You can completely customize all the library functionalities. -But you can easily modify the output. For example, let's add a trace to each message: +For example, suppose you want to add a timestamp before your logs:


@@ -207,26 +214,21 @@ But you can easily modify the output. For example, let's add a trace to each mes

```js -var acho = Acho({ - color: true, - level: 'debug', +const acho = require('acho') +const log = acho({ // Customize how to print the 'type' of each message - outputType: function(type) { - return '[' + type + '] ยป '; - }, + outputType: type => `[${type}]`, // Customize how to print the message. // Add things before and/or after. - outputMessage: function(message) { - return Date() + ' :: ' + message; - } -}); + outputMessage: message => `${Date.now()} :: ${message}` +}) -acho.info('I am hungry'); +acho.info('I am hungry') ``` -If you need customize more the output you can setup `.print` `.generateMessage` (see below) that are a more low level methods for generate and print the output message. +That's all. ## API @@ -262,20 +264,18 @@ Default: `false` Prints trace between log from the same level. Specially useful to debug timings. -##### **{Boolean}** color - -Default: `true`. - -Enable or disable colorized output. - ##### **{Boolean}** upperCase +![](docs/images/12.png) + Default: `false`. Enable or disable print log level in upper case. ##### **{Boolean|Number}** trace +![](docs/images/11.png) + Default: `false`. Prints a numeric counter trace associated with each log line. diff --git a/docs/images/00.png b/docs/images/00.png index cab7d22..b6d71a9 100644 Binary files a/docs/images/00.png and b/docs/images/00.png differ diff --git a/docs/images/01.png b/docs/images/01.png index 0157e9f..be51e75 100644 Binary files a/docs/images/01.png and b/docs/images/01.png differ diff --git a/docs/images/02.png b/docs/images/02.png index ca10ae1..21c07e0 100644 Binary files a/docs/images/02.png and b/docs/images/02.png differ diff --git a/docs/images/03.png b/docs/images/03.png index 0d06689..4cd56ad 100644 Binary files a/docs/images/03.png and b/docs/images/03.png differ diff --git a/docs/images/04.png b/docs/images/04.png index ef254da..97ec496 100644 Binary files a/docs/images/04.png and b/docs/images/04.png differ diff --git a/docs/images/05.png b/docs/images/05.png index 8d6db69..47a9422 100644 Binary files a/docs/images/05.png and b/docs/images/05.png differ diff --git a/docs/images/06.png b/docs/images/06.png index 1f16311..ce678ff 100644 Binary files a/docs/images/06.png and b/docs/images/06.png differ diff --git a/docs/images/07.png b/docs/images/07.png index b033afd..ae77e31 100644 Binary files a/docs/images/07.png and b/docs/images/07.png differ diff --git a/docs/images/08.png b/docs/images/08.png index f3a3730..0f18ddc 100644 Binary files a/docs/images/08.png and b/docs/images/08.png differ diff --git a/docs/images/09.png b/docs/images/09.png index 523be3e..c39b056 100644 Binary files a/docs/images/09.png and b/docs/images/09.png differ diff --git a/docs/images/10.png b/docs/images/10.png new file mode 100644 index 0000000..034eff8 Binary files /dev/null and b/docs/images/10.png differ diff --git a/docs/images/11.png b/docs/images/11.png new file mode 100644 index 0000000..3518dd4 Binary files /dev/null and b/docs/images/11.png differ diff --git a/docs/images/12.png b/docs/images/12.png new file mode 100644 index 0000000..c4a7f88 Binary files /dev/null and b/docs/images/12.png differ diff --git a/docs/images/resume.png b/docs/images/resume.png index b7ed677..6daf3fd 100644 Binary files a/docs/images/resume.png and b/docs/images/resume.png differ diff --git a/examples/levels.js b/examples/levels.js index b9bdc44..40473fe 100644 --- a/examples/levels.js +++ b/examples/levels.js @@ -1,30 +1,9 @@ 'use strict' -const Acho = require('..') -const acho = Acho({ - diff: true, - trace: 1000, - // upperCase: true, - context: 'generated' - // keyword: 'symbol' -}) - -const levels = Object.keys(acho.types) - -const fixtureObj = { - foo: 'bar', - hello: 'world' -} - -const fixtureArr = [1, 2, 3, 4, 5] - -acho.debug('%j', fixtureObj) -acho.debug(fixtureObj) -acho.debug(fixtureArr) +const acho = require('acho') -levels.forEach(function (level) { - setInterval(function () { - acho[level]('This is a auto-generated ' + level + ' message') - acho[level](fixtureObj) - }, 1000) +const log = acho() +const types = Object.keys(log.types) +types.forEach(type => { + log[type]('hello world') }) diff --git a/examples/skin-cli.js b/examples/skin-cli.js index 229ff5b..987cad8 100644 --- a/examples/skin-cli.js +++ b/examples/skin-cli.js @@ -8,6 +8,8 @@ const log = acho({ keyword: 'symbol' }) +console.log() + const types = Object.keys(log.types) types.forEach(type => { log[type]('hello world') diff --git a/examples/skin-syslog.js b/examples/skin-syslog.js new file mode 100644 index 0000000..41aa227 --- /dev/null +++ b/examples/skin-syslog.js @@ -0,0 +1,15 @@ +'use strict' + +const acho = require('acho') +const skinCli = require('acho-skin-syslog') + +const log = acho({ + types: skinCli +}) + +console.log() + +const types = Object.keys(log.types) +types.forEach(type => { + log[type]('hello world') +}) diff --git a/examples/trace.js b/examples/trace.js new file mode 100644 index 0000000..1af9845 --- /dev/null +++ b/examples/trace.js @@ -0,0 +1,29 @@ +'use strict' + +const Acho = require('..') +const acho = Acho({ + diff: true, + trace: 1000 + // upperCase: true, + // keyword: 'symbol' +}) + +const levels = Object.keys(acho.types) + +const fixtureObj = { + foo: 'bar', + hello: 'world' +} + +const fixtureArr = [1, 2, 3, 4, 5] + +acho.debug('%j', fixtureObj) +acho.debug(fixtureObj) +acho.debug(fixtureArr) + +levels.forEach(function (level) { + setInterval(function () { + acho[level]('This is a auto-generated ' + level + ' message') + acho[level](fixtureObj) + }, 1000) +}) diff --git a/examples/uppercase.js b/examples/uppercase.js new file mode 100644 index 0000000..8066e9d --- /dev/null +++ b/examples/uppercase.js @@ -0,0 +1,9 @@ +'use strict' + +const acho = require('acho') + +const log = acho({upperCase: true}) +const types = Object.keys(log.types) +types.forEach(type => { + log[type]('hello world') +})