createLogger.js.flow
4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// @flow
import environmentIsNode from 'detect-node';
import createGlobalThis from 'globalthis';
import stringify from 'json-stringify-safe';
import {
sprintf,
} from 'sprintf-js';
import {
logLevels,
} from '../constants';
import type {
LoggerType,
MessageContextType,
MessageEventHandlerType,
TranslateMessageFunctionType,
} from '../types';
const globalThis = createGlobalThis();
let domain;
if (environmentIsNode) {
// eslint-disable-next-line global-require
domain = require('domain');
}
const getParentDomainContext = () => {
if (!domain) {
return {};
}
const parentRoarrContexts = [];
let currentDomain = process.domain;
// $FlowFixMe
if (!currentDomain || !currentDomain.parentDomain) {
return {};
}
while (currentDomain && currentDomain.parentDomain) {
currentDomain = currentDomain.parentDomain;
if (currentDomain.roarr && currentDomain.roarr.context) {
parentRoarrContexts.push(currentDomain.roarr.context);
}
}
let domainContext = {};
for (const parentRoarrContext of parentRoarrContexts) {
domainContext = {
...domainContext,
...parentRoarrContext,
};
}
return domainContext;
};
const getFirstParentDomainContext = () => {
if (!domain) {
return {};
}
let currentDomain = process.domain;
// $FlowFixMe
if (currentDomain && currentDomain.roarr && currentDomain.roarr.context) {
return currentDomain.roarr.context;
}
// $FlowFixMe
if (!currentDomain || !currentDomain.parentDomain) {
return {};
}
while (currentDomain && currentDomain.parentDomain) {
currentDomain = currentDomain.parentDomain;
if (currentDomain.roarr && currentDomain.roarr.context) {
return currentDomain.roarr.context;
}
}
return {};
};
const createLogger = (onMessage: MessageEventHandlerType, parentContext?: MessageContextType): LoggerType => {
// eslint-disable-next-line id-length, unicorn/prevent-abbreviations
const log = (a, b, c, d, e, f, g, h, i, k) => {
const time = Date.now();
const sequence = globalThis.ROARR.sequence++;
let context;
let message;
if (typeof a === 'string') {
context = {
...getFirstParentDomainContext(),
...parentContext || {},
};
// eslint-disable-next-line id-length, object-property-newline
const {...args} = {a, b, c, d, e, f, g, h, i, k};
const values = Object.keys(args).map((key) => {
return args[key];
});
// eslint-disable-next-line unicorn/no-reduce
const hasOnlyOneParameterValued = 1 === values.reduce((accumulator, value) => {
// eslint-disable-next-line no-return-assign, no-param-reassign
return accumulator += typeof value === 'undefined' ? 0 : 1;
}, 0);
message = hasOnlyOneParameterValued ? sprintf('%s', a) : sprintf(a, b, c, d, e, f, g, h, i, k);
} else {
if (typeof b !== 'string') {
throw new TypeError('Message must be a string.');
}
context = JSON.parse(stringify({
...getFirstParentDomainContext(),
...parentContext || {},
...a,
}));
message = sprintf(b, c, d, e, f, g, h, i, k);
}
onMessage({
context,
message,
sequence,
time,
version: '1.0.0',
});
};
log.child = (context: TranslateMessageFunctionType | MessageContextType): LoggerType => {
if (typeof context === 'function') {
return createLogger((message) => {
if (typeof context !== 'function') {
throw new TypeError('Unexpected state.');
}
onMessage(context(message));
}, parentContext);
}
return createLogger(onMessage, {
...getFirstParentDomainContext(),
...parentContext,
...context,
});
};
log.getContext = (): MessageContextType => {
return {
...getFirstParentDomainContext(),
...parentContext || {},
};
};
log.adopt = async (routine, context) => {
if (!domain) {
return routine();
}
const adoptedDomain = domain.create();
return adoptedDomain
.run(() => {
// $FlowFixMe
adoptedDomain.roarr = {
context: {
...getParentDomainContext(),
...context,
},
};
return routine();
});
};
for (const logLevel of Object.keys(logLevels)) {
// eslint-disable-next-line id-length, unicorn/prevent-abbreviations
log[logLevel] = (a, b, c, d, e, f, g, h, i, k) => {
return log.child({
logLevel: logLevels[logLevel],
})(a, b, c, d, e, f, g, h, i, k);
};
}
// @see https://github.com/facebook/flow/issues/6705
// $FlowFixMe
return log;
};
export default createLogger;