-
Notifications
You must be signed in to change notification settings - Fork 3
/
Logger.ts
71 lines (60 loc) · 2.75 KB
/
Logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Logging
*/
import { Logger, LogLevel } from "@itwin/core-bentley";
import type { ILogger as IOidcClientLogger } from "oidc-client-ts";
import { Log as OidcClientLog } from "oidc-client-ts";
import { BrowserAuthorizationLoggerCategory } from "./LoggerCategory";
/**
* Utility to forward oidc-client logs to the Bentley logger
* Because the logger used by the oidc-client library is static, we can't tie specific UserManager instances to different logging categories.
* Thus, the best we can do is tie all logs originating from the library to a single logging category (derived from the name of this class).
* @beta
*/
export class BrowserAuthorizationLogger implements IOidcClientLogger {
private static initialized: boolean = false;
private constructor() {
}
public debug(message?: any, ...optionalParams: any[]): void {
Logger.logTrace(BrowserAuthorizationLoggerCategory.Authorization, message, () => optionalParams);
}
public info(message?: any, ...optionalParams: any[]): void {
Logger.logInfo(BrowserAuthorizationLoggerCategory.Authorization, message, () => optionalParams);
}
public warn(message?: any, ...optionalParams: any[]): void {
Logger.logWarning(BrowserAuthorizationLoggerCategory.Authorization, message, () => optionalParams);
}
public error(message?: any, ...optionalParams: any[]): void {
Logger.logError(BrowserAuthorizationLoggerCategory.Authorization, message, () => optionalParams);
}
protected static getLogLevel(loggerCategory: string): number {
const logLevel: LogLevel | undefined = Logger.getLevel(loggerCategory);
switch (logLevel) {
case LogLevel.Error:
return OidcClientLog.ERROR;
case LogLevel.Warning:
return OidcClientLog.WARN;
case LogLevel.Info:
return OidcClientLog.INFO;
case LogLevel.Trace:
return OidcClientLog.DEBUG;
case LogLevel.None:
return OidcClientLog.NONE;
default:
return OidcClientLog.NONE;
}
}
/** Initializes forwarding of OidcClient logs to the Bentley Logger */
public static initializeLogger() {
const logLevel = BrowserAuthorizationLogger.getLogLevel(BrowserAuthorizationLoggerCategory.Authorization);
if (!BrowserAuthorizationLogger.initialized) {
OidcClientLog.setLogger(new BrowserAuthorizationLogger());
}
OidcClientLog.setLevel(logLevel);
BrowserAuthorizationLogger.initialized = true;
}
}