Skip to content

Commit

Permalink
feat(core): add stringToLogLevel utility
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Feb 13, 2025
1 parent cf445a5 commit abece9b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +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';
import { _export } from './internal/exporter';
export const internal = {
_export,
Expand Down
51 changes: 51 additions & 0 deletions packages/opentelemetry-core/src/utils/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed 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
*
* https://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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { diag, DiagLogLevel } from '@opentelemetry/api';
import { inspect } from 'util';

const logLevelMap: { [key: string]: DiagLogLevel } = {
ALL: DiagLogLevel.ALL,
VERBOSE: DiagLogLevel.VERBOSE,
DEBUG: DiagLogLevel.DEBUG,
INFO: DiagLogLevel.INFO,
WARN: DiagLogLevel.WARN,
ERROR: DiagLogLevel.ERROR,
NONE: DiagLogLevel.NONE,
};

/**
* 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(
value: string | undefined

Check warning on line 34 in packages/opentelemetry-core/src/utils/configuration.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L34 was not covered by tests
): DiagLogLevel | undefined {
if (value == null) {

Check warning on line 36 in packages/opentelemetry-core/src/utils/configuration.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L36 was not covered by tests
// don't fall back to default - no value set has different semantics for ús than an incorrect value (do not set vs. fall back to default)
return undefined;

Check warning on line 38 in packages/opentelemetry-core/src/utils/configuration.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L38 was not covered by tests
}

const resolvedLogLevel = logLevelMap[value.toUpperCase()];

Check warning on line 41 in packages/opentelemetry-core/src/utils/configuration.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L41 was not covered by tests

if (resolvedLogLevel == null) {
diag.warn(

Check warning on line 44 in packages/opentelemetry-core/src/utils/configuration.ts

View check run for this annotation

Codecov / codecov/patch

packages/opentelemetry-core/src/utils/configuration.ts#L43-L44

Added lines #L43 - L44 were not covered by tests
`Cannot resolve log level ${inspect(value)}, expected one of ${inspect(Object.keys(logLevelMap))}, using default`
);
return DiagLogLevel.INFO;

Check warning on line 47 in packages/opentelemetry-core/src/utils/configuration.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L47 was not covered by tests
}

return resolvedLogLevel;

Check warning on line 50 in packages/opentelemetry-core/src/utils/configuration.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L50 was not covered by tests
}

0 comments on commit abece9b

Please sign in to comment.