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 <[email protected]>
  • Loading branch information
Naseem committed Apr 18, 2020
1 parent b7dcb8c commit 609dfa3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 5 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,13 @@
*/

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

export class ConsoleLogger implements Logger {
constructor(level: LogLevel = LogLevel.INFO) {
constructor(
level: LogLevel = LogLevel[process.env.OTEL_LOG_LEVEL as LogLevelStrings] ||
LogLevel.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 LogLevelStrings = 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

0 comments on commit 609dfa3

Please sign in to comment.