Skip to content

Commit

Permalink
Merge pull request #9037 from guardian/ravi/consolidate-logs
Browse files Browse the repository at this point in the history
Fix cluster mode logs
  • Loading branch information
arelra authored Oct 6, 2023
2 parents 18446c5 + 297ff36 commit 4dbe62c
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions dotcom-rendering/src/server/lib/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import type { LoggingEvent } from 'log4js';
import { addLayout, configure, getLogger, shutdown } from 'log4js';
import { loggingStore } from './logging-store';

const logName = `dotcom-rendering.log`;

const logLocation =
process.env.NODE_ENV === 'production' &&
!process.env.DISABLE_LOGGING_AND_METRICS
? '/var/log/dotcom-rendering/dotcom-rendering.log'
: `${path.resolve('logs')}/dotcom-rendering.log`;

const stage =
typeof process.env.GU_STAGE === 'string'
? process.env.GU_STAGE.toUpperCase()
: 'DEV';
? `/var/log/dotcom-rendering/${logName}`
: `${path.resolve('logs')}/${logName}`;

const logFields = (logEvent: LoggingEvent): unknown => {
const { request } = loggingStore.getStore() ?? {
Expand All @@ -22,12 +19,17 @@ const logFields = (logEvent: LoggingEvent): unknown => {
const coreFields = {
stack: 'frontend',
app: 'dotcom-rendering',
stage,
stage:
typeof process.env.GU_STAGE === 'string'
? process.env.GU_STAGE.toUpperCase()
: 'DEV',
'@timestamp': logEvent.startTime,
'@version': 1,
level: logEvent.level.levelStr,
level_value: logEvent.level.level,
request,
// NODE_APP_INSTANCE is set by pm2
thread_name: process.env.NODE_APP_INSTANCE ?? '0',
};
// log4js uses any[] to type data but we want to coerce it here
// because we now depend on the type to log the result properly
Expand Down Expand Up @@ -79,6 +81,10 @@ const enableLog4j = {
development: { appenders: ['console'], level: 'info' },
},
pm2: true,
// log4js cluster mode handling does not work as it prevents
// logs from processes other than the main process from
// writing to the log.
disableClustering: true,
};

if (process.env.DISABLE_LOGGING_AND_METRICS === 'true') {
Expand Down

0 comments on commit 4dbe62c

Please sign in to comment.