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

feat: add test logger #365

Merged
merged 1 commit into from
Oct 21, 2024
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
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ module.exports = {
...require('./coralogix'),
...require('./bunyan'),
...require('./secret'),
...require('./testing'),
};
55 changes: 55 additions & 0 deletions src/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const { MemLogger, serializeMessage, SimpleInterface } = require('./log');

const ANSI_REGEXP = RegExp([
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))',
].join('|'), 'g');

/**
* Creates a test logger that logs to the console but also to an internal buffer. The contents of
* the buffer can be retrieved with {@code Logger#getOutput()} which will flush also close the
* logger. Each test logger will be registered with a unique category, so that there is no risk of
* reusing a logger in between tests.
* @params {object} config logger config
* @params {string} [config.level='debug'] log level
* @params {bool} [config.keepANSI=false] enable to keep ANSI characters in string
* @returns {SimpleInterface}
*/
function createTestLogger(config = {}) {
const memLogger = new MemLogger({
level: 'silly',
});

const plainMessage = (msg) => serializeMessage(msg.message);
const stripMessage = (msg) => serializeMessage(msg.message).replace(ANSI_REGEXP, '');
const msgFormat = config.keepANSI ? plainMessage : stripMessage;

const testLogger = new SimpleInterface({
level: config.level || 'debug',
logger: memLogger,
});

return new Proxy(testLogger, {
get(target, prop) {
if (prop === 'getOutput') {
return () => `${memLogger.buf.map((msg) => `${msg.level}: ${msgFormat(msg)}`).join('\n')}\n`;
}
return target[prop];
},
});
}

module.exports = {
createTestLogger,
};
23 changes: 23 additions & 0 deletions test/testing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */
const assert = require('assert');
const { createTestLogger } = require('../src/index');

describe('Test Logger', () => {
it('Creates test logger and produces output', () => {
const logger = createTestLogger();
logger.info('Hello, world.');
assert.strictEqual(logger.getOutput(), 'info: Hello, world.\n');
});
});