Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | /* eslint-disable
|
---|
4 | no-param-reassign,
|
---|
5 | space-before-function-paren
|
---|
6 | */
|
---|
7 | const MethodFactory = require('./MethodFactory');
|
---|
8 |
|
---|
9 | const defaults = {
|
---|
10 | name (options) {
|
---|
11 | return options.logger.name;
|
---|
12 | },
|
---|
13 | time () {
|
---|
14 | return new Date().toTimeString().split(' ')[0];
|
---|
15 | },
|
---|
16 | level (options) {
|
---|
17 | return `[${options.level}]`;
|
---|
18 | },
|
---|
19 | template: '{{time}} {{level}} '
|
---|
20 | };
|
---|
21 |
|
---|
22 | class PrefixFactory extends MethodFactory {
|
---|
23 | constructor(logger, options) {
|
---|
24 | super(logger);
|
---|
25 |
|
---|
26 | this.options = Object.assign({}, defaults, options);
|
---|
27 | }
|
---|
28 |
|
---|
29 | interpolate(level) {
|
---|
30 | return this.options.template.replace(/{{([^{}]*)}}/g, (stache, prop) => {
|
---|
31 | const fn = this.options[prop];
|
---|
32 |
|
---|
33 | if (fn) {
|
---|
34 | return fn({ level, logger: this.logger });
|
---|
35 | }
|
---|
36 |
|
---|
37 | return stache;
|
---|
38 | });
|
---|
39 | }
|
---|
40 |
|
---|
41 | make(method) {
|
---|
42 | const og = super.make(method);
|
---|
43 |
|
---|
44 | return (...args) => {
|
---|
45 | const [first] = args;
|
---|
46 |
|
---|
47 | const output = this.interpolate(method);
|
---|
48 |
|
---|
49 | if (typeof first === 'string') {
|
---|
50 | args[0] = output + first;
|
---|
51 | } else {
|
---|
52 | args.unshift(output);
|
---|
53 | }
|
---|
54 |
|
---|
55 | og(...args);
|
---|
56 | };
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | module.exports = PrefixFactory;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.