|
1 | 1 | # Monk Log library |
2 | 2 |
|
3 | | -This is a simple logging library for Nodejs projects depends on: |
4 | | -- [loglevel](https://github.com/pimterry/loglevel) |
5 | | -- [loglevel-plugin-prefix](https://github.com/kutuluk/loglevel-plugin-prefix) |
6 | | -- [chalk](https://github.com/chalk/chalk#readme) |
| 3 | +Are you using `console.log` in your project? Are you tired of configuring |
| 4 | +loglevel / winston / whatever to add timestamp, colors to your log messages? |
| 5 | +Do you want a consistent log style? |
| 6 | +This is the library for you! You can now log with style -- no configuration |
| 7 | +required! |
7 | 8 |
|
8 | 9 | ## Installation |
9 | 10 |
|
10 | | -For add this as `npm` dependency |
| 11 | +Simply add this library as a dependency in your project. |
| 12 | +You can use the github URI: |
11 | 13 |
|
12 | | -```bash |
13 | | -npm install git+ssh://git@git.webmonks.org:node-libraries/monk-log.git |
| 14 | +``` |
| 15 | +yarn add github:monksoftware/monk-log |
14 | 16 | ``` |
15 | 17 |
|
16 | 18 | or add manually to your `package.json` file |
17 | 19 |
|
18 | 20 | ```json |
19 | 21 | { |
20 | 22 | "dependencies": { |
21 | | - ... , |
22 | | - "monk-log": "git+ssh://git@git.webmonks.org:node-libraries/monk-log.git" |
| 23 | + [...] , |
| 24 | + "monk-log": "github:monksoftware/monk-log" |
23 | 25 | } |
24 | 26 | } |
25 | 27 | ``` |
26 | 28 |
|
27 | | -## Setting up |
| 29 | +## How to use |
| 30 | + |
| 31 | +Works pretty much the same as `loglevel`, the only difference is that |
| 32 | +the exported root logger is preconfigured with loglevel-plugin-prefix |
| 33 | +and custom formatting functions in order to output nice colorful log messages |
| 34 | +with timestamp, log level and logger name. |
28 | 35 |
|
29 | 36 | ```javascript |
30 | | -const { levels, monkLog } = require('monk-log') |
31 | | -const log = monkLog(options) |
| 37 | +const log = require('monk-log') |
32 | 38 |
|
33 | 39 | log.trace(msg) |
34 | 40 | log.debug(msg) |
35 | 41 | log.info(msg) |
36 | 42 | log.warn(msg) |
37 | 43 | log.error(msg) |
38 | | - |
39 | | -``` |
40 | | - |
41 | | -## Documentation |
42 | | - |
43 | | -The monk log library accept optional parameters: |
44 | | -``` |
45 | | -{ |
46 | | - name: // Choose a name for log instance, default is monk-log |
47 | | - level: // Choose a log level, default il DEBUG, possible level are: |
48 | | - // "TRACE" "DEBUG" "INFO" "WARN" "ERROR" |
49 | | - wrap: // Wrap `name` with custom identifier, default is and array of square brackets ['[', ']'] |
50 | | -} |
51 | 44 | ``` |
52 | 45 |
|
53 | 46 | ## Examples |
54 | 47 |
|
55 | | -see more inside `examples folder` |
| 48 | +See more inside `examples folder` |
56 | 49 |
|
| 50 | +### Ready-to-go logging |
57 | 51 | ```javascript |
58 | | -const { levels, monkLog } = require('monk-log') |
59 | | -const log = monkLog({ |
60 | | - name: 'MONK-LOG', |
61 | | - level: levels.DEBUG, |
62 | | - wrap: ['+', '+'] |
63 | | -}) |
| 52 | +const log = require('monk-log') |
| 53 | +log.warn('You can have nice log messages in just one line!') |
64 | 54 |
|
65 | | -log.debug('this is a debug log') |
66 | | -log.info('this is an info log') |
67 | | -log.warn('this is a warning log') |
68 | | -log.error('this is an error log') |
| 55 | +// Outputs |
| 56 | +// [2019-02-18T01:10:49.933] WARN [root]: You can have nice log messages in just one line! |
| 57 | +``` |
69 | 58 |
|
70 | | -// Response |
71 | | -// [2019-02-08T18:35:40.245] INFO monk-log: Set log level to DEBUG |
72 | | -// [2019-02-08T18:35:40.245] DEBUG +MONK-LOG+: this is a debug log |
73 | | -// [2019-02-08T18:35:40.245] INFO +MONK-LOG+: this is an info log |
74 | | -// [2019-02-08T18:35:40.245] WARN +MONK-LOG+: this is a warning log |
75 | | -// [2019-02-08T18:35:40.245] ERROR +MONK-LOG+: this is an error log |
| 59 | +### Child loggers with custom format |
76 | 60 |
|
| 61 | +```javascript |
| 62 | +const log = require('monk-log') |
| 63 | +// Default level is WARN, |
| 64 | +log.setDefaultLevel('DEBUG') |
| 65 | +const childLogger = log.getLogger('CHILD', 'DEBUG', { |
| 66 | + template: 'When: %t, who: %n, why: %l, what: ' |
| 67 | +}) |
| 68 | +log.info('Child logger has been set up') |
| 69 | +childLogger.debug('this is a debug message') |
| 70 | +childLogger.info('This is a info message') |
| 71 | +childLogger.warn('this is a warning message') |
| 72 | +childLogger.error('this is an error message') |
| 73 | + |
| 74 | +// Outputs |
| 75 | +// [2019-02-18T01:08:46.260] INFO [root]: Child logger has been set up |
| 76 | +// When: 2019-02-18T01:08:46.262, who: CHILD, why: DEBUG, what: this is a debug message |
| 77 | +// When: 2019-02-18T01:08:46.262, who: CHILD, why: INFO, what: This is a info message |
| 78 | +// When: 2019-02-18T01:08:46.262, who: CHILD, why: WARN, what: this is a warning message |
| 79 | +// When: 2019-02-18T01:08:46.263, who: CHILD, why: ERROR, what: this is an error message |
77 | 80 | ``` |
0 commit comments