Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 15, 2017
1 parent 9163710 commit 84dfb20
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 95 deletions.
138 changes: 69 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,48 @@ npm install acho

## Usage

### Logging levels

<p><details>
<summary>
<b>Examples</b>
</summary>
<ul><li><a href="./examples/levels.js">Defaults</a></li><li><a href="./examples/skin-cli.js">Skin CLI</a></li><li><a href="./examples/skin-syslog.js">Skin Syslog</a></li></ul>
</details></p>

<p align="center">
<br>
<img src="docs/images/10.png" alt="acho">
<br>
</p>

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')
```

<p align="center">
<br>
<img src="docs/images/01.png" alt="acho">
<br>
</p>
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.
Expand All @@ -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.
Expand All @@ -108,22 +112,21 @@ We define `.push` as accumulator for store the log internally:
</p>

```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`:

<p align="center">
<br>
<img src="docs/images/03.png" alt="acho">
<br>
</p>

```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.
Expand All @@ -135,12 +138,19 @@ The method `.add` combine `.push` and `.print` actions in one: It store the mes
</p>

```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

<p><details>
<summary>
<b>Examples</b>
</summary>
<ul><li><a href="./examples/interpolation.js">Interpolation</a></li></ul>
</details></p>

<p align="center">
<br>
<img src="docs/images/09.png" alt="acho">
Expand All @@ -161,44 +171,41 @@ 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: {
foo: 'bar',
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**.
<p><details>
<summary>
<b>Examples</b>
</summary>
<ul><li><a href="./examples/trace.js">Trace & Diff</a></li><li><a href="./examples/uppercase.js">Uppercase</a></li></ul>
</details></p>

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:

<p align="center">
<br>
Expand All @@ -207,26 +214,21 @@ But you can easily modify the output. For example, let's add a trace to each mes
</p>

```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

Expand Down Expand Up @@ -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.
Expand Down
Binary file modified docs/images/00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/resume.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 5 additions & 26 deletions examples/levels.js
Original file line number Diff line number Diff line change
@@ -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')
})
2 changes: 2 additions & 0 deletions examples/skin-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const log = acho({
keyword: 'symbol'
})

console.log()

const types = Object.keys(log.types)
types.forEach(type => {
log[type]('hello world')
Expand Down
15 changes: 15 additions & 0 deletions examples/skin-syslog.js
Original file line number Diff line number Diff line change
@@ -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')
})
29 changes: 29 additions & 0 deletions examples/trace.js
Original file line number Diff line number Diff line change
@@ -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)
})
9 changes: 9 additions & 0 deletions examples/uppercase.js
Original file line number Diff line number Diff line change
@@ -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')
})

0 comments on commit 84dfb20

Please sign in to comment.