UNPKG

5.82 kBMarkdownView Raw
1# loglevel-plugin-prefix
2
3Plugin 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
10npm 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
19This 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
27This 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
36var 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
51Plugin 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
57The `levelFormatter`, `nameFormatter` and `timestampFormatter` is a functions for formatting corresponding values.
58
59Alternatively, you can use `format` option. This is a function that receives formatted values (level, name, timestamp) and should returns a prefix string.
60
61If 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
79Output
80```
81[16:53:46] WARN: prefixed message
82```
83
84### Node
85```javascript
86const chalk = require('chalk');
87const log = require('loglevel');
88const prefix = require('loglevel-plugin-prefix');
89
90const colors = {
91 TRACE: chalk.magenta,
92 DEBUG: chalk.cyan,
93 INFO: chalk.blue,
94 WARN: chalk.yellow,
95 ERROR: chalk.red,
96};
97
98prefix.reg(log);
99log.enableAll();
100
101prefix.apply(log, {
102 format(level, name, timestamp) {
103 return `${chalk.gray(`[${timestamp}]`)} ${colors[level.toUpperCase()](level)} ${chalk.green(`${name}:`)}`;
104 },
105});
106
107prefix.apply(log.getLogger('critical'), {
108 format(level, name, timestamp) {
109 return chalk.red.bold(`[${timestamp}] ${level} ${name}:`);
110 },
111});
112
113log.trace('trace');
114log.debug('debug');
115log.getLogger('critical').info('Something significant happened');
116log.log('log');
117log.info('info');
118log.warn('warn');
119log.error('error');
120```
121
122Output
123
124![output](https://raw.githubusercontent.com/kutuluk/loglevel-plugin-prefix/master/colored.png "output")
125
126## Custom options
127
128```javascript
129const log = require('loglevel');
130const prefix = require('loglevel-plugin-prefix');
131
132prefix.reg(log);
133log.enableAll();
134
135prefix.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
148log.info('%s prefix', 'template');
149
150const fn = (level, name, timestamp) => `[${timestamp}] ${level} (${name}) static text:`;
151
152prefix.apply(log, { format: fn });
153
154log.info('%s prefix', 'functional');
155
156prefix.apply(log, { template: '[%t] %l (%n) static text:' });
157
158log.info('again %s prefix', 'template');
159```
160
161Output
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
171const log = require('loglevel');
172const prefix = require('loglevel-plugin-prefix');
173
174prefix.reg(log);
175log.enableAll();
176
177log.info('root');
178
179const chicken = log.getLogger('chicken');
180chicken.info('chicken');
181
182prefix.apply(chicken, { template: '%l (%n):' });
183chicken.info('chicken');
184
185prefix.apply(log);
186log.info('root');
187
188const egg = log.getLogger('egg');
189egg.info('egg');
190
191const fn = (level, name) => `${level} (${name}):`;
192
193prefix.apply(egg, { format: fn });
194egg.info('egg');
195
196prefix.apply(egg, {
197 levelFormatter(level) {
198 return level.toLowerCase();
199 },
200});
201egg.info('egg');
202
203chicken.info('chicken');
204log.info('root');
205```
206
207Output
208```
209root
210chicken
211INFO (chicken): chicken
212[16:53:46] INFO: root
213[16:53:46] INFO: egg
214INFO (egg): egg
215info (egg): egg
216INFO (chicken): chicken
217[16:53:46] INFO: root
218```