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

Commit

Permalink
Merge pull request #38 from Financial-Times/handle-value-with-equals
Browse files Browse the repository at this point in the history
Handle equals sign in field value
  • Loading branch information
ironsidevsquincy authored Feb 8, 2017
2 parents 059fe73 + 9769279 commit 9b62401
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lib/format.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const sanitise = value => (typeof value === 'string') ? value.replace(/"/g, '\'').replace(/\n/g, ' ') : value;

const formatMessage = message => sanitise(message);

const formatValue = value => {
const splunkifyValue = value => {
if (Array.isArray(value)) {
return `"${value.map(sanitise).join(',')}"`;
} else {
const sanitisedValue = sanitise(value);
// wrap in quotes, if it contains a space
return (/\s/.test(sanitisedValue)) ? `"${sanitisedValue}"` : sanitisedValue;
return (/[\s=]/.test(sanitisedValue)) ? `"${sanitisedValue}"` : sanitisedValue;
}
};

const formatMessage = message => sanitise(message);

const formatFields = (fields = {}, { splunkFriendly = false } = {}) => {
const formattedFields = Object.keys(fields)
.map(fieldName => {
const fieldValue = splunkFriendly ? formatValue(fields[fieldName]) : fields[fieldName];
const fieldValue = splunkFriendly ? splunkifyValue(fields[fieldName]) : fields[fieldName];
return `${fieldName}=${fieldValue}`;
});

Expand Down
7 changes: 7 additions & 0 deletions test/lib/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ describe('Format', () => {
format.fields(meta, { splunkFriendly: true }).should.equal('msg="Bad response"');
});

it('should wrap values with equals (`=`) in double quotes', () => {
const meta = {
msg: 'Bad=response'
};
format.fields(meta, { splunkFriendly: true }).should.equal('msg="Bad=response"');
});

it('should convert double quotes to singles', () => {
const meta = {
msg: 'Server responded with "Bad Request", 400'
Expand Down

0 comments on commit 9b62401

Please sign in to comment.