Skip to content

Commit f4ed3f2

Browse files
authored
feat(sdk): add console logger (open-telemetry#203)
* feat: add console logger * fix: review comments
1 parent 5e1c04b commit f4ed3f2

File tree

6 files changed

+202
-2
lines changed

6 files changed

+202
-2
lines changed

packages/opentelemetry-basic-tracer/src/BasicTracer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
isValid,
2424
randomSpanId,
2525
NoRecordingSpan,
26-
NoopLogger,
26+
ConsoleLogger,
2727
} from '@opentelemetry/core';
2828
import {
2929
BinaryFormat,
@@ -55,7 +55,7 @@ export class BasicTracer implements types.Tracer {
5555
this._httpTextFormat = config.httpTextFormat || new HttpTraceContext();
5656
this._sampler = config.sampler || ALWAYS_SAMPLER;
5757
this._scopeManager = config.scopeManager;
58-
this._logger = config.logger || new NoopLogger();
58+
this._logger = config.logger || new ConsoleLogger(config.logLevel);
5959
}
6060

6161
/**

packages/opentelemetry-basic-tracer/src/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
Logger,
2323
Sampler,
2424
} from '@opentelemetry/types';
25+
import { LogLevel } from '@opentelemetry/core';
2526

2627
/**
2728
* BasicTracerConfig provides an interface for configuring a Basic Tracer.
@@ -48,6 +49,9 @@ export interface BasicTracerConfig {
4849
*/
4950
logger?: Logger;
5051

52+
/** level of logger. */
53+
logLevel?: LogLevel;
54+
5155
/**
5256
* Sampler determines if a span should be recorded or should be a NoopSpan.
5357
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Logger } from '@opentelemetry/types';
18+
import { LogLevel } from './types';
19+
20+
export class ConsoleLogger implements Logger {
21+
constructor(level: LogLevel = LogLevel.DEBUG) {
22+
if (level >= LogLevel.DEBUG) {
23+
this.debug = (...args) => {
24+
console.debug(...args);
25+
};
26+
}
27+
if (level >= LogLevel.INFO) {
28+
this.info = (...args) => {
29+
console.info(...args);
30+
};
31+
}
32+
if (level >= LogLevel.WARN) {
33+
this.warn = (...args) => {
34+
console.warn(...args);
35+
};
36+
}
37+
if (level >= LogLevel.ERROR) {
38+
this.error = (...args) => {
39+
console.error(...args);
40+
};
41+
}
42+
}
43+
44+
debug(message: string, ...args: unknown[]) {}
45+
46+
error(message: string, ...args: unknown[]) {}
47+
48+
warn(message: string, ...args: unknown[]) {}
49+
50+
info(message: string, ...args: unknown[]) {}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/** Defines a log levels. */
18+
export enum LogLevel {
19+
ERROR,
20+
WARN,
21+
INFO,
22+
DEBUG,
23+
}

packages/opentelemetry-core/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
export * from './common/ConsoleLogger';
1718
export * from './common/NoopLogger';
19+
export * from './common/types';
1820
export * from './context/propagation/BinaryTraceContext';
1921
export * from './context/propagation/HttpTraceContext';
2022
export * from './platform';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Copyright 2019, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import { ConsoleLogger } from '../../src/common/ConsoleLogger';
19+
import { LogLevel } from '../../src/common/types';
20+
21+
describe('ConsoleLogger', () => {
22+
const origDebug = console.debug;
23+
const origInfo = console.info;
24+
const origWarn = console.warn;
25+
const origError = console.error;
26+
let debugCalledArgs: unknown;
27+
let infoCalledArgs: unknown;
28+
let warnCalledArgs: unknown;
29+
let errorCalledArgs: unknown;
30+
31+
beforeEach(() => {
32+
// mock
33+
console.debug = (...args: unknown[]) => {
34+
debugCalledArgs = args;
35+
};
36+
console.info = (...args: unknown[]) => {
37+
infoCalledArgs = args;
38+
};
39+
console.warn = (...args: unknown[]) => {
40+
warnCalledArgs = args;
41+
};
42+
console.error = (...args: unknown[]) => {
43+
errorCalledArgs = args;
44+
};
45+
});
46+
47+
afterEach(() => {
48+
// restore
49+
debugCalledArgs = null;
50+
infoCalledArgs = null;
51+
warnCalledArgs = null;
52+
errorCalledArgs = null;
53+
console.debug = origDebug;
54+
console.info = origInfo;
55+
console.warn = origWarn;
56+
console.error = origError;
57+
});
58+
59+
describe('constructor', () => {
60+
it('should log with default level', () => {
61+
const consoleLogger = new ConsoleLogger();
62+
consoleLogger.error('error called %s', 'param1');
63+
assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']);
64+
consoleLogger.warn('warn called %s', 'param1');
65+
assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']);
66+
consoleLogger.info('info called %s', 'param1');
67+
assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']);
68+
consoleLogger.debug('debug called %s', 'param1');
69+
assert.deepStrictEqual(debugCalledArgs, ['debug called %s', 'param1']);
70+
});
71+
72+
it('should log with debug', () => {
73+
const consoleLogger = new ConsoleLogger(LogLevel.DEBUG);
74+
consoleLogger.error('error called');
75+
assert.deepStrictEqual(errorCalledArgs, ['error called']);
76+
consoleLogger.warn('warn called %s', 'param1');
77+
assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']);
78+
consoleLogger.info('info called %s', 'param1');
79+
assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']);
80+
consoleLogger.debug('debug called %s', 'param1');
81+
assert.deepStrictEqual(debugCalledArgs, ['debug called %s', 'param1']);
82+
});
83+
84+
it('should log with info', () => {
85+
const consoleLogger = new ConsoleLogger(LogLevel.INFO);
86+
consoleLogger.error('error called %s', 'param1');
87+
assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']);
88+
consoleLogger.warn('warn called %s', 'param1');
89+
assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']);
90+
consoleLogger.info('info called %s', 'param1');
91+
assert.deepStrictEqual(infoCalledArgs, ['info called %s', 'param1']);
92+
consoleLogger.debug('debug called %s', 'param1');
93+
assert.strictEqual(debugCalledArgs, null);
94+
});
95+
96+
it('should log with warn', () => {
97+
const consoleLogger = new ConsoleLogger(LogLevel.WARN);
98+
consoleLogger.error('error called %s', 'param1');
99+
assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']);
100+
consoleLogger.warn('warn called %s', 'param1');
101+
assert.deepStrictEqual(warnCalledArgs, ['warn called %s', 'param1']);
102+
consoleLogger.info('info called %s', 'param1');
103+
assert.strictEqual(infoCalledArgs, null);
104+
consoleLogger.debug('debug called %s', 'param1');
105+
assert.strictEqual(debugCalledArgs, null);
106+
});
107+
108+
it('should log with error', () => {
109+
const consoleLogger = new ConsoleLogger(LogLevel.ERROR);
110+
consoleLogger.error('error called %s', 'param1');
111+
assert.deepStrictEqual(errorCalledArgs, ['error called %s', 'param1']);
112+
consoleLogger.warn('warn called %s', 'param1');
113+
assert.strictEqual(warnCalledArgs, null);
114+
consoleLogger.info('info called %s', 'param1');
115+
assert.strictEqual(infoCalledArgs, null);
116+
consoleLogger.debug('debug called %s', 'param1');
117+
assert.strictEqual(debugCalledArgs, null);
118+
});
119+
});
120+
});

0 commit comments

Comments
 (0)