Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrap logfmt message with double quotes #280

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,11 @@ export class Logger {

let logEntry = '';
keys.forEach(key => {
logEntry += `${key}=${parsedJSON[key]} `;
let logMsg = `${parsedJSON[key]}`;
if (logMsg.trim().includes(' ')) {
logMsg = `"${logMsg}"`;
}
logEntry += `${key}=${logMsg} `;
});
if (loggerStream.stream) {
loggerStream.stream.write(logEntry.trimRight() + '\n');
Expand Down
46 changes: 31 additions & 15 deletions test/unit/loggerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe('Logger', () => {
expect(cbSpy.callCount).to.be.equal(0);
});
});

describe('filters', () => {
const sid = 'SIDHERE!';
const simpleString = `sid=${sid}`;
Expand Down Expand Up @@ -380,32 +381,47 @@ describe('Logger', () => {
it('should transform to logfmt streams', () => {
let output = '';

const out = $$.SANDBOX.stub(process.stdout, 'write');
const err = $$.SANDBOX.stub(process.stderr, 'write').callsFake(error => {
output += error;
const out = $$.SANDBOX.stub(process.stdout, 'write').callsFake(info => {
output += info;
});

const testStream1: LoggerStream = {
name: 'test stream 1',
level: LoggerLevel.DEBUG,
stream: process.stderr
};
const testStream2: LoggerStream = {
name: 'test stream 2',
const testStream: LoggerStream = {
name: 'test stream',
level: LoggerLevel.INFO,
stream: process.stdout
};

const testLogger = new Logger({ name: 'testLogger', format: LoggerFormat.LOGFMT });
testLogger.addStream(testStream1);
testLogger.addStream(testStream2);

// const logger = await Logger.root();
testLogger.addStream(testStream);
testLogger.addField('container_id', '1234567890');

testLogger.info('info');
out.restore();
err.restore();

expect(output).to.contain('msg=info');
expect(output).to.contain('container_id=1234567890');
});

it('should wrap LogFmt message with quotes', () => {
let output = '';

const out = $$.SANDBOX.stub(process.stdout, 'write').callsFake(info => {
output += info;
});

const testStream: LoggerStream = {
name: 'test stream',
level: LoggerLevel.INFO,
stream: process.stdout
};

const testLogger = new Logger({ name: 'testLogger', format: LoggerFormat.LOGFMT });
testLogger.addStream(testStream);

testLogger.info('some long message with white space in between');
out.restore();

expect(output).to.contain('msg="some long message with white space in between"');
});
});
});