Skip to content

Commit 7e6e7b0

Browse files
authored
Make timestamp prefix in logs configurable (#1118)
2 parents 4481873 + 3c3b697 commit 7e6e7b0

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All changes that impact users of this module are documented in this file, in the [Common Changelog](https://common-changelog.org) format with some additional specifications defined in the CONTRIBUTING file. This codebase adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
## Unreleased [minor]
6+
7+
> Development of this release was supported by the [French Ministry for Foreign Affairs](https://www.diplomatie.gouv.fr/fr/politique-etrangere-de-la-france/diplomatie-numerique/) through its ministerial [State Startups incubator](https://beta.gouv.fr/startups/open-terms-archive.html) under the aegis of the Ambassador for Digital Affairs.
8+
9+
### Added
10+
11+
- Add configuration option to toggle timestamp prefix in logs; set [`@opentermsarchive/engine.logger.timestampPrefix` to `true` or `false` in your configuration file](https://docs.opentermsarchive.org/#configuring) to control this feature.
12+
513
## 2.6.0 - 2024-11-19
614

715
_Full changeset and discussions: [#1116](https://github.com/OpenTermsArchive/engine/pull/1116)._

config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"host": "smtp-relay.sendinblue.com",
4949
"username": "[email protected]"
5050
},
51-
"sendMailOnError": false
51+
"sendMailOnError": false,
52+
"timestampPrefix": true
5253
},
5354
"notifier": {
5455
"sendInBlue": {

scripts/dataset/logger/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import config from 'config';
12
import winston from 'winston';
23

34
import logger from '../../../src/logger/index.js';
@@ -6,11 +7,13 @@ const { combine, timestamp, printf, colorize } = winston.format;
67

78
logger.format = combine(
89
colorize(),
9-
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
10+
timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }),
1011
printf(({ level, message, counter, hash, timestamp }) => {
1112
const prefix = counter && hash ? `${counter.toString().padEnd(6)} ${hash.padEnd(40)}` : '';
1213

13-
return `${timestamp} ${level.padEnd(15)} ${prefix.padEnd(50)} ${message}`;
14+
const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : '';
15+
16+
return `${timestampPrefix}${level.padEnd(15)} ${prefix.padEnd(50)} ${message}`;
1417
}),
1518
);
1619

src/collection-api/logger.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
3030
const logger = winston.createLogger({
3131
format: combine(
3232
colorize(),
33-
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
34-
printf(({ level, message, timestamp }) => `${timestamp} ${level.padEnd(15)} ${message}`),
33+
timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }),
34+
printf(({ level, message, timestamp }) => {
35+
const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : '';
36+
37+
return `${timestampPrefix}${level.padEnd(15)} ${message}`;
38+
}),
3539
),
3640
transports,
3741
rejectionHandlers: transports,

src/logger/index.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,17 @@ const { combine, timestamp, printf, colorize } = winston.format;
88

99
const alignedWithColorsAndTime = combine(
1010
colorize(),
11-
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
11+
timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }),
1212
printf(({ level, message, timestamp, serviceId, termsType, documentId }) => {
13-
let prefix = '';
13+
const servicePrefix = serviceId && termsType
14+
? `${serviceId}${termsType}${documentId ? `:${documentId}` : ''}`
15+
: '';
1416

15-
if (serviceId && termsType) {
16-
prefix = `${serviceId}${termsType}`;
17-
}
18-
19-
if (documentId) {
20-
prefix = `${prefix}:${documentId}`;
21-
}
17+
const truncatedPrefix = servicePrefix.length > 75 ? `${servicePrefix.slice(0, 74)}…` : servicePrefix;
2218

23-
if (prefix.length > 75) {
24-
prefix = `${prefix.substring(0, 74)}…`;
25-
}
19+
const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : '';
2620

27-
return `${timestamp} ${level.padEnd(15)} ${prefix.padEnd(75)} ${message}`;
21+
return `${timestampPrefix}${level.padEnd(15)} ${truncatedPrefix.padEnd(75)} ${message}`;
2822
}),
2923
);
3024

0 commit comments

Comments
 (0)