Skip to content

Commit

Permalink
[PROD-45916] Add callback to logger before stringifying message
Browse files Browse the repository at this point in the history
  • Loading branch information
klesgidis committed Aug 8, 2024
1 parent 2a89db2 commit bf796e4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/appenders/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const formatterResolver = require('../formatters/FormatterResolver');
const getOut = (opts = {}) => {
const color = !!opts.color;
const styles = opts.styles;
const loggerCallback = opts.loggerCallback;
const date = true;

const out = (data, options) => {
Expand All @@ -15,7 +16,7 @@ const getOut = (opts = {}) => {

if (log_tag) {
for (const i in styles) {
const formatter = formatterResolver(styles[i], color, date, log_tag);
const formatter = formatterResolver(styles[i], color, date, log_tag, loggerCallback);
const fmt = formatter.formatObject(data, options);
process.stdout.write(`${fmt}\n`);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/formatters/BaseFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ const colorStatusCodes = {
};

class BaseFormatter {
constructor(color = false, appendDate = false, requestType = '') {
constructor(color = false, appendDate = false, requestType = '', loggerCallback = null) {
this.color = color;
this.appendDate = appendDate;
this.requestType = requestType;
this.loggerCallback = loggerCallback;
}

formatObject(obj) {
Expand Down
6 changes: 4 additions & 2 deletions lib/formatters/FormatterResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const formatters = {
json: JsonFormatter
};

module.exports = (type = '', color = false, appendDate = false, requestType = '') => {
return formatters[type] ? new formatters[type](color, appendDate, requestType) : new MissingFormatter(type);
module.exports = (type = '', color = false, appendDate = false, requestType = '', loggerCallback = null) => {
return formatters[type]
? new formatters[type](color, appendDate, requestType, loggerCallback)
: new MissingFormatter(type);
};
8 changes: 5 additions & 3 deletions lib/formatters/JsonFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const merge = require('lodash/merge');
const BaseFormatter = require('./BaseFormatter');

class JsonFormatter extends BaseFormatter {
constructor(color = false, appendDate = false, requestType = '') {
super(false, appendDate, requestType);
constructor(color = false, appendDate = false, requestType = '', loggerCallback = null) {
super(false, appendDate, requestType, loggerCallback);
}

formatObject(obj) {
let jsonObject = obj instanceof Error ? this.formatErrorObject(obj) : this.formatValidObject(obj);

if (this.loggerCallback && typeof this.loggerCallback === 'function') {
this.loggerCallback(jsonObject);
}
return JSON.stringify(jsonObject);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const { isEmpty } = require('lodash');
module.exports = (options = {}) => {
const color = typeof options.color === 'boolean' ? options.color : true;
const styles = !isEmpty(options.styles) ? options.styles : ['simple'];
const defaultLogger = out({ color, styles });
const loggerCallback = options.loggerCallback;
const defaultLogger = out({ color, styles, loggerCallback });
return {
color,
styles,
Expand Down
20 changes: 20 additions & 0 deletions test/lib/formatters/JsonFormatterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,24 @@ describe('Test Json Formatter', () => {
json.message.should.equal('test-error');
json.stack_trace.should.equal(obj.stack);
});

it('should call lggerCallback', () => {
const stub = sandbox.stub();
const formatter = new JsonFormatter(false, false, '', stub);
const obj = {
method: 'GET',
requestId: 'testRequestId',
path: 'test-path',
log_tag: 'inbound_request',
duration: 50
};
formatter.formatObject(obj);
const event = stub.args[0][0];

event.severity.should.equal('info');
event['logging.googleapis.com/operation'].id.should.equal(obj.requestId);
event.requestId.should.equal(obj.requestId);
event.httpRequest.requestUrl.should.equal(obj.path);
event.httpRequest.latency.should.equal('0.050000s');
});
});
1 change: 1 addition & 0 deletions types/riviere.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function riviere(options?: {
info: any,
error: any
},
loggerCallback?: (event: any) => void,
inbound?: {
enabled: boolean,
request?: {
Expand Down

0 comments on commit bf796e4

Please sign in to comment.