Skip to content

Commit e954193

Browse files
committed
fixup! feat(core): add stringToLogLevel utility
1 parent abece9b commit e954193

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
110110
* feat(sdk-trace-base): add stack trace warning to debug instrumentation [#5363](https://github.com/open-telemetry/opentelemetry-js/pull/5363) @neilfordyce
111111
* feat(core): add more scalable replacements for getEnv(), getEnvWithoutDefaults() [#5443](https://github.com/open-telemetry/opentelemetry-js/pull/5443) @pichlermarc
112112
* refactor(exporter-jaeger): migrate away from getEnv() [#5464](https://github.com/open-telemetry/opentelemetry-js/pull/5464) @pichlermarc
113+
* feat(core): add stringToLogLevel utility [#????](https://github.com/open-telemetry/opentelemetry-js/pull/????) @pichlermarc
113114

114115
### :bug: (Bug Fix)
115116

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ All notable changes to experimental packages in this project will be documented
3939
* refactor(exporter-prometheus): remove unnecessary isNaN() check [#5377](https://github.com/open-telemetry/opentelemetry-js/pull/5377) @cjihrig
4040
* refactor(sdk-node): move code to auto-instantiate propagators into utils [#5355](https://github.com/open-telemetry/opentelemetry-js/pull/5355) @pichlermarc
4141
* chore: unpin `@opentelemetry/semantic-conventions` dep to allow better de-duplication in installs [#5439](https://github.com/open-telemetry/opentelemetry-js/pull/5439) @trentm
42+
* refactor(sdk-node): migrate away from getEnv() [#????](https://github.com/open-telemetry/opentelemetry-js/pull/????) @pichlermarc
4243

4344
## 0.57.0
4445

packages/opentelemetry-core/src/utils/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function stringToLogLevel(
4242

4343
if (resolvedLogLevel == null) {
4444
diag.warn(
45-
`Cannot resolve log level ${inspect(value)}, expected one of ${inspect(Object.keys(logLevelMap))}, using default`
45+
`Unknown log level ${inspect(value)}, expected one of ${inspect(Object.keys(logLevelMap))}, using default`
4646
);
4747
return DiagLogLevel.INFO;
4848
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { stringToLogLevel } from '../../../src';
17+
import * as assert from 'assert';
18+
import * as sinon from 'sinon';
19+
import { diag, DiagLogLevel } from '@opentelemetry/api';
20+
21+
describe('stringToLogLevel', function () {
22+
afterEach(function () {
23+
sinon.restore();
24+
});
25+
26+
it('should map valid string to log level', function () {
27+
assert.strictEqual(stringToLogLevel('ALL'), DiagLogLevel.ALL);
28+
assert.strictEqual(stringToLogLevel('VERBOSE'), DiagLogLevel.VERBOSE);
29+
assert.strictEqual(stringToLogLevel('DEBUG'), DiagLogLevel.DEBUG);
30+
assert.strictEqual(stringToLogLevel('INFO'), DiagLogLevel.INFO);
31+
assert.strictEqual(stringToLogLevel('WARN'), DiagLogLevel.WARN);
32+
assert.strictEqual(stringToLogLevel('ERROR'), DiagLogLevel.ERROR);
33+
assert.strictEqual(stringToLogLevel('NONE'), DiagLogLevel.NONE);
34+
});
35+
36+
it('should ignore casing when resolving', function () {
37+
assert.strictEqual(stringToLogLevel('error'), DiagLogLevel.ERROR);
38+
assert.strictEqual(stringToLogLevel('eRRoR'), DiagLogLevel.ERROR);
39+
});
40+
41+
it('should return undefined on undefined input', function () {
42+
assert.strictEqual(stringToLogLevel(undefined), undefined);
43+
});
44+
45+
it('should return undefined on null input', function () {
46+
assert.strictEqual(stringToLogLevel(undefined), undefined);
47+
});
48+
49+
it('should fall back to INFO and warn on input that cannot be mapped', function () {
50+
const warnStub = sinon.stub(diag, 'warn');
51+
assert.strictEqual(stringToLogLevel('does not exist'), DiagLogLevel.INFO);
52+
sinon.assert.calledOnceWithMatch(warnStub, 'Unknown');
53+
});
54+
});

0 commit comments

Comments
 (0)