1 | # acho
|
2 |
|
3 | <p align="center">
|
4 | <br>
|
5 | <img src="https://i.imgur.com/qdpBpnw.gif" alt="acho">
|
6 | <br>
|
7 | </p>
|
8 |
|
9 | ![Last version](https://img.shields.io/github/tag/achohq/acho.svg?style=flat-square)
|
10 | [![Build Status](http://img.shields.io/travis/achohq/acho/master.svg?style=flat-square)](https://travis-ci.org/achohq/acho)
|
11 | [![Coverage Status](https://img.shields.io/coveralls/achohq/acho.svg?style=flat-square)](https://coveralls.io/github/achohq/acho)
|
12 | [![Dependency status](http://img.shields.io/david/achohq/acho.svg?style=flat-square)](https://david-dm.org/achohq/acho)
|
13 | [![Dev Dependencies Status](http://img.shields.io/david/dev/achohq/acho.svg?style=flat-square)](https://david-dm.org/achohq/acho#info=devDependencies)
|
14 | [![NPM Status](http://img.shields.io/npm/dm/acho.svg?style=flat-square)](https://www.npmjs.org/package/acho)
|
15 | [![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/kikobeats)
|
16 |
|
17 | > Simple & hackable log system for NodeJS.
|
18 |
|
19 | # Why
|
20 |
|
21 | * Easy to use, customize and extend.
|
22 | * Expressive API with chaineable methods.
|
23 | * Mininum dependencies, just focussing on one thing.
|
24 | * Compatible with AMD/CommonJS or just global object in the browser.
|
25 |
|
26 | ## Install
|
27 |
|
28 | ```bash
|
29 | npm install acho
|
30 | ```
|
31 |
|
32 | If you want to use it in the browser (powered by [Browserify](http://browserify.org/)):
|
33 |
|
34 | ```bash
|
35 | bower install acho --save
|
36 | ```
|
37 |
|
38 | and later add it to your HTML:
|
39 |
|
40 | ```html
|
41 | <script src="bower_components/acho/dist/acho.js"></script>
|
42 | ```
|
43 |
|
44 | ## Usage
|
45 |
|
46 | ### First steps
|
47 |
|
48 | Acho exports itself according to UMD best practices, which means that no matter where you are using the library, you get a version tailored for your environment.
|
49 |
|
50 | If you're using a module loader (or Node), simple require the library as you would any other module.
|
51 |
|
52 | If you're using a browser, the library falls back to attaching itself to window as the global `Acho`.
|
53 |
|
54 | #### CommonJS
|
55 |
|
56 | ```js
|
57 | var Acho = require('acho');
|
58 | var acho = Acho();
|
59 | ```
|
60 |
|
61 | #### Global/Browser
|
62 |
|
63 | ```js
|
64 | var acho = Acho();
|
65 | ```
|
66 |
|
67 | #### AMD
|
68 |
|
69 | I don't personally use AMD, so I can't conjure an example, but it should work fine as well.
|
70 |
|
71 | It's time to use it!
|
72 |
|
73 | <p align="center">
|
74 | <br>
|
75 | <img src="docs/images/00.png" alt="acho">
|
76 | <br>
|
77 | </p>
|
78 |
|
79 | ```js
|
80 | acho.info('hello world');
|
81 | ```
|
82 |
|
83 | All public methods are chainable:
|
84 |
|
85 | <p align="center">
|
86 | <br>
|
87 | <img src="docs/images/01.png" alt="acho">
|
88 | <br>
|
89 | </p>
|
90 |
|
91 | ```js
|
92 | acho
|
93 | .info('hello world')
|
94 | .error('something bad happens');
|
95 | ```
|
96 |
|
97 | Maybe you don't want to output the message, but store it for later use:
|
98 |
|
99 | <p align="center">
|
100 | <br>
|
101 | <img src="docs/images/02.png" alt="acho">
|
102 | <br>
|
103 | </p>
|
104 |
|
105 | ```js
|
106 | acho.push('success', 'good job', 'well done', 'great!');
|
107 | console.log(acho.messages.success);
|
108 | ```
|
109 |
|
110 | If you want to print previously stored messages, just call the method `print`:
|
111 |
|
112 | <p align="center">
|
113 | <br>
|
114 | <img src="docs/images/03.png" alt="acho">
|
115 | <br>
|
116 | </p>
|
117 |
|
118 | ```js
|
119 | acho.print()
|
120 | ```
|
121 |
|
122 | You might be thinking: Can I combine both, to store and both print a message? Absolutely!
|
123 |
|
124 | <p align="center">
|
125 | <br>
|
126 | <img src="docs/images/04.png" alt="acho">
|
127 | <br>
|
128 | </p>
|
129 |
|
130 | ```js
|
131 | acho.add('info', 'this message is printed and stored');
|
132 | console.log(acho.messages.info)
|
133 | ```
|
134 |
|
135 | ### Defining the level
|
136 |
|
137 | Establishing the loglevel is a good way to filter out undesired information from output. The available levels by default are:
|
138 |
|
139 | - `fatal` : Display calls to `.fatal()` messages.
|
140 | - `error` : Display calls to `.fatal()`, `.error()` messages.
|
141 | - `warn` : Display calls from `.fatal()`, `.error()`, `.warn()` messages.
|
142 | - `info` : Display calls from `.fatal()`, `.error()`, `.warn()`, `info()` messages.
|
143 | - `debug` : Display calls from `.fatal()`, `.error()`, `.warn()`, `info()`, `debug()` messages.
|
144 |
|
145 | Additionally exists two special levels:
|
146 |
|
147 | - `muted` : Avoid all output.
|
148 | - `all` : Allow print all message types.
|
149 |
|
150 | The default log level is `all`. You can define it in the constructor:
|
151 |
|
152 | ```js
|
153 | var acho = Acho({level: 'debug'})
|
154 | ```
|
155 |
|
156 | or at runtime:
|
157 |
|
158 | ```js
|
159 | acho.level = 'debug';
|
160 | ```
|
161 |
|
162 | See more at [examples/levels](https://github.com/achohq/acho/blob/master/examples/levels.js).
|
163 |
|
164 | ### Customization
|
165 |
|
166 | 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**.
|
167 |
|
168 | By default the messages structure is brief: Just the message type followed by the message itself.
|
169 |
|
170 | But you can easily modify the output. For example, let's add a timestamp to each message:
|
171 |
|
172 | <p align="center">
|
173 | <br>
|
174 | <img src="docs/images/05.png" alt="acho">
|
175 | <br>
|
176 | </p>
|
177 |
|
178 | ```js
|
179 | var acho = Acho({
|
180 | color: true,
|
181 | level: 'debug',
|
182 |
|
183 | // Customize how to print the 'type' of each message
|
184 | outputType: function(type) {
|
185 | return '[' + type + '] » ';
|
186 | },
|
187 |
|
188 | // Customize how to print the message.
|
189 | // Add things before and/or after.
|
190 | outputMessage: function(message) {
|
191 | return Date() + ' :: ' + message;
|
192 | }
|
193 | });
|
194 |
|
195 | acho.info('I am hungry');
|
196 | ```
|
197 |
|
198 | 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.
|
199 |
|
200 | ## Formatters
|
201 |
|
202 | We use [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting. Below are the officially supported formatters:
|
203 |
|
204 | | Formatter | Representation |
|
205 | |-----------|---------------------------------------------------------------|
|
206 | | `%s` | String. |
|
207 | | `%d` | Number (both integer and float). |
|
208 | | `%j` | JSON serialization in one line |
|
209 | | `%J` | JSON pretty object in multiple lines |
|
210 | | `%%` | Single percent sign ('%'). This does not consume an argument. |
|
211 |
|
212 | By default, the `%j` is applied when you pass an object to be logged:
|
213 |
|
214 | ```js
|
215 | acho.info({hello: 'world', foo: 'bar'})
|
216 | // => 'info hello=world foo=bar'
|
217 | ```
|
218 |
|
219 | If you want to use a different formatter, use printf markup:
|
220 |
|
221 | ```js
|
222 | acho.info('formatting with object interpolation %J', {
|
223 | hello: 'world',
|
224 | foo: 'bar',
|
225 | deep: {
|
226 | foo: 'bar',
|
227 | arr: [1, 2, 3, 4, 5]
|
228 | }
|
229 | })
|
230 |
|
231 | // info formatting with object interpolation
|
232 | // hello: "world"
|
233 | // foo: "bar"
|
234 | // deep:
|
235 | // foo: "bar"
|
236 | // arr:
|
237 | // 0: 1
|
238 | // 1: 2
|
239 | // 2: 3
|
240 | // 3: 4
|
241 | // 4: 5
|
242 | ```
|
243 |
|
244 | See more at [examples/formatter](https://github.com/achohq/acho/blob/master/examples/formatter.js).
|
245 |
|
246 | ## API
|
247 |
|
248 | ### Acho({Object} [options])
|
249 |
|
250 | Create a logger. Available options:
|
251 |
|
252 | <img src="docs/images/07.png" align="right">
|
253 |
|
254 | ##### **{String}** keyword
|
255 |
|
256 | Default: `loglevel`
|
257 |
|
258 | Instead of print the type log level, print the keyword. By default this behavior is not activated.
|
259 |
|
260 | You can pass the special keyword `symbol` to show an unicode icon. This is special behavior for CLI programs.
|
261 |
|
262 | <img src="docs/images/08.png" align="right">
|
263 |
|
264 | ##### **{String}** align
|
265 |
|
266 | Default: `' '`
|
267 |
|
268 | It adds an alignment separator between the type of the message and the message.
|
269 |
|
270 | You can provide your own separator or disable it providing a `false`.
|
271 |
|
272 | <img src="docs/images/06.png" align="right">
|
273 |
|
274 | ##### **{Boolean}** diff
|
275 |
|
276 | Default: `false`
|
277 |
|
278 | Prints timestamp between log from the same level. Specially useful to debug timmings.
|
279 |
|
280 | ##### **{Boolean}** color
|
281 |
|
282 | Default: `false`.
|
283 |
|
284 | Enable or disable colorized output.
|
285 |
|
286 | ##### **{Boolean}** upperCase
|
287 |
|
288 | Default: `false`.
|
289 |
|
290 | Enable or disable print log level in upper case.
|
291 |
|
292 | ##### **{Number}** timestamp
|
293 |
|
294 | Default: `0`.
|
295 |
|
296 | Prints a counter timestamp associated with each log line. Useful for debug log traces.
|
297 |
|
298 | ##### **{Number}** offset
|
299 |
|
300 | Default: `2`.
|
301 |
|
302 | The amount of left whitespace between the property key and all of it's sub-properties.
|
303 |
|
304 | This option is only applied under JSON pretty object in multiple lines (%J).
|
305 |
|
306 | ##### **{Number}** depth
|
307 |
|
308 | Default: `Infinity`.
|
309 |
|
310 | Colapses all properties deeper than specified by depth.
|
311 |
|
312 | This option is only applied under JSON pretty object in multiple lines (%J).
|
313 |
|
314 | ##### **{String}** level
|
315 |
|
316 | Default: `all`
|
317 |
|
318 | Provides the logging level. This sets from what level print logs using tranport.
|
319 |
|
320 | Additionally you can provide `muted` to express don't print logs.
|
321 |
|
322 | ##### **{Function}** transport
|
323 |
|
324 | Default: `console.log`
|
325 |
|
326 | Defines where write the log message.
|
327 |
|
328 | ##### **{Object}** types
|
329 |
|
330 | You can provide the types and priorities.
|
331 |
|
332 | ##### **{Object}** messages
|
333 |
|
334 | It provides a initial internal store state per each log level. This option is useful when you want to integrate the logger with the ouptut of a delayed function.
|
335 |
|
336 | ##### **{Function}** print
|
337 |
|
338 | Provides a function that determines how to print the messages. By default uses `.generateMessage` for generate the mesage that will be outputted.
|
339 |
|
340 | ##### **{Function}** outputType
|
341 |
|
342 | Provides a function to customize the type in the output.
|
343 |
|
344 | ##### **{Function}** outputMessage
|
345 |
|
346 | Provides a function to customize the message in the output.
|
347 |
|
348 | ##### **{Function}** generateMessage
|
349 |
|
350 | Provides a function that generate the message to be outputted. It combines other internal methods for generate the output (as `.isPrintable` or `.colorize`) and normally you are not interested in the definition of it, but you can provide it as option as well.
|
351 |
|
352 | ##### **{Function}** generateTypeMessage
|
353 |
|
354 | Provides a function used to generate the type message.
|
355 |
|
356 | ### .push({String} <type>, {String} <message>)
|
357 |
|
358 | Store a message of given `type` internally.
|
359 |
|
360 | ### .add({String} <type>, {String} <message>)
|
361 |
|
362 | Store a message of given `type` internally and also output it.
|
363 |
|
364 | For each level you have a function following the pattern:
|
365 |
|
366 | ### .print()
|
367 |
|
368 | Prints all messages internally stored.
|
369 |
|
370 | ### .\[loglevel\]({String} <message>)
|
371 |
|
372 | For each log level that you declared in the constructor (or the default log levels provides by the library if you don't declare nothing) will be created a function with the same name to output a message with these log level.
|
373 |
|
374 | ## License
|
375 |
|
376 | MIT © [Kiko Beats](http://www.kikobeats.com)
|