Skip to content

Commit

Permalink
refactor: use switch case instead of convoluded function
Browse files Browse the repository at this point in the history
Signed-off-by: Naseem <naseem@transit.app>
  • Loading branch information
Naseem committed Jul 24, 2020
1 parent 92a04ee commit f08ca1c
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/opentelemetry-core/src/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { LogLevel, LogLevelString } from '../common/types';
import { LogLevel } from '../common/types';

export type ENVIRONMENT_MAP = { [key: string]: string | number };

Expand Down Expand Up @@ -65,24 +65,37 @@ function parseNumber(
}

/**
* Parses a LogLevelString as a LogLevel
* Environmentally sets log level if valid log level string is provided
* @param key
* @param environment
* @param values
*/
function parseLogLevel(
function setLogLevelFromEnv(
key: keyof ENVIRONMENT,
environment: ENVIRONMENT_MAP | ENVIRONMENT,
values: ENVIRONMENT_MAP
) {
const value = values[key];
const logLevelStrings = Object.keys(LogLevel).filter(
k => isNaN(Number(k)) === true
) as LogLevelString[];
const regex = new RegExp(logLevelStrings.join('|'), 'i');
if (value && typeof value === 'string' && regex.test(value)) {
const logLevelString = value.toUpperCase() as LogLevelString;
environment[key] = LogLevel[logLevelString];
switch (typeof value === 'string' ? value.toUpperCase() : value) {
case 'DEBUG':
environment[key] = LogLevel.DEBUG;
break;

case 'INFO':
environment[key] = LogLevel.INFO;
break;

case 'WARN':
environment[key] = LogLevel.WARN;
break;

case 'ERROR':
environment[key] = LogLevel.ERROR;
break;

default:
// do nothing
break;
}
}

Expand All @@ -101,7 +114,7 @@ export function parseEnvironment(values: ENVIRONMENT_MAP): ENVIRONMENT {
break;

case 'OTEL_LOG_LEVEL':
parseLogLevel(key, environment, values);
setLogLevelFromEnv(key, environment, values);
break;

default:
Expand Down

0 comments on commit f08ca1c

Please sign in to comment.