Skip to content

Commit

Permalink
fix: create platform specific implementations to get log level
Browse files Browse the repository at this point in the history
Signed-off-by: Naseem <[email protected]>
  • Loading branch information
Naseem committed May 17, 2020
1 parent 4d450e9 commit c4516f7
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 24 deletions.
9 changes: 3 additions & 6 deletions packages/opentelemetry-core/src/common/ConsoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
*/

import { Logger } from '@opentelemetry/api';
import { LogLevel, LogLevelString } from './types';
import { LogLevel } from './types';
import { getLogLevel } from '../platform';

export class ConsoleLogger implements Logger {
constructor(
level: LogLevel = LogLevel[
(process.env.OTEL_LOG_LEVEL as LogLevelString) || 'INFO'
]
) {
constructor(level: LogLevel = getLogLevel()) {
if (level >= LogLevel.DEBUG) {
this.debug = (...args) => {
console.debug(...args);
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/platform/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export * from './BasePlugin';
export * from './hex-to-base64';
export * from './id';
export * from './log-level';
export * from './performance';
export * from './sdk-info';
export * from './timer-util';
24 changes: 24 additions & 0 deletions packages/opentelemetry-core/src/platform/browser/log-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2020, 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.
*/

/**
* @returns the default log level for opentelemetry.
*/
import { LogLevel } from '../../common/types';

export function getLogLevel(): LogLevel {
return LogLevel.INFO;
}
3 changes: 2 additions & 1 deletion packages/opentelemetry-core/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

export * from './BasePlugin';
export * from './hex-to-base64';
export * from './id';
export * from './log-level';
export * from './performance';
export * from './sdk-info';
export * from './timer-util';
export * from './hex-to-base64';
26 changes: 26 additions & 0 deletions packages/opentelemetry-core/src/platform/node/log-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2020, 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.
*/

/**
* Returns an environmentally set log level if set.
* Defaults to INFO.
* @returns the log level for opentelemetry.
*/
import { LogLevel, LogLevelString } from '../../common/types';

export function getLogLevel(): LogLevel {
return LogLevel[(process.env.OTEL_LOG_LEVEL as LogLevelString) ?? 'INFO'];
}
14 changes: 1 addition & 13 deletions packages/opentelemetry-core/test/common/ConsoleLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe('ConsoleLogger', () => {
console.info = origInfo;
console.warn = origWarn;
console.error = origError;
delete process.env.OTEL_LOG_LEVEL;
});

describe('constructor', () => {
Expand All @@ -66,19 +65,8 @@ describe('ConsoleLogger', () => {
assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']);
consoleLogger.info('info called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']);
});

it('should log with environmentally set level', () => {
process.env.OTEL_LOG_LEVEL = 'DEBUG';
const consoleLogger = new ConsoleLogger();
consoleLogger.error('error called');
assert.deepStrictEqual(errorCalledArgs, ['error called']);
consoleLogger.warn('warn called %s', 'param1');
assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']);
consoleLogger.info('info called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']);
consoleLogger.debug('debug called %s', 'param1');
assert.deepStrictEqual(debugCalledArgs, ['debug called %s', 'param1']);
assert.strictEqual(debugCalledArgs, undefined);
});

it('should log with debug', () => {
Expand Down
73 changes: 73 additions & 0 deletions packages/opentelemetry-core/test/platform/log-level.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*!
* Copyright 2019, 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 * as assert from 'assert';
import { ConsoleLogger } from '../../src/common/ConsoleLogger';

describe('Log level', () => {
const origDebug = console.debug;
const origInfo = console.info;
const origWarn = console.warn;
const origError = console.error;
let debugCalledArgs: unknown;
let infoCalledArgs: unknown;
let warnCalledArgs: unknown;
let errorCalledArgs: unknown;

beforeEach(() => {
// mock
console.debug = (...args: unknown[]) => {
debugCalledArgs = args;
};
console.info = (...args: unknown[]) => {
infoCalledArgs = args;
};
console.warn = (...args: unknown[]) => {
warnCalledArgs = args;
};
console.error = (...args: unknown[]) => {
errorCalledArgs = args;
};
});

afterEach(() => {
// restore
debugCalledArgs = null;
infoCalledArgs = null;
warnCalledArgs = null;
errorCalledArgs = null;
console.debug = origDebug;
console.info = origInfo;
console.warn = origWarn;
console.error = origError;
if (typeof process !== 'undefined' && process.release.name === 'node')
delete process.env.OTEL_LOG_LEVEL;
});

if (typeof process !== 'undefined' && process.release.name === 'node')
it('should log with environmentally set level in Node', () => {
process.env.OTEL_LOG_LEVEL = 'WARN';
const consoleLogger = new ConsoleLogger();
consoleLogger.error('error called');
assert.deepStrictEqual(errorCalledArgs, ['error called']);
consoleLogger.warn('warn called %s', 'param1');
assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']);
consoleLogger.info('info called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, undefined);
consoleLogger.debug('debug called %s', 'param1');
assert.deepStrictEqual(debugCalledArgs, undefined);
});
});
4 changes: 2 additions & 2 deletions packages/opentelemetry-metrics/src/types.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 '@opentelemetry/core';
import { LogLevel, getLogLevel } from '@opentelemetry/core';
import { Logger, ValueType } from '@opentelemetry/api';
import { MetricExporter } from './export/types';
import { Resource } from '@opentelemetry/resources';
Expand Down Expand Up @@ -76,7 +76,7 @@ export interface MeterConfig {

/** Default Meter configuration. */
export const DEFAULT_CONFIG = {
logLevel: LogLevel[(process.env.OTEL_LOG_LEVEL as LogLevelString) || 'INFO'],
logLevel: getLogLevel(),
};

/** The default metric creation options value. */
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-tracing/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { ALWAYS_SAMPLER, LogLevel, LogLevelString } from '@opentelemetry/core';
import { ALWAYS_SAMPLER, getLogLevel } from '@opentelemetry/core';

/** Default limit for Message events per span */
export const DEFAULT_MAX_EVENTS_PER_SPAN = 128;
Expand All @@ -31,7 +31,7 @@ export const DEFAULT_MAX_LINKS_PER_SPAN = 32;
*/
export const DEFAULT_CONFIG = {
defaultAttributes: {},
logLevel: LogLevel[(process.env.OTEL_LOG_LEVEL as LogLevelString) || 'INFO'],
logLevel: getLogLevel(),
sampler: ALWAYS_SAMPLER,
traceParams: {
numberOfAttributesPerSpan: DEFAULT_MAX_ATTRIBUTES_PER_SPAN,
Expand Down

0 comments on commit c4516f7

Please sign in to comment.