1 | # loglevel-plugin-prefix
|
2 |
|
3 | Plugin for [loglevel](https://github.com/pimterry/loglevel) message prefixing.
|
4 |
|
5 | [![NPM version](https://img.shields.io/npm/v/loglevel-plugin-prefix.svg?style=flat-square)](https://www.npmjs.com/package/loglevel-plugin-prefix)[![Build Status](https://img.shields.io/travis/kutuluk/loglevel-plugin-prefix/master.svg?style=flat-square)](https://travis-ci.org/kutuluk/loglevel-plugin-prefix)
|
6 |
|
7 | ## Installation
|
8 |
|
9 | ```sh
|
10 | npm install loglevel-plugin-prefix
|
11 | ```
|
12 |
|
13 | ## API
|
14 |
|
15 | **This plugin is under active development and should be considered as an unstable. No guarantees regarding API stability are made. Backward compatibility is guaranteed only by path releases.**
|
16 |
|
17 | #### `reg(loglevel)`
|
18 |
|
19 | This method registers plugin for loglevel. This method must be called at least once before any call to the apply method. Repeated calls to this method are ignored.
|
20 |
|
21 | #### Parameters
|
22 |
|
23 | `loglevel` - the root logger, imported from loglevel module
|
24 |
|
25 | #### `apply(logger, options)`
|
26 |
|
27 | This method applies the plugin to the logger. Before using this method, the `reg` method must be called, otherwise a warning will be logged. **From the next release, the call apply before reg will throw an error.**
|
28 |
|
29 | #### Parameters
|
30 |
|
31 | `logger` - any logger of loglevel
|
32 |
|
33 | `options` - an optional configuration object
|
34 |
|
35 | ```javascript
|
36 | var defaults = {
|
37 | template: '[%t] %l:',
|
38 | levelFormatter: function (level) {
|
39 | return level.toUpperCase();
|
40 | },
|
41 | nameFormatter: function (name) {
|
42 | return name || 'root';
|
43 | },
|
44 | timestampFormatter: function (date) {
|
45 | return date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1');
|
46 | },
|
47 | format: undefined
|
48 | };
|
49 | ```
|
50 |
|
51 | Plugin formats the prefix using `template` option as a printf-like format. The `template` is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the value from loglevel messages parameters. Supported placeholders are:
|
52 |
|
53 | - `%l` - level of message
|
54 | - `%n` - name of logger
|
55 | - `%t` - timestamp of message
|
56 |
|
57 | The `levelFormatter`, `nameFormatter` and `timestampFormatter` is a functions for formatting corresponding values.
|
58 |
|
59 | Alternatively, you can use `format` option. This is a function that receives formatted values (level, name, timestamp) and should returns a prefix string.
|
60 |
|
61 | If both `format` and` template` are present in the configuration, the `template` parameter is ignored. When both these parameters are missing in the configuration, the inherited behavior is used.
|
62 |
|
63 | ## Usage
|
64 |
|
65 | ### Browser directly
|
66 | ```html
|
67 | <script src="https://unpkg.com/loglevel/dist/loglevel.min.js"></script>
|
68 | <script src="https://unpkg.com/loglevel-plugin-prefix@^0.8/dist/loglevel-plugin-prefix.min.js"></script>
|
69 |
|
70 | <script>
|
71 | var logger = log.noConflict();
|
72 | var prefixer = prefix.noConflict();
|
73 | prefixer.reg(logger);
|
74 | prefixer.apply(logger);
|
75 | logger.warn('prefixed message');
|
76 | </script>
|
77 | ```
|
78 |
|
79 | Output
|
80 | ```
|
81 | [16:53:46] WARN: prefixed message
|
82 | ```
|
83 |
|
84 | ### Node
|
85 | ```javascript
|
86 | const chalk = require('chalk');
|
87 | const log = require('loglevel');
|
88 | const prefix = require('loglevel-plugin-prefix');
|
89 |
|
90 | const colors = {
|
91 | TRACE: chalk.magenta,
|
92 | DEBUG: chalk.cyan,
|
93 | INFO: chalk.blue,
|
94 | WARN: chalk.yellow,
|
95 | ERROR: chalk.red,
|
96 | };
|
97 |
|
98 | prefix.reg(log);
|
99 | log.enableAll();
|
100 |
|
101 | prefix.apply(log, {
|
102 | format(level, name, timestamp) {
|
103 | return `${chalk.gray(`[${timestamp}]`)} ${colors[level.toUpperCase()](level)} ${chalk.green(`${name}:`)}`;
|
104 | },
|
105 | });
|
106 |
|
107 | prefix.apply(log.getLogger('critical'), {
|
108 | format(level, name, timestamp) {
|
109 | return chalk.red.bold(`[${timestamp}] ${level} ${name}:`);
|
110 | },
|
111 | });
|
112 |
|
113 | log.trace('trace');
|
114 | log.debug('debug');
|
115 | log.getLogger('critical').info('Something significant happened');
|
116 | log.log('log');
|
117 | log.info('info');
|
118 | log.warn('warn');
|
119 | log.error('error');
|
120 | ```
|
121 |
|
122 | Output
|
123 |
|
124 | ![output](https://raw.githubusercontent.com/kutuluk/loglevel-plugin-prefix/master/colored.png "output")
|
125 |
|
126 | ## Custom options
|
127 |
|
128 | ```javascript
|
129 | const log = require('loglevel');
|
130 | const prefix = require('loglevel-plugin-prefix');
|
131 |
|
132 | prefix.reg(log);
|
133 | log.enableAll();
|
134 |
|
135 | prefix.apply(log, {
|
136 | template: '[%t] %l (%n) static text:',
|
137 | levelFormatter(level) {
|
138 | return level.toUpperCase();
|
139 | },
|
140 | nameFormatter(name) {
|
141 | return name || 'global';
|
142 | },
|
143 | timestampFormatter(date) {
|
144 | return date.toISOString();
|
145 | },
|
146 | });
|
147 |
|
148 | log.info('%s prefix', 'template');
|
149 |
|
150 | const fn = (level, name, timestamp) => `[${timestamp}] ${level} (${name}) static text:`;
|
151 |
|
152 | prefix.apply(log, { format: fn });
|
153 |
|
154 | log.info('%s prefix', 'functional');
|
155 |
|
156 | prefix.apply(log, { template: '[%t] %l (%n) static text:' });
|
157 |
|
158 | log.info('again %s prefix', 'template');
|
159 | ```
|
160 |
|
161 | Output
|
162 | ```
|
163 | [2017-05-29T12:53:46.000Z] INFO (global) static text: template prefix
|
164 | [2017-05-29T12:53:46.000Z] INFO (global) static text: functional prefix
|
165 | [2017-05-29T12:53:46.000Z] INFO (global) static text: again template prefix
|
166 | ```
|
167 |
|
168 | ## Option inheritance
|
169 |
|
170 | ```javascript
|
171 | const log = require('loglevel');
|
172 | const prefix = require('loglevel-plugin-prefix');
|
173 |
|
174 | prefix.reg(log);
|
175 | log.enableAll();
|
176 |
|
177 | log.info('root');
|
178 |
|
179 | const chicken = log.getLogger('chicken');
|
180 | chicken.info('chicken');
|
181 |
|
182 | prefix.apply(chicken, { template: '%l (%n):' });
|
183 | chicken.info('chicken');
|
184 |
|
185 | prefix.apply(log);
|
186 | log.info('root');
|
187 |
|
188 | const egg = log.getLogger('egg');
|
189 | egg.info('egg');
|
190 |
|
191 | const fn = (level, name) => `${level} (${name}):`;
|
192 |
|
193 | prefix.apply(egg, { format: fn });
|
194 | egg.info('egg');
|
195 |
|
196 | prefix.apply(egg, {
|
197 | levelFormatter(level) {
|
198 | return level.toLowerCase();
|
199 | },
|
200 | });
|
201 | egg.info('egg');
|
202 |
|
203 | chicken.info('chicken');
|
204 | log.info('root');
|
205 | ```
|
206 |
|
207 | Output
|
208 | ```
|
209 | root
|
210 | chicken
|
211 | INFO (chicken): chicken
|
212 | [16:53:46] INFO: root
|
213 | [16:53:46] INFO: egg
|
214 | INFO (egg): egg
|
215 | info (egg): egg
|
216 | INFO (chicken): chicken
|
217 | [16:53:46] INFO: root
|
218 | ```
|