1 | 'use strict';
|
2 |
|
3 | const MethodFactory = require('../lib/MethodFactory');
|
4 |
|
5 | const defaults = {
|
6 | level: opts => `[${opts.level}]`,
|
7 | name: opts => opts.logger.name,
|
8 | template: '{{time}} {{level}} ',
|
9 | time: () => new Date().toTimeString().split(' ')[0]
|
10 | };
|
11 |
|
12 | module.exports = class PrefixFactory extends MethodFactory {
|
13 | constructor(logger, options) {
|
14 | super(logger);
|
15 | this.options = Object.assign({}, defaults, options);
|
16 | }
|
17 |
|
18 | interpolate(level) {
|
19 | return this.options.template.replace(/{{([^{}]*)}}/g, (stache, prop) => {
|
20 | const fn = this.options[prop];
|
21 |
|
22 | if (fn) {
|
23 | return fn({ level, logger: this.logger });
|
24 | }
|
25 |
|
26 | return stache;
|
27 | });
|
28 | }
|
29 |
|
30 | make(methodName) {
|
31 | const og = super.make(methodName);
|
32 |
|
33 | return (...args) => {
|
34 | const output = this.interpolate(methodName);
|
35 | const [first] = args;
|
36 |
|
37 | if (typeof first === 'string') {
|
38 | args[0] = output + first;
|
39 | } else {
|
40 | args.unshift(output);
|
41 | }
|
42 |
|
43 | og(...args);
|
44 | };
|
45 | }
|
46 | };
|