diff --git a/README.md b/README.md index 001f2a3..d4bb411 100755 --- a/README.md +++ b/README.md @@ -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 @@ -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) \ No newline at end of file diff --git a/lib/Acho.coffee b/lib/Acho.coffee index 2ce4764..165a604 100644 --- a/lib/Acho.coffee +++ b/lib/Acho.coffee @@ -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 @@ -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 @@ -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) diff --git a/package.json b/package.json index 162da3d..cc47cf8 100755 --- a/package.json +++ b/package.json @@ -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", @@ -29,7 +29,7 @@ "node": "*" }, "homepage": "Kikobeats/acho", - "keywords": [], + "keywords": ["log", "logging"], "license": "MIT", "main": "./index.js", "repository": "Kikobeats/acho", diff --git a/test/test.coffee b/test/test.coffee index 9b9ee72..87a0dfb 100755 --- a/test/test.coffee +++ b/test/test.coffee @@ -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', ->