Skip to content

Commit

Permalink
refactor: rename stringToLogLevel to diagLogLevelFromString
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Feb 13, 2025
1 parent 4a4f9d5 commit 3a0a6c3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import { NodeSDKConfiguration } from './types';
import {
getBooleanFromEnv,
getStringFromEnv,
stringToLogLevel,
diagLogLevelFromString,
} from '@opentelemetry/core';
import {
getResourceDetectorsFromEnv,
Expand Down Expand Up @@ -239,7 +239,7 @@ export class NodeSDK {
const logLevel = getStringFromEnv('OTEL_LOG_LEVEL');
if (logLevel != null) {
diag.setLogger(new DiagConsoleLogger(), {
logLevel: stringToLogLevel(logLevel),
logLevel: diagLogLevelFromString(logLevel),
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export { merge } from './utils/merge';
export { TimeoutError, callWithTimeout } from './utils/timeout';
export { isUrlIgnored, urlMatches } from './utils/url';
export { BindOnceFuture } from './utils/callback';
export { stringToLogLevel } from './utils/configuration';
export { diagLogLevelFromString } from './utils/configuration';
import { _export } from './internal/exporter';
export const internal = {
_export,
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-core/src/utils/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const logLevelMap: { [key: string]: DiagLogLevel } = {
* Convert a string to a {@link DiagLogLevel}, defaults to {@link DiagLogLevel} if the log level does not exist or undefined if the input is undefined.
* @param value
*/
export function stringToLogLevel(
export function diagLogLevelFromString(
value: string | undefined
): DiagLogLevel | undefined {
if (value == null) {
Expand Down
26 changes: 13 additions & 13 deletions packages/opentelemetry-core/test/common/utils/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { stringToLogLevel } from '../../../src';
import { diagLogLevelFromString } from '../../../src';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { diag, DiagLogLevel } from '@opentelemetry/api';
Expand All @@ -24,31 +24,31 @@ describe('stringToLogLevel', function () {
});

it('should map valid string to log level', function () {
assert.strictEqual(stringToLogLevel('ALL'), DiagLogLevel.ALL);
assert.strictEqual(stringToLogLevel('VERBOSE'), DiagLogLevel.VERBOSE);
assert.strictEqual(stringToLogLevel('DEBUG'), DiagLogLevel.DEBUG);
assert.strictEqual(stringToLogLevel('INFO'), DiagLogLevel.INFO);
assert.strictEqual(stringToLogLevel('WARN'), DiagLogLevel.WARN);
assert.strictEqual(stringToLogLevel('ERROR'), DiagLogLevel.ERROR);
assert.strictEqual(stringToLogLevel('NONE'), DiagLogLevel.NONE);
assert.strictEqual(diagLogLevelFromString('ALL'), DiagLogLevel.ALL);
assert.strictEqual(diagLogLevelFromString('VERBOSE'), DiagLogLevel.VERBOSE);
assert.strictEqual(diagLogLevelFromString('DEBUG'), DiagLogLevel.DEBUG);
assert.strictEqual(diagLogLevelFromString('INFO'), DiagLogLevel.INFO);
assert.strictEqual(diagLogLevelFromString('WARN'), DiagLogLevel.WARN);
assert.strictEqual(diagLogLevelFromString('ERROR'), DiagLogLevel.ERROR);
assert.strictEqual(diagLogLevelFromString('NONE'), DiagLogLevel.NONE);
});

it('should ignore casing when resolving', function () {
assert.strictEqual(stringToLogLevel('error'), DiagLogLevel.ERROR);
assert.strictEqual(stringToLogLevel('eRRoR'), DiagLogLevel.ERROR);
assert.strictEqual(diagLogLevelFromString('error'), DiagLogLevel.ERROR);
assert.strictEqual(diagLogLevelFromString('eRRoR'), DiagLogLevel.ERROR);
});

it('should return undefined on undefined input', function () {
assert.strictEqual(stringToLogLevel(undefined), undefined);
assert.strictEqual(diagLogLevelFromString(undefined), undefined);
});

it('should return undefined on null input', function () {
assert.strictEqual(stringToLogLevel(undefined), undefined);
assert.strictEqual(diagLogLevelFromString(undefined), undefined);
});

it('should fall back to INFO and warn on input that cannot be mapped', function () {
const warnStub = sinon.stub(diag, 'warn');
assert.strictEqual(stringToLogLevel('does not exist'), DiagLogLevel.INFO);
assert.strictEqual(diagLogLevelFromString('does not exist'), DiagLogLevel.INFO);
sinon.assert.calledOnceWithMatch(warnStub, 'Unknown');
});
});

0 comments on commit 3a0a6c3

Please sign in to comment.