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

Commit

Permalink
Translate log level to string in production logs
Browse files Browse the repository at this point in the history
This involves a regex to replace the level in the string as per pinojs/pino#279
  • Loading branch information
Charlie Briggs committed Jan 11, 2018
1 parent 150073e commit 2e80695
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
coverage
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -12,17 +12,17 @@ The logger's API is identical to that of pino with the following exceptions:

* The property `sourcetype: _json` is added to logs in production for Splunk compatibility.
* Lambda related environment variables are added by default:
* AWS_EXECUTION_ENV,
* AWS_LAMBDA_FUNCTION_NAME,
* AWS_LAMBDA_FUNCTION_MEMORY_SIZE,
* AWS_LAMBDA_FUNCTION_VERSION
* `AWS_EXECUTION_ENV`,
* `AWS_LAMBDA_FUNCTION_NAME`,
* `AWS_LAMBDA_FUNCTION_MEMORY_SIZE`,
* `AWS_LAMBDA_FUNCTION_VERSION`
* Defaults to ISO timestamp logging for splunk compatiblity. At the time of writing this incurs a 25% pino performance penalty.

### Pino properties

Pino adds the following properties to logs by default:

* `level` - the log level in integer form. Refer to the `pino` documentation to translate this.
* `level` - the log level in string form. This is translated from the `pino` default of logging an integer representation.
* `v` - the pino logger API version.
* `hostname` - the hostname the process is running on.
* `pid` - the process PID.
Expand Down
13 changes: 12 additions & 1 deletion lib/__tests__/logger.test.js
@@ -1,3 +1,4 @@
import pino from 'pino';
import createLogger from '../logger';

const setupEnv = ({ envKey, value, setter }) => {
Expand Down Expand Up @@ -65,7 +66,7 @@ describe('log formatting', () => {

test('it logs JSON to stdout in the correct format', () => {
const expectedLog = {
level: 30,
level: 'info',
message: 'someMessage',
someObject: { withNesting: true },
sourceType: '_json',
Expand All @@ -79,6 +80,16 @@ describe('log formatting', () => {
expect(callJson).toEqual(expectedLog);
});

Object.keys(pino.levels.values).forEach(level => {
test(`it writes log level as the appropriate strings when the level is ${level}`, () => {
logger = createLogger();
logger[level]({ someObject: { withNesting: true } }, 'someMessage');

const callJson = getLogObject();
expect(callJson).toHaveProperty('level', level);
});
});

test('it includes variable properties', () => {
logger = createLogger();
logger.info({ someObject: { withNesting: true } }, 'someMessage');
Expand Down
47 changes: 33 additions & 14 deletions lib/logger.js
Expand Up @@ -7,24 +7,43 @@ const isLambda = () =>
false
);

const getBaseLogger = () => {
const level = process.env.CONSOLE_LOG_LEVEL || 'trace';
const stringifyVersion = chunk =>
chunk.replace(
/("level":\s*)(\d+)/,
(match, keyString, levelString) =>
`${keyString}"${pino.levels.labels[parseInt(levelString, 10)]}"`,
);

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

if (!isProduction()) {
prettyPrint = pino.pretty({ forceColor: true });
const getProductionStream = () => ({
write: writeLog,
});

prettyPrint.pipe(process.stdout);
}
const getPrettyPrintedStream = () => {
const stream = pino.pretty({ forceColor: true });

return pino({
prettyPrint,
level,
messageKey: 'message',
// enable ISO time stamps rather than epoch time
timestamp: pino.stdTimeFunctions.slowTime,
});
stream.pipe(process.stdout);
return stream;
};

const getBaseLogger = () => {
const level = process.env.CONSOLE_LOG_LEVEL || 'trace';
const stream = isProduction()
? getProductionStream()
: getPrettyPrintedStream();

return pino(
{
level,
messageKey: 'message',
// enable ISO time stamps rather than epoch time
timestamp: pino.stdTimeFunctions.slowTime,
},
stream,
);
};

const getMetaData = () => ({
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
@@ -1,8 +1,7 @@
{
"name": "@financial-times/lambda-logger",
"version": "1.0.0",
"description":
"Logger used by the lambda team. Logs in JSON format using pino",
"description": "Logger used by the lambda team. Logs in JSON format using pino",
"main": "dist/@financial-times/lambda-logger.js",
"browser": "dist/@financial-times/lambda-logger.umd.js",
"module": "dist/@financial-times/lambda-logger.m.js",
Expand All @@ -16,7 +15,10 @@
"precommit": "lint-staged"
},
"lint-staged": {
"*.js": ["eslint --cache --fix", "git add"]
"*.js": [
"eslint --cache --fix",
"git add"
]
},
"engines": {
"node": ">= 6.10.0"
Expand Down

0 comments on commit 2e80695

Please sign in to comment.