Skip to content

Commit ee8aa12

Browse files
authored
Fb/new logger (#20)
* add grueni-logger as reference * some logger level changes * start logger v2 * more loggering * bla bla progress * bla bla progress 2 * bla bla progress 3 * bla bla progress 4 * bla bla proud of our progress 5 * bla bla proud of our progress 6 * bla bla proud of our progress 7 * bla bla proud of our progress 8 * bla bla proud of our progress 9 * bla bla proud of our progress 10 * bla bla proud of our progress 11 * slightly better handling of non-cf case * update snapshots * some more work on request logger * child approach * cds stuff * update tests * switch to loggerV2 * remove dependency * more changes to incorporate new Logger * fix cds context reading bugs * add level_name enum and logging for subdomain if present * test fixes * new todos and consolidate logger * progress 1 * lots of changes after discussions with max and make _ts nanos * tests and logger api changes * add instance id field * help intellisense autocomplete * readable => format * lil cleanup pass * readable level should be uppercase * update snapshots where readable levels are uppercase * fix component instance approach * keep it in the js number range * convert instance index to an integer * explain hrtime trick * wording * more proofreading * only log error info if it has content * remove unneeded mocking * remove main TODO for this PR * better mocking for cfEnv * move up tryRequire to other requires
1 parent cf01882 commit ee8aa12

File tree

12 files changed

+732
-4440
lines changed

12 files changed

+732
-4440
lines changed

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

33
module.exports = {
4+
prettierPath: null,
5+
46
// A list of paths to modules that run some code to configure or set up the testing framework before each test
57
setupFilesAfterEnv: ["./jest.setupAfterEnv.js"],
68
};

package-lock.json

Lines changed: 327 additions & 4225 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"npm": ">=8.19.0"
2424
},
2525
"dependencies": {
26-
"cf-nodejs-logging-support": "^7.1.0",
2726
"redis": "^4.6.7",
2827
"verror": "^1.10.1",
2928
"yaml": "^2.3.1"

src/env.js

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
11
"use strict";
22

3-
const VError = require("verror");
4-
5-
const { Logger } = require("./logger");
63
const { ENV } = require("./shared/static");
74

8-
const COMPONENT_NAME = "/Env";
9-
const VERROR_CLUSTER_NAME = "Env";
10-
11-
const isLocal = process.env[ENV.USER] !== "vcap";
12-
const isOnCF = !isLocal;
13-
14-
const logger = new Logger(COMPONENT_NAME, isOnCF);
5+
const isOnCF = process.env[ENV.USER] === "vcap";
156

167
class CfEnv {
178
static parseEnvVar(env, envVar) {
189
try {
1910
if (Object.prototype.hasOwnProperty.call(env, envVar)) {
2011
return JSON.parse(process.env[envVar]);
2112
}
22-
} catch (err) {
23-
logger.error(
24-
new VError(
25-
{
26-
name: VERROR_CLUSTER_NAME,
27-
cause: err,
28-
info: {
29-
envVar,
30-
},
31-
},
32-
"environment variable is not valid JSON"
33-
)
34-
);
35-
}
13+
} catch (err) {} // eslint-disable-line no-empty
3614
}
3715

3816
constructor(env = process.env) {
39-
this.__cfApp = CfEnv.parseEnvVar(env, ENV.CF_APP) || {};
40-
this.__cfServices = CfEnv.parseEnvVar(env, ENV.CF_SERVICES) || {};
41-
this.__cfServiceList = [].concat(...Object.values(this.__cfServices));
17+
this.cfApp = CfEnv.parseEnvVar(env, ENV.CF_APP) || {};
18+
this.cfServices = CfEnv.parseEnvVar(env, ENV.CF_SERVICES) || {};
19+
this.cfInstanceGuid = env[ENV.CF_INSTANCE_GUID];
20+
this.cfInstanceIp = env[ENV.CF_INSTANCE_IP];
21+
this.cfInstanceIndex = env[ENV.CF_INSTANCE_INDEX] ? parseInt(env[ENV.CF_INSTANCE_INDEX]) : undefined;
22+
this.__cfServiceList = [].concat(...Object.values(this.cfServices));
4223
this.__cfServiceLabelMap = this.__cfServiceList.reduce((result, service) => {
4324
if (service.label && !result[service.label]) {
4425
result[service.label] = service;
@@ -47,21 +28,16 @@ class CfEnv {
4728
}, {});
4829
}
4930

31+
/**
32+
* @return CfEnv
33+
*/
5034
static getInstance() {
5135
if (!CfEnv.__instance) {
5236
CfEnv.__instance = new CfEnv();
5337
}
5438
return CfEnv.__instance;
5539
}
5640

57-
cfApp() {
58-
return this.__cfApp;
59-
}
60-
61-
cfServices() {
62-
return this.__cfServices;
63-
}
64-
6541
cfServiceCredentials(options) {
6642
const service = this.__cfServiceList.find((service) =>
6743
Object.entries(options).reduce((result, [key, value]) => result && service[key] === value, true)
@@ -76,7 +52,8 @@ class CfEnv {
7652
}
7753

7854
module.exports = {
79-
isLocal,
55+
CfEnv,
56+
8057
isOnCF,
8158
cfEnv: CfEnv.getInstance(),
8259
};

src/featureToggles.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// TODO locale for validation messages
1515
// TODO investigate how to turn library into a cds plugin similar to https://github.com/cap-js-community/event-queue
1616
// this would incorporate a ready-made feature-service interface copied from example-cap-server
17-
// TODO remove dependency on cf-nodejs-logging-support. the way request level information are handled in there is
18-
// not good. either we build our own logging, or hook into cds.log and make cds a full dependency...
1917

2018
const { promisify } = require("util");
2119
const path = require("path");
@@ -97,7 +95,7 @@ const SCOPE_PREFERENCE_ORDER_MASKS = [
9795
];
9896

9997
const readFileAsync = promisify(readFile);
100-
let logger = new Logger(COMPONENT_NAME, isOnCF);
98+
let logger = new Logger(COMPONENT_NAME);
10199

102100
const readConfigFromFile = async (configFilepath = DEFAULT_CONFIG_FILEPATH) => {
103101
const fileData = await readFileAsync(configFilepath);
@@ -125,7 +123,7 @@ class FeatureToggles {
125123
* Populate this.__config.
126124
*/
127125
_processConfig(config) {
128-
const { uris: cfAppUris } = cfEnv.cfApp();
126+
const { uris: cfAppUris } = cfEnv.cfApp;
129127

130128
const configEntries = Object.entries(config);
131129
for (const [featureKey, { type, active, appUrl, validation, fallbackValue, allowedScopes }] of configEntries) {
@@ -221,7 +219,7 @@ class FeatureToggles {
221219
}
222220
let cfApp;
223221
try {
224-
cfApp = cfEnv.cfApp();
222+
cfApp = cfEnv.cfApp;
225223
if (cfApp.application_name) {
226224
return cfApp.application_name;
227225
}

0 commit comments

Comments
 (0)