Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 13, 2015
1 parent edac5ab commit 1c12565
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 48 deletions.
129 changes: 123 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# acho

[![Build Status](http://img.shields.io/travis/Kikobeats/acho/master.svg?style=flat)](https://travis-ci.org/Kikobeats/acho)
[![Coverage Status](http://img.shields.io/coveralls/Kikobeats/acho/master.svg?style=flat)](https://coveralls.io/r/Kikobeats/acho?branch=master)
[![Dependency status](http://img.shields.io/david/Kikobeats/acho.svg?style=flat)](https://david-dm.org/Kikobeats/acho)
[![Dev Dependencies Status](http://img.shields.io/david/dev/Kikobeats/acho.svg?style=flat)](https://david-dm.org/Kikobeats/acho#info=devDependencies)
[![NPM Status](http://img.shields.io/npm/dm/acho.svg?style=flat)](https://www.npmjs.org/package/acho)
[![Gittip](http://img.shields.io/gittip/Kikobeats.svg?style=flat)](https://www.gittip.com/Kikobeats/)

**NOTE:** more badges availables in [shields.io](http://shields.io/)
> A extremely (but powerful) logging system for NodeJS and browser.
# Why

* Extremely basic and easy to use, customize and extend.
* Expressive API with chaineable methods.
* Mininum dependencies, just focused in one thing.

> A dead simple logger

## Install

Expand All @@ -31,10 +35,123 @@ and later link in your HTML:

## Usage

## API
### First steps

## License
For use it, basically you need to create a new logger instance.

```js
var Acho = require('acho');
var acho = new Acho({color: true});
```

It's time to use it!

```js
acho.info('hello world');
// => 'hello world'
```

All public methods for use the library are chaineables:

```js
acho
.info('hello world')
.error('something bad happens');
// => 'info: hello world'
// => 'error: 'something bard happens'
```

Maybe you don't want output the message, but store it and for later use it can be a good idea:

```js
acho.push('success', 'good job!');
console.log(acho.messages.success);
// => ['good job']
```

If you want to print later, just call the method `print`:

```js
acho.print()
// => 'success: good job!'
```

At this moment maybe you are thinking: Can I combine print the message with store the message? Absolutely!

```js
acho.add('info', 'this message is printed and stored');
// => 'info: 'this message is printed and stored'
console.log(acho.messages.info)
// => ['this message is printed and stored']
```

because `messages` is public you can define your own way to print the messages:

```json
```

Also you can do anything you need: change the color of a type, add or modify the types, changes the priorities,... **you have the power**.

MIT © [Kiko Beats](http://www.kikobeats.com)

### Stablish the level

Stablishing the level of your logs is a good way to avoid some information that maybe you don't want to output. The error levels are:

- `error`: Display calls to `.error() messages`.
- `warning`: Display calls from `.error()`, `.warning() messages`.
- `success`: Display calls from `.error()`, `.warning(), `success()` messages`.
- `info`: Display calls from `.error()`, `.warning(), `success()`, `info()` messages`.
- `verbose`: Display calls from `.error()`, `.warning(), `success()` `info()`, `verbose()` messages`.
- `debug`: Display calls from `.error()`, `.warning(), `success()` `info()`, `verbose()` `debug()` messages`.
- `silly`: Display calls from `.error()`, `.warning(), `success()` `info()`, `verbose()` `debug()` `silly()` messages`
- `silent`: Avoid all.

The default log level is `info`. You can do it in the the constructor:

```js
var acho = new Acho({level: 'silly'})
```

or in any time:

```js
acho.level = 'debug';
```

### Customization

By default the messages structure is minimal: Just the message type followed by the content of the message.

But you can easily customize it, for example, adding an timestamp for each message.

For do it, we offer two methods, `outputType` and `outputMessage`:

```js
acho = new Acho({
color: true,
level: 'silly',
outputType: function(type) {
return '[' + type + '] »';
},

outputMessage: function(message) {
return Date() + ' :: ' + message;
}
});
```

Now is moment to your awesome output:

```
acho.info('I have hungry');
// => '[ info ] » Fri Mar 13 2015 18:12:48 GMT+0100 (CET) :: I have hungry'
```

You can change how to output the message in the constructor or in any time.


## API

## License

MIT © [Kiko Beats](http://www.kikobeats.com)
72 changes: 33 additions & 39 deletions lib/Acho.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@

chalk = require 'chalk'

# 'error' : Display calls to `.error()`
# 'warn' : Display calls from `.error()` to `.warn()`
# 'debug' : Display calls from `.error()`, `.warn()` to `.debug()`
# 'info' : Display calls from `.error()`, `.warn()`, `.debug()` to `.info()`
# 'verbose': Display calls from `.error()`, `.warn()`, `.debug()`, `.info()` to `.verbose()`
DEFAULT =
OUTPUT_TYPE: (type) -> "#{type}\t: "
OUTPUT_MESSAGE: (message) -> message
TYPES:
error:
level : 0
color : 'red'
warning:
level : 1
color : 'yellow'
success:
level : 2
color : 'green'
info:
level : 3
color : 'white'
verbose:
level : 4
color : 'cyan'
debug:
level : 5
color : 'blue'
silly:
level : 6
color : 'rainbow'

module.exports = class Acho

constructor: (options) ->
@color = if options.color? then options.color else false
@level = if options.level? then options.level else 'info'
@types = if options.types? then options.types else DEFAULT.TYPES
@messages = {}
@messages[type] = [] for type of Acho.types
@outputType = options.outputType if options.outputType?
@outputMessage = options.outputMessage if options.outputMessage?
@messages[type] = [] for type of @types
@outputType = if options.outputType? then options.outputType else DEFAULT.OUTPUT_TYPE
@outputMessage = if options.outputMessage? then options.outputMessage else DEFAULT.OUTPUT_MESSAGE
this

outputType: (type) -> "#{type}: "

outputMessage: (message) -> message

push: (type, message) ->
@messages[type].push message
this

track: (type, message) ->
add: (type, message) ->
@[type] message
@push type, message
this
Expand Down Expand Up @@ -60,36 +77,13 @@ module.exports = class Acho
@_messageBuilder 'silly', message
this

@types:
error:
level : 0
color : 'red'
warning:
level : 1
color : 'yellow'
success:
level : 2
color : 'green'
info:
level : 3
color : 'white'
verbose:
level : 4
color : 'cyan'
debug:
level : 5
color : 'blue'
silly:
level : 6
color : 'rainbow'

print: ->
for type of Acho.types
for type of @types
@_messageBuilder(type, message) for message in @messages[type]

_isInLevel: (type) ->
return false if @level is 'silent'
Acho.types[type].level <= Acho.types[@level].level
@types[type].level <= @types[@level].level

_colorizeMessage: (color, message) ->
return message unless @color
Expand All @@ -104,7 +98,7 @@ module.exports = class Acho

_messageBuilder: (type, message) ->
return unless @_isInLevel type
colorType = Acho.types[type].color
colorType = @types[type].color
messageType = @outputType(type)
messageType = @_colorizeMessage(colorType, messageType)
message = @outputMessage(message)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acho",
"description": "A dead simple logger",
"description": "A extremely (but powerful) logging system for NodeJS and browser.",
"version": "0.1.0",
"author": {
"name": "Kiko Beats",
Expand Down Expand Up @@ -29,7 +29,7 @@
"node": "*"
},
"homepage": "Kikobeats/acho",
"keywords": [],
"keywords": ["log", "logging"],
"license": "MIT",
"main": "./index.js",
"repository": "Kikobeats/acho",
Expand Down
2 changes: 1 addition & 1 deletion test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe 'Acho ::', ->
@acho.messages.error.length.should.be.equal 1

it 'add a message and print', ->
@acho.track 'error', 'hello world'
@acho.add 'error', 'hello world'
@acho.messages.error.length.should.be.equal 2

it 'print a normal message', ->
Expand Down

0 comments on commit 1c12565

Please sign in to comment.