Skip to content

Commit

Permalink
fix: correct the usage of default logger for level and update test
Browse files Browse the repository at this point in the history
- fix issue where the global diag level affected trailing tests
  • Loading branch information
MSNev committed Feb 4, 2021
1 parent 3fae460 commit b681850
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/src/diag/logLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function createLogLevelDiagLogger(
}

if (!logger) {
logger = DiagAPI.instance().getLogger();
logger = DiagAPI.instance();
}

function _filterFunc(
Expand Down
58 changes: 44 additions & 14 deletions packages/opentelemetry-api/test/diag/logLevel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('LogLevelFilter DiagLogger', () => {
beforeEach(() => {
// set no logger
diag.setLogger(null as any);
diag.setLogLevel(DiagLogLevel.INFO);

// mock
dummyLogger = {} as DiagLogger;
Expand Down Expand Up @@ -213,6 +214,7 @@ describe('LogLevelFilter DiagLogger', () => {

it(`should use default logger for undefined and log ${fName} message with ${map.message} level`, () => {
diag.setLogger(dummyLogger);
diag.setLogLevel(DiagLogLevel.ALL);
const testLogger = createLogLevelDiagLogger(map.level, undefined);
testLogger[fName](`${fName} called %s`, 'param1');
diagLoggerFunctions.forEach(lName => {
Expand All @@ -229,6 +231,7 @@ describe('LogLevelFilter DiagLogger', () => {

it(`should use default logger for null and log ${fName} message with ${map.message} level`, () => {
diag.setLogger(dummyLogger);
diag.setLogLevel(DiagLogLevel.ALL);
const testLogger = createLogLevelDiagLogger(map.level, null);
testLogger[fName](`${fName} called %s`, 'param1');
diagLoggerFunctions.forEach(lName => {
Expand Down Expand Up @@ -260,21 +263,48 @@ describe('LogLevelFilter DiagLogger', () => {
});
});

it(`diag setLogLevel is ignored for specific logLevel logger and should log ${fName} message with ${map.message} level`, () => {
diag.setLogger(dummyLogger);
diag.setLogLevel(DiagLogLevel.NONE);
levelMap.forEach(masterLevelMap => {
it(`diag setLogLevel is not ignored when set to ${masterLevelMap.message} and using default logger to log ${fName} message with ${map.message} level`, () => {
diag.setLogger(dummyLogger);
diag.setLogLevel(masterLevelMap.level);

const testLogger = createLogLevelDiagLogger(map.level);
testLogger[fName](`${fName} called %s`, 'param1');
diagLoggerFunctions.forEach(lName => {
if (fName === lName && map.ignoreFuncs.indexOf(lName) === -1) {
assert.deepStrictEqual(calledArgs[lName], [
`${fName} called %s`,
'param1',
]);
} else {
assert.strictEqual(calledArgs[lName], null);
}
const testLogger = createLogLevelDiagLogger(map.level);
testLogger[fName](`${fName} called %s`, 'param1');
diagLoggerFunctions.forEach(lName => {
if (
fName === lName &&
map.ignoreFuncs.indexOf(lName) === -1 &&
masterLevelMap.ignoreFuncs.indexOf(lName) === -1
) {
assert.deepStrictEqual(calledArgs[lName], [
`${fName} called %s`,
'param1',
]);
} else {
assert.strictEqual(calledArgs[lName], null);
}
});
});

it(`diag setLogLevel is ignored when set to ${masterLevelMap.message} when using a specific logger to log ${fName} message with ${map.message} level`, () => {
diag.setLogger(dummyLogger);
diag.setLogLevel(masterLevelMap.level);

const testLogger = createLogLevelDiagLogger(
map.level,
diag.getLogger()
);
testLogger[fName](`${fName} called %s`, 'param1');
diagLoggerFunctions.forEach(lName => {
if (fName === lName && map.ignoreFuncs.indexOf(lName) === -1) {
assert.deepStrictEqual(calledArgs[lName], [
`${fName} called %s`,
'param1',
]);
} else {
assert.strictEqual(calledArgs[lName], null);
}
});
});
});

Expand Down

0 comments on commit b681850

Please sign in to comment.