Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Make process.stdout.write blocking if on a lambda
Browse files Browse the repository at this point in the history
This is not reliable anymore after AWS changes.

See 'nodejs/node#1741 (comment)'
  • Loading branch information
Charlie Briggs committed Jul 27, 2018
1 parent bb2e0b6 commit bec792f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
extends: ['@financial-times/de-tooling', 'prettier'],
plugins: ['prettier'],
globals: { awslambda: true },
rules: {
'prettier/prettier': 'error',
},
Expand Down
23 changes: 11 additions & 12 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
const pino = require('pino');

const isProduction = () => process.env.NODE_ENV === 'production';
Expand All @@ -15,21 +14,12 @@ const stringifyVersion = chunk =>
`${keyString}"${pino.levels.labels[parseInt(levelString, 10)]}"`
);

const writeLambdaLog = chunk => {
return awslambda.sendConsoleLogs(stringifyVersion(chunk));
};

const writeLog = chunk => {
return process.stdout.write(stringifyVersion(chunk));
process.stdout.write(stringifyVersion(chunk));
};

const getProductionStream = () => ({
write:
isLambda() &&
awslambda &&
typeof awslambda.sendConsoleLogs === 'function'
? writeLambdaLog
: writeLog,
write: writeLog,
});

const getPrettyPrintedStream = () => {
Expand Down Expand Up @@ -77,6 +67,15 @@ const getMetaData = () => ({
});

const getLoggerWithMetadata = () => {
/* eslint-disable no-underscore-dangle */
if (
isLambda() &&
process.stdout._handle &&
typeof process.stdout._handle.setBlocking === 'function'
) {
process.stdout._handle.setBlocking(true);
}
/* eslint-enable no-underscore-dangle */
const logger = getBaseLogger();
const metadata = getMetaData();

Expand Down

0 comments on commit bec792f

Please sign in to comment.