standard-log 6.3.1 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/standard-log.js +12 -2
- package/dist/standard-log.js.gz +0 -0
- package/dist/standard-log.js.map +1 -1
- package/dist/standard-log.js.map.gz +0 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +10 -2
- package/lib/config.js.map +1 -1
- package/lib/configForTest.d.ts.map +1 -1
- package/lib/configForTest.js +2 -0
- package/lib/configForTest.js.map +1 -1
- package/libm/config.d.ts.map +1 -1
- package/libm/config.js +10 -2
- package/libm/config.js.map +1 -1
- package/libm/configForTest.d.ts.map +1 -1
- package/libm/configForTest.js +2 -0
- package/libm/configForTest.js.map +1 -1
- package/package.json +2 -2
- package/src/config.spec.ts +12 -4
- package/src/config.ts +10 -2
- package/src/configForTest.ts +2 -0
- package/src/console/createConsoleLogReporter.spec.ts +135 -127
- package/src/logReporter.spec.ts +78 -67
- package/src/memory/MemoryLogReporter.spec.ts +79 -74
|
Binary file
|
package/lib/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE9C,oBAAY,aAAa,GAAG;IAC1B,IAAI,EAAE,OAAO,CAAC;IACd,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,WAAW,EAAE,CAAA;CACzB,CAAA;AAED,eAAO,MAAM,MAAM,EAAE;IAAE,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;CAgCnF,CAAA"}
|
package/lib/config.js
CHANGED
|
@@ -6,11 +6,19 @@ var type_plus_1 = require("type-plus");
|
|
|
6
6
|
var customLogLevel_1 = require("./customLogLevel");
|
|
7
7
|
var errors_1 = require("./errors");
|
|
8
8
|
var getDefaultReporter_1 = require("./getDefaultReporter");
|
|
9
|
+
var getLogger_1 = require("./getLogger");
|
|
9
10
|
var store_1 = require("./store");
|
|
10
11
|
exports.config = function config(options) {
|
|
11
12
|
if (options === void 0) { options = {}; }
|
|
12
|
-
if (store_1.store.value.configured
|
|
13
|
-
|
|
13
|
+
if (store_1.store.value.configured) {
|
|
14
|
+
if (store_1.store.value.mode === 'production') {
|
|
15
|
+
throw new errors_1.ProhibitedDuringProduction('config');
|
|
16
|
+
}
|
|
17
|
+
if (store_1.store.value.mode === 'test') {
|
|
18
|
+
var log = (0, getLogger_1.getLogger)('standard-log');
|
|
19
|
+
log.warn("already configured for test, ignoring config() call");
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
14
22
|
}
|
|
15
23
|
if (options.mode)
|
|
16
24
|
store_1.store.value.mode = options.mode;
|
package/lib/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;;AAAA,uCAAsC;AACtC,mDAAoD;AACpD,mCAAqD;AACrD,2DAAyD;AACzD,iCAA+B;AAUlB,QAAA,MAAM,GAA6E,SAAS,MAAM,CAAC,OAAoC;IAApC,wBAAA,EAAA,YAAoC;IAClJ,IAAI,aAAK,CAAC,KAAK,CAAC,UAAU,IAAI,aAAK,CAAC,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;;AAAA,uCAAsC;AACtC,mDAAoD;AACpD,mCAAqD;AACrD,2DAAyD;AACzD,yCAAuC;AACvC,iCAA+B;AAUlB,QAA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MAAM,CAAC,aAAK,CAAC,KAAK,CAAC,SAAS,CAAC,IAAG,CAAA;KAClF;SACI,IAAI,aAAK,CAAC,KAAK,CAAC,IAAI,KAAK,aAAa,EAAE;QAC3C,OAAO,CAAC,IAAI,CAAC,kFAAkF,CAAC,CAAA;KACjG;AACH,CAAQ,CAAA;AAER,MAAM,CAAC,cAAc,CAAC,cAAM,EAAE,UAAU,EAAE;IACxC;;OAEG;IACH,GAAG;QACD,OAAO,aAAK,CAAC,KAAK,CAAC,UAAU,CAAA;IAC/B,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configForTest.d.ts","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"configForTest.d.ts","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAGlC,wBAAgB,aAAa,CAAC,QAAQ,CAAC,EAAE,QAAQ;;EAShD"}
|
package/lib/configForTest.js
CHANGED
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.configForTest = void 0;
|
|
4
4
|
var config_1 = require("./config");
|
|
5
5
|
var memory_1 = require("./memory");
|
|
6
|
+
var store_1 = require("./store");
|
|
6
7
|
function configForTest(logLevel) {
|
|
8
|
+
store_1.store.reset();
|
|
7
9
|
var reporter = (0, memory_1.createMemoryLogReporter)();
|
|
8
10
|
(0, config_1.config)({
|
|
9
11
|
reporters: [reporter],
|
package/lib/configForTest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configForTest.js","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":";;;AAAA,mCAAiC;AACjC,mCAAkD;
|
|
1
|
+
{"version":3,"file":"configForTest.js","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":";;;AAAA,mCAAiC;AACjC,mCAAkD;AAClD,iCAA+B;AAI/B,SAAgB,aAAa,CAAC,QAAmB;IAC/C,aAAK,CAAC,KAAK,EAAE,CAAA;IACb,IAAM,QAAQ,GAAG,IAAA,gCAAuB,GAAE,CAAA;IAC1C,IAAA,eAAM,EAAC;QACL,SAAS,EAAE,CAAC,QAAQ,CAAC;QACrB,IAAI,EAAE,MAAM;QACZ,QAAQ,UAAA;KACT,CAAC,CAAA;IACF,OAAO,EAAE,QAAQ,UAAA,EAAE,CAAA;AACrB,CAAC;AATD,sCASC"}
|
package/libm/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE9C,oBAAY,aAAa,GAAG;IAC1B,IAAI,EAAE,OAAO,CAAC;IACd,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,WAAW,EAAE,CAAA;CACzB,CAAA;AAED,eAAO,MAAM,MAAM,EAAE;IAAE,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;CAgCnF,CAAA"}
|
package/libm/config.js
CHANGED
|
@@ -2,10 +2,18 @@ import { forEachKey } from 'type-plus';
|
|
|
2
2
|
import { addCustomLogLevel } from './customLogLevel';
|
|
3
3
|
import { ProhibitedDuringProduction } from './errors';
|
|
4
4
|
import { getDefaultReporter } from './getDefaultReporter';
|
|
5
|
+
import { getLogger } from './getLogger';
|
|
5
6
|
import { store } from './store';
|
|
6
7
|
export const config = function config(options = {}) {
|
|
7
|
-
if (store.value.configured
|
|
8
|
-
|
|
8
|
+
if (store.value.configured) {
|
|
9
|
+
if (store.value.mode === 'production') {
|
|
10
|
+
throw new ProhibitedDuringProduction('config');
|
|
11
|
+
}
|
|
12
|
+
if (store.value.mode === 'test') {
|
|
13
|
+
const log = getLogger('standard-log');
|
|
14
|
+
log.warn(`already configured for test, ignoring config() call`);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
9
17
|
}
|
|
10
18
|
if (options.mode)
|
|
11
19
|
store.value.mode = options.mode;
|
package/libm/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAU/B,MAAM,CAAC,MAAM,MAAM,GAA6E,SAAS,MAAM,CAAC,UAAkC,EAAE;IAClJ,IAAI,KAAK,CAAC,KAAK,CAAC,UAAU,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAU/B,MAAM,CAAC,MAA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,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;KAClF;SACI,IAAI,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,aAAa,EAAE;QAC3C,OAAO,CAAC,IAAI,CAAC,kFAAkF,CAAC,CAAA;KACjG;AACH,CAAQ,CAAA;AAER,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,UAAU,EAAE;IACxC;;OAEG;IACH,GAAG;QACD,OAAO,KAAK,CAAC,KAAK,CAAC,UAAU,CAAA;IAC/B,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configForTest.d.ts","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"configForTest.d.ts","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAGlC,wBAAgB,aAAa,CAAC,QAAQ,CAAC,EAAE,QAAQ;;EAShD"}
|
package/libm/configForTest.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { config } from './config';
|
|
2
2
|
import { createMemoryLogReporter } from './memory';
|
|
3
|
+
import { store } from './store';
|
|
3
4
|
export function configForTest(logLevel) {
|
|
5
|
+
store.reset();
|
|
4
6
|
const reporter = createMemoryLogReporter();
|
|
5
7
|
config({
|
|
6
8
|
reporters: [reporter],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configForTest.js","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"configForTest.js","sourceRoot":"","sources":["../src/configForTest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAA;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAI/B,MAAM,UAAU,aAAa,CAAC,QAAmB;IAC/C,KAAK,CAAC,KAAK,EAAE,CAAA;IACb,MAAM,QAAQ,GAAG,uBAAuB,EAAE,CAAA;IAC1C,MAAM,CAAC;QACL,SAAS,EAAE,CAAC,QAAQ,CAAC;QACrB,IAAI,EAAE,MAAM;QACZ,QAAQ;KACT,CAAC,CAAA;IACF,OAAO,EAAE,QAAQ,EAAE,CAAA;AACrB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "standard-log",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "The standard log library",
|
|
5
5
|
"homepage": "https://github.com/unional/standard-log/tree/master/packages/log",
|
|
6
6
|
"bugs": {
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"global-store": "1.0.0-beta.19"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "49724a71fad09041e7ff1023b77fe51dc9931424"
|
|
61
61
|
}
|
package/src/config.spec.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import a from 'assertron'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
config, createMemoryLogReporter,
|
|
4
|
+
logLevels, ProhibitedDuringProduction, toLogLevelName
|
|
5
|
+
} from '.'
|
|
6
|
+
import { configForTest } from './configForTest'
|
|
3
7
|
import { store } from './store'
|
|
4
8
|
|
|
5
9
|
beforeEach(() => store.reset())
|
|
@@ -15,9 +19,7 @@ test('default logLevel is info', () => {
|
|
|
15
19
|
})
|
|
16
20
|
|
|
17
21
|
test('configure default logLevel', () => {
|
|
18
|
-
config({
|
|
19
|
-
logLevel: logLevels.planck
|
|
20
|
-
})
|
|
22
|
+
config({ logLevel: logLevels.planck })
|
|
21
23
|
|
|
22
24
|
expect(store.value.logLevel).toBe(logLevels.planck)
|
|
23
25
|
})
|
|
@@ -27,6 +29,12 @@ test('calling config twice throws ProhibitedDuringProduction in production mode'
|
|
|
27
29
|
a.throws(() => config({ mode: 'development' }), ProhibitedDuringProduction)
|
|
28
30
|
})
|
|
29
31
|
|
|
32
|
+
test('calling config twice in test mode is allowed, but emit a warning', () => {
|
|
33
|
+
const { reporter } = configForTest()
|
|
34
|
+
config({ mode: 'production' })
|
|
35
|
+
expect(reporter.getLogMessageWithLevel()).toEqual('(WARN) already configured for test, ignoring config() call')
|
|
36
|
+
})
|
|
37
|
+
|
|
30
38
|
test('add custom levels', () => {
|
|
31
39
|
config({
|
|
32
40
|
customLevels: {
|
package/src/config.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { forEachKey } from 'type-plus'
|
|
|
2
2
|
import { addCustomLogLevel } from './customLogLevel'
|
|
3
3
|
import { ProhibitedDuringProduction } from './errors'
|
|
4
4
|
import { getDefaultReporter } from './getDefaultReporter'
|
|
5
|
+
import { getLogger } from './getLogger'
|
|
5
6
|
import { store } from './store'
|
|
6
7
|
import { LogMode, LogReporter } from './types'
|
|
7
8
|
|
|
@@ -13,8 +14,15 @@ export type ConfigOptions = {
|
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export const config: { (options?: Partial<ConfigOptions>): void, readonly isLocked: boolean } = function config(options: Partial<ConfigOptions> = {}) {
|
|
16
|
-
if (store.value.configured
|
|
17
|
-
|
|
17
|
+
if (store.value.configured) {
|
|
18
|
+
if (store.value.mode === 'production') {
|
|
19
|
+
throw new ProhibitedDuringProduction('config')
|
|
20
|
+
}
|
|
21
|
+
if (store.value.mode === 'test') {
|
|
22
|
+
const log = getLogger('standard-log')
|
|
23
|
+
log.warn(`already configured for test, ignoring config() call`)
|
|
24
|
+
return
|
|
25
|
+
}
|
|
18
26
|
}
|
|
19
27
|
|
|
20
28
|
if (options.mode) store.value.mode = options.mode
|
package/src/configForTest.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { config } from './config'
|
|
2
2
|
import { createMemoryLogReporter } from './memory'
|
|
3
|
+
import { store } from './store'
|
|
3
4
|
import { LogLevel } from './types'
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
export function configForTest(logLevel?: LogLevel) {
|
|
8
|
+
store.reset()
|
|
7
9
|
const reporter = createMemoryLogReporter()
|
|
8
10
|
config({
|
|
9
11
|
reporters: [reporter],
|
|
@@ -1,177 +1,185 @@
|
|
|
1
|
-
import a from 'assertron'
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
store.reset()
|
|
9
|
-
config({ mode: 'test' })
|
|
10
|
-
})
|
|
1
|
+
import a from 'assertron'
|
|
2
|
+
import {
|
|
3
|
+
addCustomLogLevel, clearCustomLogLevel, config, configForTest,
|
|
4
|
+
ConsoleLogFormatter, createConsoleLogReporter,
|
|
5
|
+
LogFilter, logLevels, plainLogFormatter, ProhibitedDuringProduction
|
|
6
|
+
} from '..'
|
|
7
|
+
import { store } from '../store'
|
|
11
8
|
|
|
12
|
-
describe('
|
|
13
|
-
|
|
14
|
-
const reporter = createConsoleLogReporter({ formatter: plainLogFormatter });
|
|
15
|
-
['emergency', 'info', 'warn', 'debug'].forEach(level => {
|
|
16
|
-
reporter.write({ id: 'plain', level: (logLevels as any)[level], args: ['hello', 'world'], timestamp: new Date() })
|
|
17
|
-
})
|
|
18
|
-
})
|
|
19
|
-
})
|
|
9
|
+
describe('production checks', () => {
|
|
10
|
+
beforeEach(() => store.reset())
|
|
20
11
|
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
test('set filter during production mode throws', () => {
|
|
13
|
+
config({ mode: 'production' })
|
|
23
14
|
const reporter = createConsoleLogReporter()
|
|
24
|
-
|
|
15
|
+
a.throws(() => { reporter.filter = () => true }, ProhibitedDuringProduction)
|
|
25
16
|
})
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
|
|
18
|
+
const idFormatter: ConsoleLogFormatter = (entry) => [entry.id]
|
|
19
|
+
test('set formatter during production mode throws', () => {
|
|
20
|
+
config({ mode: 'production' })
|
|
21
|
+
const reporter = createConsoleLogReporter()
|
|
22
|
+
a.throws(() => reporter.formatter = idFormatter, ProhibitedDuringProduction)
|
|
29
23
|
})
|
|
30
24
|
})
|
|
31
25
|
|
|
32
|
-
describe('log level', () => {
|
|
33
|
-
test('error, alert, critical and emergency logs are written to console.error', () => {
|
|
34
|
-
const id = 'log'
|
|
35
|
-
const reporter = createConsoleLogReporter()
|
|
36
|
-
const fakeConsole = reporter.console = createFakeConsole();
|
|
37
26
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
reporter.write({ id, level: logLevels.alert, args: ['alert'], timestamp: new Date() })
|
|
41
|
-
reporter.write({ id, level: logLevels.error, args: ['error'], timestamp: new Date() })
|
|
27
|
+
describe('unit tests', () => {
|
|
28
|
+
beforeEach(() => configForTest())
|
|
42
29
|
|
|
43
|
-
|
|
30
|
+
describe('rendering tests', () => {
|
|
31
|
+
test('plain rendering', () => {
|
|
32
|
+
const reporter = createConsoleLogReporter({ formatter: plainLogFormatter });
|
|
33
|
+
['emergency', 'info', 'warn', 'debug'].forEach(level => {
|
|
34
|
+
reporter.write({ id: 'plain', level: (logLevels as any)[level], args: ['hello', 'world'], timestamp: new Date() })
|
|
35
|
+
})
|
|
36
|
+
})
|
|
44
37
|
})
|
|
45
38
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
describe('id', () => {
|
|
40
|
+
test('default is "console"', () => {
|
|
41
|
+
const reporter = createConsoleLogReporter()
|
|
42
|
+
expect(reporter.id).toBe('console')
|
|
43
|
+
})
|
|
44
|
+
test('can be overridden', () => {
|
|
45
|
+
const reporter = createConsoleLogReporter({ id: 'neo-console' })
|
|
46
|
+
expect(reporter.id).toBe('neo-console')
|
|
47
|
+
})
|
|
48
|
+
})
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
describe('log level', () => {
|
|
51
|
+
test('error, alert, critical and emergency logs are written to console.error', () => {
|
|
52
|
+
const id = 'log'
|
|
53
|
+
const reporter = createConsoleLogReporter()
|
|
54
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
53
55
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
reporter.write({ id, level: logLevels.emergency, args: ['emergency'], timestamp: new Date() })
|
|
57
|
+
reporter.write({ id, level: logLevels.critical, args: ['critical'], timestamp: new Date() })
|
|
58
|
+
reporter.write({ id, level: logLevels.alert, args: ['alert'], timestamp: new Date() })
|
|
59
|
+
reporter.write({ id, level: logLevels.error, args: ['error'], timestamp: new Date() })
|
|
56
60
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const reporter = createConsoleLogReporter()
|
|
61
|
+
expect(fakeConsole.errors.length).toBe(4)
|
|
62
|
+
})
|
|
60
63
|
|
|
61
|
-
|
|
64
|
+
test('warn logs are written to console.warn', () => {
|
|
65
|
+
const id = 'log'
|
|
66
|
+
const reporter = createConsoleLogReporter()
|
|
62
67
|
|
|
63
|
-
|
|
64
|
-
reporter.write({ id, level: logLevels.info, args: ['info'], timestamp: new Date() })
|
|
68
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
})
|
|
70
|
+
reporter.write({ id, level: logLevels.warn, args: ['warn'], timestamp: new Date() })
|
|
68
71
|
|
|
72
|
+
expect(fakeConsole.warns.length).toBe(1)
|
|
73
|
+
})
|
|
69
74
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const fakeConsole = reporter.console = createFakeConsole();
|
|
75
|
+
test('notice and info logs are written to console.info', () => {
|
|
76
|
+
const id = 'log'
|
|
77
|
+
const reporter = createConsoleLogReporter()
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
reporter.write({ id, level: logLevels.trace, args: ['trace'], timestamp: new Date() })
|
|
77
|
-
reporter.write({ id, level: logLevels.planck, args: ['planck'], timestamp: new Date() })
|
|
79
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
78
80
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
})
|
|
81
|
+
reporter.write({ id, level: logLevels.notice, args: ['notice'], timestamp: new Date() })
|
|
82
|
+
reporter.write({ id, level: logLevels.info, args: ['info'], timestamp: new Date() })
|
|
82
83
|
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
expect(fakeConsole.infos.length).toBe(2)
|
|
85
|
+
})
|
|
85
86
|
|
|
86
|
-
test('are written according to their log level value', () => {
|
|
87
|
-
addCustomLogLevel('high', 50)
|
|
88
|
-
addCustomLogLevel('important', 450)
|
|
89
|
-
addCustomLogLevel('interest', 650)
|
|
90
|
-
addCustomLogLevel('silly', 1000)
|
|
91
87
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
88
|
+
test('debug, trace, and planck logs are written to console.debug', () => {
|
|
89
|
+
const id = 'log'
|
|
90
|
+
const reporter = createConsoleLogReporter()
|
|
91
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
95
92
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
reporter.write({ id, level: 1000, args: ['d'], timestamp: new Date() })
|
|
93
|
+
reporter.write({ id, level: logLevels.debug, args: ['debug'], timestamp: new Date() })
|
|
94
|
+
reporter.write({ id, level: logLevels.trace, args: ['trace'], timestamp: new Date() })
|
|
95
|
+
reporter.write({ id, level: logLevels.planck, args: ['planck'], timestamp: new Date() })
|
|
100
96
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
expect(fakeConsole.infos.length).toBe(1)
|
|
104
|
-
expect(fakeConsole.debugs.length).toBe(1)
|
|
97
|
+
expect(fakeConsole.debugs.length).toBe(3)
|
|
98
|
+
})
|
|
105
99
|
})
|
|
106
|
-
})
|
|
107
100
|
|
|
108
|
-
|
|
101
|
+
describe('custom logs', () => {
|
|
102
|
+
afterEach(() => clearCustomLogLevel())
|
|
109
103
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
104
|
+
test('are written according to their log level value', () => {
|
|
105
|
+
addCustomLogLevel('high', 50)
|
|
106
|
+
addCustomLogLevel('important', 450)
|
|
107
|
+
addCustomLogLevel('interest', 650)
|
|
108
|
+
addCustomLogLevel('silly', 1000)
|
|
115
109
|
|
|
116
|
-
|
|
110
|
+
const id = 'log'
|
|
111
|
+
const reporter = createConsoleLogReporter()
|
|
112
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
117
113
|
|
|
118
|
-
|
|
119
|
-
|
|
114
|
+
reporter.write({ id, level: 50, args: ['a'], timestamp: new Date() })
|
|
115
|
+
reporter.write({ id, level: 450, args: ['b'], timestamp: new Date() })
|
|
116
|
+
reporter.write({ id, level: 650, args: ['c'], timestamp: new Date() })
|
|
117
|
+
reporter.write({ id, level: 1000, args: ['d'], timestamp: new Date() })
|
|
120
118
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
119
|
+
expect(fakeConsole.errors.length).toBe(1)
|
|
120
|
+
expect(fakeConsole.warns.length).toBe(1)
|
|
121
|
+
expect(fakeConsole.infos.length).toBe(1)
|
|
122
|
+
expect(fakeConsole.debugs.length).toBe(1)
|
|
123
|
+
})
|
|
126
124
|
})
|
|
127
125
|
|
|
128
|
-
|
|
129
|
-
config({ mode: 'production' })
|
|
130
|
-
const reporter = createConsoleLogReporter()
|
|
131
|
-
a.throws(() => reporter.formatter = idFormatter, ProhibitedDuringProduction)
|
|
132
|
-
})
|
|
133
|
-
})
|
|
126
|
+
const idFormatter: ConsoleLogFormatter = (entry) => [entry.id]
|
|
134
127
|
|
|
135
|
-
describe('
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
128
|
+
describe('formatter', () => {
|
|
129
|
+
test('use specific formatter', () => {
|
|
130
|
+
const id = 'id-only'
|
|
131
|
+
const reporter = createConsoleLogReporter({ formatter: idFormatter })
|
|
132
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
140
133
|
|
|
141
|
-
|
|
142
|
-
reporter.write({ id: 'secret', level: logLevels.emergency, args: ['emergency'], timestamp: new Date() })
|
|
134
|
+
reporter.write({ id, level: logLevels.emergency, args: ['emergency'], timestamp: new Date() })
|
|
143
135
|
|
|
144
|
-
|
|
145
|
-
|
|
136
|
+
expect(fakeConsole.errors).toEqual([['id-only']])
|
|
137
|
+
})
|
|
146
138
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
139
|
+
test('formatter can be changed afterward', () => {
|
|
140
|
+
const reporter = createConsoleLogReporter()
|
|
141
|
+
const formatter = reporter.formatter
|
|
142
|
+
reporter.formatter = idFormatter
|
|
143
|
+
expect(reporter.formatter).not.toEqual(formatter)
|
|
144
|
+
})
|
|
152
145
|
})
|
|
153
146
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
147
|
+
describe('filter', () => {
|
|
148
|
+
const filter: LogFilter = (entry) => entry.id !== 'secret'
|
|
149
|
+
test('use specific filter', () => {
|
|
150
|
+
const reporter = createConsoleLogReporter({ formatter: idFormatter, filter })
|
|
151
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
152
|
+
|
|
153
|
+
reporter.write({ id: 'normal', level: logLevels.emergency, args: ['emergency'], timestamp: new Date() })
|
|
154
|
+
reporter.write({ id: 'secret', level: logLevels.emergency, args: ['emergency'], timestamp: new Date() })
|
|
155
|
+
|
|
156
|
+
expect(fakeConsole.errors).toEqual([['normal']])
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('can change filter', () => {
|
|
160
|
+
const reporter = createConsoleLogReporter()
|
|
161
|
+
const filter = reporter.filter
|
|
162
|
+
reporter.filter = entry => entry.id !== 'secret'
|
|
163
|
+
expect(reporter.filter).not.toEqual(filter)
|
|
164
|
+
})
|
|
158
165
|
})
|
|
159
|
-
})
|
|
160
166
|
|
|
161
|
-
test('write log entry with multiple arguments', () => {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
167
|
+
test('write log entry with multiple arguments', () => {
|
|
168
|
+
const id = 'log'
|
|
169
|
+
const reporter = createConsoleLogReporter({ formatter: plainLogFormatter })
|
|
170
|
+
const fakeConsole = reporter.console = createFakeConsole()
|
|
165
171
|
|
|
166
|
-
|
|
172
|
+
const entry = { id, level: logLevels.info, args: ['a', 'b', 'c'], timestamp: new Date() }
|
|
167
173
|
|
|
168
|
-
|
|
174
|
+
reporter.write(entry)
|
|
169
175
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
176
|
+
expect(fakeConsole.infos).toEqual([
|
|
177
|
+
[entry.timestamp.toISOString(), 'log', '(INFO)', 'a', 'b', 'c']
|
|
178
|
+
])
|
|
179
|
+
})
|
|
173
180
|
})
|
|
174
181
|
|
|
182
|
+
|
|
175
183
|
function createFakeConsole() {
|
|
176
184
|
const errors: any[] = []
|
|
177
185
|
const infos: any[] = []
|