Skip to content

Commit bfbd6ae

Browse files
committed
fix for logger after cfEnv changes
1 parent 3545ba2 commit bfbd6ae

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/logger.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const util = require("util");
33
const VError = require("verror");
44

5-
const { cfEnv, isOnCF } = require("./env");
5+
const { cfEnv } = require("./env");
66
const { tryRequire } = require("./shared/static");
77
const cds = tryRequire("@sap/cds");
88

@@ -72,7 +72,7 @@ const MILLIS_IN_NANOS_NUMBER = 1000000;
7272
const MILLIS_IN_NANOS_BIGINT = BigInt(MILLIS_IN_NANOS_NUMBER);
7373

7474
const cfApp = cfEnv.cfApp;
75-
const cfAppData = isOnCF
75+
const cfAppData = cfEnv.isOnCf
7676
? {
7777
[FIELD.COMPONENT_TYPE]: "application",
7878
[FIELD.COMPONENT_NAME]: cfApp.application_name,
@@ -93,7 +93,7 @@ class Logger {
9393
type = "log",
9494
maxLevel = LEVEL.INFO,
9595
customData,
96-
format = isOnCF ? FORMAT.JSON : FORMAT.TEXT,
96+
format = cfEnv.isOnCf ? FORMAT.JSON : FORMAT.TEXT,
9797
inspectOptions = { colors: false },
9898
} = {}
9999
) {

test/logger.test.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ const featureTogglesModule = require("../src/featureToggles");
55
const { FeatureToggles } = featureTogglesModule;
66
const { FORMAT, Logger } = require("../src/logger");
77

8-
const redisWrapper = require("../src/redisWrapper");
8+
const redisWrapperMock = require("../src/redisWrapper");
99
jest.mock("../src/redisWrapper", () => require("./__mocks__/redisWrapper"));
1010

11+
const envMock = require("../src/env");
12+
jest.mock("../src/env", () => require("./__mocks__/env"));
13+
1114
const { FEATURE, mockConfig, redisKey, redisChannel, refreshMessage } = require("./mockdata");
1215

1316
let featureToggles = null;
@@ -18,10 +21,14 @@ const processStreamSpy = {
1821
};
1922

2023
const cleanupTextLogCalls = (args) =>
21-
args.map((arg) =>
22-
typeof arg !== "string"
23-
? arg
24-
: arg.replace(/\d\d:\d\d:\d\d.\d\d\d/g, "88:88:88.888").replace(/(?<=\n)\s+at.*?\n/g, "")
24+
args.map(
25+
(arg) =>
26+
typeof arg !== "string"
27+
? arg
28+
: arg
29+
.replace(/\d\d:\d\d:\d\d.\d\d\d/g, "88:88:88.888") // timestamps
30+
.replace(/\n$/g, "") // final newline
31+
.replace(/(?<=\n)\s+at.*?\n/g, "") // stacktrace
2532
);
2633

2734
const cleanupJsonLogCalls = (args) =>
@@ -50,7 +57,8 @@ let layer = "/test";
5057

5158
describe("logger test", () => {
5259
beforeEach(() => {
53-
redisWrapper._reset();
60+
redisWrapperMock._reset();
61+
envMock._reset();
5462
featureToggles = new FeatureToggles({ redisKey, redisChannel, refreshMessage });
5563
});
5664

@@ -72,12 +80,10 @@ describe("logger test", () => {
7280
"88:88:88.888 | WARN | /test | FeatureTogglesError: found invalid fallback values during initialization
7381
{
7482
validationErrors: '[{"featureKey":"test/feature_b","errorMessage":"registered validator \\\\"{0}\\\\" failed for value \\\\"{1}\\\\" with error {2}","errorMessageValues":["mockConstructor",1,"bad validator"]}]'
75-
}
76-
",
83+
}",
7784
],
7885
[
79-
"88:88:88.888 | INFO | /test | finished initialization with 9 feature toggles with CF_REDIS
80-
",
86+
"88:88:88.888 | INFO | /test | finished initialization with 9 feature toggles with CF_REDIS",
8187
],
8288
]
8389
`);
@@ -90,8 +96,7 @@ caused by: Error: bad validator
9096
validator: 'mockConstructor',
9197
featureKey: 'test/feature_b',
9298
value: 1
93-
}
94-
",
99+
}",
95100
],
96101
]
97102
`);
@@ -136,8 +141,7 @@ caused by: Error: bad validator
136141
logger.info("some info");
137142
expect(processStreamSpy.stdout.mock.calls.map(cleanupTextLogCalls)[0]).toMatchInlineSnapshot(`
138143
[
139-
"88:88:88.888 | INFO | some info
140-
",
144+
"88:88:88.888 | INFO | some info",
141145
]
142146
`);
143147
expect(processStreamSpy.stdout.mock.calls.length).toBe(1);
@@ -159,8 +163,19 @@ caused by: Error: bad validator
159163
logger.info("some info");
160164
expect(processStreamSpy.stdout.mock.calls.map(cleanupTextLogCalls)[0]).toMatchInlineSnapshot(`
161165
[
162-
"88:88:88.888 | INFO | /test | some info
163-
",
166+
"88:88:88.888 | INFO | /test | some info",
167+
]
168+
`);
169+
expect(processStreamSpy.stdout.mock.calls.length).toBe(1);
170+
});
171+
172+
it("info on cf defaults to json format", async () => {
173+
envMock.cfEnv.isOnCf = true;
174+
logger = new Logger(layer);
175+
logger.info("some info");
176+
expect(processStreamSpy.stdout.mock.calls.map(cleanupJsonLogCalls)[0]).toMatchInlineSnapshot(`
177+
[
178+
"{"level":"info","msg":"some info","type":"log","layer":"/test"}",
164179
]
165180
`);
166181
expect(processStreamSpy.stdout.mock.calls.length).toBe(1);

0 commit comments

Comments
 (0)