Skip to content

Commit 3f71ecd

Browse files
fix(#10357): prevent DEBUG logs from appearing in production (#10583)
Co-authored-by: Amir Saudagar <saudagaramir55@gmail.com>
1 parent b3fb12b commit 3f71ecd

10 files changed

Lines changed: 57 additions & 8 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ env:
1313
ECR_REPO: '720541322708.dkr.ecr.eu-west-2.amazonaws.com/medic'
1414
ECR_PUBLIC_REPO: 'public.ecr.aws/medic'
1515
COUCHDB_LOG_LEVEL: 'debug'
16+
LOG_LEVEL: 'debug'
1617
TAG: ${{ (github.ref_type == 'tag' && github.ref_name) || '' }}
1718
BRANCH: ${{ github.head_ref || github.ref_name }}
1819
BUILD_NUMBER: ${{ github.run_id }}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"build-webapp-dev": "cd webapp && npm run build -- --configuration=development && npm run compile",
2828
"build-cht-form": "./scripts/build/build-prepare.sh && cd webapp && npm run build:cht-form",
2929
"copy-api-resources": "cp -r api/src/public/* api/build/static/",
30-
"dev-api": "./scripts/build/copy-static-files.sh && (npm run --prefix shared-libs/cht-datasource build-watch & npm run --prefix api run-watch)",
31-
"dev-sentinel": "npm run --prefix shared-libs/cht-datasource build-watch & npm run --prefix sentinel run-watch",
30+
"dev-api": "export LOG_LEVEL=debug && ./scripts/build/copy-static-files.sh && (npm run --prefix shared-libs/cht-datasource build-watch & npm run --prefix api run-watch)",
31+
"dev-sentinel": "export LOG_LEVEL=debug; npm run --prefix shared-libs/cht-datasource build-watch & npm run --prefix sentinel run-watch",
3232
"local-images": "export VERSION=$(node ./scripts/build/get-version.js) && ./scripts/build/build-service-images.sh && node scripts/build/cli localDockerComposeFiles",
3333
"update-service-worker": "node scripts/build/cli updateServiceWorker",
3434
"-- DEV TEST SCRIPTS": "-----------------------------------------------------------------------------------------------",

scripts/build/helm/templates/api/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ spec:
1919
terminationGracePeriodSeconds: 0
2020
containers:
2121
- env:
22+
- name: LOG_LEVEL
23+
value: '{{ .Values.api.log_level | default "info" }}'
2224
- name: BUILDS_URL
2325
value: {{ .Values.upstream_servers.builds_url | default "https://staging.dev.medicmobile.org/_couch/builds_4" }}
2426
- name: COUCH_URL

scripts/build/helm/templates/sentinel/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ spec:
1818
spec:
1919
containers:
2020
- env:
21+
- name: LOG_LEVEL
22+
value: '{{ .Values.sentinel.log_level | default "info" }}'
2123
- name: API_HOST
2224
value: api.{{ .Values.namespace }}.svc.cluster.local
2325
- name: COUCH_URL

scripts/build/helm/tests/integration-k3d-values.yaml.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ local_storage:
2727
preExistingDiskPath-2: "{{{data_path}}}/srv2"
2828
preExistingDiskPath-3: "{{{data_path}}}/srv3"
2929

30+
# API log level for integration tests
31+
api:
32+
log_level: "debug"
33+
34+
# Sentinel log level for integration tests
35+
sentinel:
36+
log_level: "debug"

scripts/build/helm/values/base.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ cht_image_tag: "{{cht_image_tag}}" # TEMPLATE: This is a template variable, not
88
api:
99
service:
1010
type: ClusterIP # DEFAULT: Good default for internal services
11+
log_level: "info" # DEFAULT: Log level for API service
12+
13+
# Sentinel Service configuration
14+
sentinel:
15+
log_level: "info" # DEFAULT: Log level for Sentinel service
1116

1217
# If images are cached, the same image tag will never be pulled twice. For development, this means that it's not
1318
# possible to upgrade to a newer version of the same branch, as the old image will always be reused.

shared-libs/logger/src/node-logger.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { createLogger, format, transports } = require('winston');
2-
const env = process.env.NODE_ENV || 'development';
2+
// LOG_LEVEL controls logging globally. Defaults to 'info' if not explicitly set.
3+
const logLevel = process.env.LOG_LEVEL || 'info';
4+
35

46
const cleanUpErrorsFromSymbolProperties = (info) => {
57
if (!info) {
@@ -61,7 +63,7 @@ module.exports.create = (dateFormat) => createLogger({
6163
transports: [
6264
new transports.Console({
6365
// change level if in dev environment versus production
64-
level: env === 'development' ? 'debug' : 'info',
66+
level: logLevel,
6567
format: format.combine(
6668
// https://github.com/winstonjs/winston/issues/1345
6769
format(info => {

shared-libs/logger/test/index.spec.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,30 @@ describe('node-logger internals', () => {
123123
});
124124

125125
describe('create', () => {
126-
it('uses info level in production environment', () => {
127-
nodeLogger.__set__('env', 'production');
128-
const logger = nodeLogger.create('YYYY-MM-DD');
126+
it('defaults to info level when LOG_LEVEL is not set', () => {
127+
const originalLogLevel = process.env.LOG_LEVEL;
128+
delete process.env.LOG_LEVEL;
129+
const freshLogger = rewire('../src/node-logger');
130+
const logger = freshLogger.create('YYYY-MM-DD');
129131
expect(logger).to.be.ok;
132+
expect(freshLogger.__get__('logLevel')).to.equal('info');
133+
if (originalLogLevel !== undefined) {
134+
process.env.LOG_LEVEL = originalLogLevel;
135+
}
136+
});
137+
138+
it('uses LOG_LEVEL when set', () => {
139+
const originalLogLevel = process.env.LOG_LEVEL;
140+
process.env.LOG_LEVEL = 'debug';
141+
const freshLogger = rewire('../src/node-logger');
142+
const logger = freshLogger.create('YYYY-MM-DD');
143+
expect(logger).to.be.ok;
144+
expect(freshLogger.__get__('logLevel')).to.equal('debug');
145+
if (originalLogLevel !== undefined) {
146+
process.env.LOG_LEVEL = originalLogLevel;
147+
} else {
148+
delete process.env.LOG_LEVEL;
149+
}
130150
});
131151
});
132152

tests/cht-core-test.override.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
api:
3+
environment:
4+
- "LOG_LEVEL=debug"
5+
sentinel:
6+
environment:
7+
- "LOG_LEVEL=debug"

tests/utils/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const CONTAINER_NAMES = {};
6363
const originalTranslations = {};
6464
const COUCH_USER_ID_PREFIX = 'org.couchdb.user:';
6565
const COMPOSE_FILES = ['cht-core', 'cht-couchdb-cluster'];
66+
const COMPOSE_OVERRIDE_FILE = path.resolve(__dirname, '../cht-core-test.override.yml');
6667
const PERMANENT_TYPES = [DOC_TYPES.TRANSLATIONS, 'translations-backup', 'user-settings', 'info'];
6768
const db = new PouchDB(`${constants.BASE_URL}/${constants.DB_NAME}`, { auth });
6869
const sentinelDb = new PouchDB(`${constants.BASE_URL}/${constants.DB_NAME}-sentinel`, { auth });
@@ -918,6 +919,7 @@ const listenForApi = async () => {
918919

919920
const dockerComposeCmd = (params) => {
920921
const composeFiles = COMPOSE_FILES.map(file => ['-f', getTestComposeFilePath(file)]).flat();
922+
composeFiles.push('-f', COMPOSE_OVERRIDE_FILE);
921923
params = `docker compose ${composeFiles.join(' ')} -p ${PROJECT_NAME} ${params}`;
922924

923925
return runCommand(params);
@@ -1207,7 +1209,8 @@ const generateComposeFiles = async () => {
12071209
const testComposePath = getTestComposeFilePath(file);
12081210

12091211
const template = await fs.promises.readFile(templatePath, 'utf-8');
1210-
await fs.promises.writeFile(testComposePath, mustache.render(template, view));
1212+
const compiled = mustache.render(template, view);
1213+
await fs.promises.writeFile(testComposePath, compiled);
12111214
}
12121215
};
12131216

0 commit comments

Comments
 (0)