Skip to content

Commit 62f19b0

Browse files
MSNevdyladan
authored andcommitted
feat(diag-logger): part 2 - breaking changes - remove api.Logger, api… (open-telemetry#1925)
1 parent 9bc2660 commit 62f19b0

File tree

11 files changed

+217
-417
lines changed

11 files changed

+217
-417
lines changed

api/src/api/diag.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ export class DiagAPI implements DiagLogger {
9999
return _logger;
100100
};
101101

102-
self.setLogger = (logger: DiagLogger): DiagLogger => {
102+
self.setLogger = (logger?: DiagLogger): DiagLogger => {
103103
const prevLogger = _logger;
104-
if (prevLogger !== logger && logger !== self) {
104+
if (!logger || logger !== self) {
105105
// Simple special case to avoid any possible infinite recursion on the logging functions
106106
_logger = logger || createNoopDiagLogger();
107107
_filteredLogger = createLogLevelDiagLogger(_logLevel, _logger);
@@ -133,10 +133,10 @@ export class DiagAPI implements DiagLogger {
133133

134134
/**
135135
* Set the DiagLogger instance
136-
* @param logger - The DiagLogger instance to set as the default logger
136+
* @param logger - [Optional] The DiagLogger instance to set as the default logger, if not provided it will set it back as a noop
137137
* @returns The previously registered DiagLogger
138138
*/
139-
public setLogger!: (logger: DiagLogger) => DiagLogger;
139+
public setLogger!: (logger?: DiagLogger) => DiagLogger;
140140

141141
/** Set the default maximum diagnostic logging level */
142142
public setLogLevel!: (maxLogLevel: DiagLogLevel) => void;
@@ -146,8 +146,5 @@ export class DiagAPI implements DiagLogger {
146146
public debug!: DiagLogFunction;
147147
public info!: DiagLogFunction;
148148
public warn!: DiagLogFunction;
149-
public startupInfo!: DiagLogFunction;
150149
public error!: DiagLogFunction;
151-
public critical!: DiagLogFunction;
152-
public terminal!: DiagLogFunction;
153150
}

api/src/common/Logger.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

api/src/diag/consoleLogger.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,17 @@
1717
import { DiagLogger, DiagLogFunction } from './logger';
1818

1919
const consoleMap: { n: keyof DiagLogger; c: keyof Console }[] = [
20-
{ n: 'terminal', c: 'error' },
21-
{ n: 'critical', c: 'error' },
2220
{ n: 'error', c: 'error' },
2321
{ n: 'warn', c: 'warn' },
2422
{ n: 'info', c: 'info' },
2523
{ n: 'debug', c: 'debug' },
2624
{ n: 'verbose', c: 'trace' },
27-
{ n: 'startupInfo', c: 'info' },
2825
];
2926

3027
/**
3128
* A simple Immutable Console based diagnostic logger which will output any messages to the Console.
3229
* If you want to limit the amount of logging to a specific level or lower use the
33-
* {@link diagLogLevelFilter}
30+
* {@link createLogLevelDiagLogger}
3431
*/
3532
export class DiagConsoleLogger implements DiagLogger {
3633
constructor() {
@@ -58,28 +55,9 @@ export class DiagConsoleLogger implements DiagLogger {
5855
}
5956
}
6057

61-
/**
62-
* Log a terminal situation that would cause the API to completely fail to initialize,
63-
* if this type of message is logged functionality of the API is not expected to be functional.
64-
*/
65-
public terminal!: DiagLogFunction;
66-
67-
/**
68-
* Log a critical error that NEEDS to be addressed, functionality of the component that emits
69-
* this log detail may non-functional. While the overall API may be.
70-
*/
71-
public critical!: DiagLogFunction;
72-
7358
/** Log an error scenario that was not expected and caused the requested operation to fail. */
7459
public error!: DiagLogFunction;
7560

76-
/**
77-
* Logs a general informational message that is used for logging component startup and version
78-
* information without causing additional general informational messages when the logging level
79-
* is set to DiagLogLevel.WARN or lower.
80-
*/
81-
public startupInfo!: DiagLogFunction;
82-
8361
/**
8462
* Log a warning scenario to inform the developer of an issues that should be investigated.
8563
* The requested operation may or may not have succeeded or completed.

api/src/diag/logLevel.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import { DiagAPI } from '../api/diag';
18-
import { Logger } from '../common/Logger';
1918
import { DiagLogger, DiagLogFunction, createNoopDiagLogger } from './logger';
2019

2120
/**
@@ -27,24 +26,9 @@ export enum DiagLogLevel {
2726
/** Diagnostic Logging level setting to disable all logging (except and forced logs) */
2827
NONE = 0,
2928

30-
/**
31-
* Identifies a terminal situation that would cause the API to completely fail to initialize,
32-
* if this type of error is logged functionality of the API is not expected to be functional.
33-
*/
34-
TERMINAL = 10,
35-
36-
/**
37-
* Identifies a critical error that needs to be addressed, functionality of the component
38-
* that emits this log detail may non-functional.
39-
*/
40-
CRITICAL = 20,
41-
4229
/** Identifies an error scenario */
4330
ERROR = 30,
4431

45-
/** Identifies startup and failure (lower) scenarios */
46-
STARTUP = 40,
47-
4832
/** Identifies a warning scenario */
4933
WARN = 50,
5034

@@ -66,35 +50,17 @@ export enum DiagLogLevel {
6650

6751
/**
6852
* This is equivalent to:
69-
* type LogLevelString = 'NONE' | TERMINAL' | 'CRITICAL' | 'ERROR' | 'WARN' | 'INFO' | 'DEBUG' | 'VERBOSE' | 'ALL';
53+
* type LogLevelString = 'NONE' | 'ERROR' | 'WARN' | 'INFO' | 'DEBUG' | 'VERBOSE' | 'ALL';
7054
*/
7155
export type DiagLogLevelString = keyof typeof DiagLogLevel;
7256

73-
/**
74-
* Mapping from DiagLogger function name to Legacy Logger function used if
75-
* the logger instance doesn't have the DiagLogger function
76-
*/
77-
const fallbackLoggerFuncMap: { [n: string]: keyof Logger } = {
78-
terminal: 'error',
79-
critical: 'error',
80-
error: 'error',
81-
warn: 'warn',
82-
info: 'info',
83-
debug: 'debug',
84-
verbose: 'debug',
85-
startupInfo: 'info',
86-
};
87-
8857
/** Mapping from DiagLogger function name to logging level. */
8958
const levelMap: { n: keyof DiagLogger; l: DiagLogLevel }[] = [
90-
{ n: 'terminal', l: DiagLogLevel.TERMINAL },
91-
{ n: 'critical', l: DiagLogLevel.CRITICAL },
9259
{ n: 'error', l: DiagLogLevel.ERROR },
9360
{ n: 'warn', l: DiagLogLevel.WARN },
9461
{ n: 'info', l: DiagLogLevel.INFO },
9562
{ n: 'debug', l: DiagLogLevel.DEBUG },
9663
{ n: 'verbose', l: DiagLogLevel.VERBOSE },
97-
{ n: 'startupInfo', l: DiagLogLevel.ERROR },
9864
];
9965

10066
/**
@@ -138,8 +104,7 @@ export function createLogLevelDiagLogger(
138104
if (maxLevel >= theLevel) {
139105
return function () {
140106
const orgArguments = arguments as unknown;
141-
const theFunc =
142-
theLogger[funcName] || theLogger[fallbackLoggerFuncMap[funcName]];
107+
const theFunc = theLogger[funcName];
143108
if (theFunc && typeof theFunc === 'function') {
144109
return theFunc.apply(
145110
logger,

api/src/diag/logger.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Logger } from '../common/Logger';
18-
19-
/**
20-
* Defines a type which can be used for as a parameter without breaking backward
21-
* compatibility. The {@link Logger} reference will be removed with the removal
22-
* of the Logger definition, this can be used as a replacement for functions
23-
* that are currently passing Logger references during migration to minimize
24-
* breaks that will occur with the removal of the Logger interface.
25-
*/
26-
export type OptionalDiagLogger = Logger | DiagLogger | null | undefined;
27-
2817
export type DiagLogFunction = (message: string, ...args: unknown[]) => void;
2918

3019
/**
3120
* Defines an internal diagnostic logger interface which is used to log internal diagnostic
3221
* messages, you can set the default diagnostic logger via the {@link DiagAPI} setLogger function.
3322
* API provided implementations include :-
3423
* - a No-Op {@link createNoopDiagLogger}
35-
* - a {@link DiagLogLevel} filtering wrapper {@link diagLogLevelFilter}
24+
* - a {@link DiagLogLevel} filtering wrapper {@link createLogLevelDiagLogger}
3625
* - a general Console {@link DiagConsoleLogger} version.
3726
*/
3827
export interface DiagLogger {
39-
/**
40-
* Log a terminal situation that would cause the API to completely fail to initialize,
41-
* if this type of message is logged functionality of the API is not expected to be functional.
42-
*/
43-
terminal: DiagLogFunction;
44-
45-
/**
46-
* Log a critical error that NEEDS to be addressed, functionality of the component that emits
47-
* this log detail may be limited or non-functional depending on when this message is emitted.
48-
* Unlike terminal message, it is expected that the overall API may still be functional, again
49-
* depending on what component and when this message is emitted.
50-
*/
51-
critical: DiagLogFunction;
52-
5328
/** Log an error scenario that was not expected and caused the requested operation to fail. */
5429
error: DiagLogFunction;
5530

56-
/**
57-
* Logs a general informational message that is used for logging component startup and version
58-
* information without causing additional general informational messages when the logging level
59-
* is set to DiagLogLevel.WARN or lower.
60-
*/
61-
startupInfo: DiagLogFunction;
62-
6331
/**
6432
* Log a warning scenario to inform the developer of an issues that should be investigated.
6533
* The requested operation may or may not have succeeded or completed.
@@ -97,10 +65,7 @@ export const diagLoggerFunctions: Array<keyof DiagLogger> = [
9765
'debug',
9866
'info',
9967
'warn',
100-
'startupInfo',
10168
'error',
102-
'critical',
103-
'terminal',
10469
];
10570

10671
function noopLogFunction() {}

api/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
export * from './common/Exception';
18-
export * from './common/Logger';
1918
export * from './common/Time';
2019
export * from './context/context';
2120
export * from './context/propagation/TextMapPropagator';
@@ -25,7 +24,6 @@ export * from './trace/attributes';
2524
export * from './trace/Event';
2625
export * from './trace/link_context';
2726
export * from './trace/link';
28-
export * from './trace/NoopLogger';
2927
export * from './trace/NoopTracer';
3028
export * from './trace/NoopTracerProvider';
3129
export * from './trace/ProxyTracer';

api/src/trace/NoopLogger.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

api/test/api/api.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ import api, {
3131
ROOT_CONTEXT,
3232
defaultTextMapSetter,
3333
defaultTextMapGetter,
34+
diag,
35+
diagLoggerFunctions,
3436
} from '../../src';
37+
import { DiagAPI } from '../../src/api/diag';
38+
import { _global } from '../../src/api/global-utils';
3539
import { NoopSpan } from '../../src/trace/NoopSpan';
3640

3741
describe('API', () => {
@@ -177,4 +181,42 @@ describe('API', () => {
177181
});
178182
});
179183
});
184+
185+
describe('Global diag', () => {
186+
it('initialization', () => {
187+
const inst = DiagAPI.instance();
188+
189+
assert.deepStrictEqual(diag, inst);
190+
});
191+
192+
diagLoggerFunctions.forEach(fName => {
193+
it(`no argument logger ${fName} message doesn't throw`, () => {
194+
diag.setLogger();
195+
assert.doesNotThrow(() => {
196+
diag[fName](`${fName} message`);
197+
});
198+
});
199+
200+
it(`null logger ${fName} message doesn't throw`, () => {
201+
diag.setLogger(null as any);
202+
assert.doesNotThrow(() => {
203+
diag[fName](`${fName} message`);
204+
});
205+
});
206+
207+
it(`undefined logger ${fName} message doesn't throw`, () => {
208+
diag.setLogger(undefined as any);
209+
assert.doesNotThrow(() => {
210+
diag[fName](`${fName} message`);
211+
});
212+
});
213+
214+
it(`empty logger ${fName} message doesn't throw`, () => {
215+
diag.setLogger({} as any);
216+
assert.doesNotThrow(() => {
217+
diag[fName](`${fName} message`);
218+
});
219+
});
220+
});
221+
});
180222
});

api/test/diag/consoleLogger.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ const consoleFuncs: Array<keyof Console> = [
2828
];
2929

3030
const expectedConsoleMap: { [n: string]: keyof Console } = {
31-
terminal: 'error',
32-
critical: 'error',
3331
error: 'error',
3432
warn: 'warn',
3533
info: 'info',
3634
debug: 'debug',
3735
verbose: 'trace',
38-
startupInfo: 'info',
3936
};
4037

4138
describe('DiagConsoleLogger', () => {

0 commit comments

Comments
 (0)