Skip to content

Commit

Permalink
feat: add OTEL_LOG_LEVEL env var
Browse files Browse the repository at this point in the history
User can now control the log level by means of env var.

Signed-off-by: Naseem <naseem@transit.app>
  • Loading branch information
Naseem committed Apr 18, 2020
1 parent b7dcb8c commit 12926be
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
8 changes: 6 additions & 2 deletions packages/opentelemetry-core/src/common/ConsoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
*/

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

export class ConsoleLogger implements Logger {
constructor(level: LogLevel = LogLevel.INFO) {
constructor(
level: LogLevel = LogLevel[
(process.env.OTEL_LOG_LEVEL as LogLevelString) || 'INFO'
]
) {
if (level >= LogLevel.DEBUG) {
this.debug = (...args) => {
console.debug(...args);
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-core/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export enum LogLevel {
DEBUG,
}

export type LogLevelString = keyof typeof LogLevel;

/**
* This interface defines a fallback to read a timeOrigin when it is not available on performance.timeOrigin,
* this happens for example on Safari Mac
Expand Down
14 changes: 14 additions & 0 deletions packages/opentelemetry-core/test/common/ConsoleLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('ConsoleLogger', () => {
console.info = origInfo;
console.warn = origWarn;
console.error = origError;
delete process.env.OTEL_LOG_LEVEL;
});

describe('constructor', () => {
Expand All @@ -67,6 +68,19 @@ describe('ConsoleLogger', () => {
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']);
});

it('should log with debug', () => {
const consoleLogger = new ConsoleLogger(LogLevel.DEBUG);
consoleLogger.error('error called');
Expand Down
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 } from '@opentelemetry/core';
import { LogLevel, LogLevelString } 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.INFO,
logLevel: LogLevel[(process.env.OTEL_LOG_LEVEL as LogLevelString) || 'INFO'],
};

/** 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 } from '@opentelemetry/core';
import { ALWAYS_SAMPLER, LogLevel, LogLevelString } 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.INFO,
logLevel: LogLevel[(process.env.OTEL_LOG_LEVEL as LogLevelString) || 'INFO'],
sampler: ALWAYS_SAMPLER,
traceParams: {
numberOfAttributesPerSpan: DEFAULT_MAX_ATTRIBUTES_PER_SPAN,
Expand Down

0 comments on commit 12926be

Please sign in to comment.