MSAL.js 中的日志记录Logging in MSAL.js

Microsoft 身份验证库 (MSAL) 应用生成日志消息,这些消息可以用来诊断问题。The Microsoft Authentication Library (MSAL) apps generate log messages that can help diagnose issues. 应用可以通过数行代码配置日志记录,并可对详细程度以及是否记录个人和组织数据进行自定义控制。An app can configure logging with a few lines of code, and have custom control over the level of detail and whether or not personal and organizational data is logged. 建议创建 MSAL 日志记录回调,并提供一种方式来让用户在遇到身份验证问题时提交日志。We recommend you create an MSAL logging callback and provide a way for users to submit logs when they have authentication issues.

日志记录级别Logging levels

MSAL 提供多个日志记录详细级别:MSAL provides several levels of logging detail:

  • 错误:指示出现问题并已生成错误。Error: Indicates something has gone wrong and an error was generated. 用于调试并确定问题。Used for debugging and identifying problems.
  • 警告:不一定会出现错误或故障,只是为了诊断和指出问题。Warning: There hasn't necessarily been an error or failure, but are intended for diagnostics and pinpointing problems.
  • 信息:MSAL 将要记录的事件可为用户提供信息,不一定用于调试。Info: MSAL will log events intended for informational purposes not necessarily intended for debugging.
  • 详细:默认。Verbose: Default. MSAL 将记录库行为的完整详细信息。MSAL logs the full details of library behavior.

个人和组织数据Personal and organizational data

默认情况下,MSAL 记录器不捕获任何高度敏感的个人或组织数据。By default, the MSAL logger doesn't capture any highly sensitive personal or organizational data. 该库提供相关选项,允许你自行决定是否记录个人和组织数据。The library provides the option to enable logging personal and organizational data if you decide to do so.

以下各节将详细介绍应用程序的 MSAL 错误日志记录。The following sections provide more details about MSAL error logging for your application.

在 MSAL.js 中配置日志记录Configure logging in MSAL.js

可以在 MSAL.js (JavaScript) 中启用日志记录,方法是在配置过程中传递一个记录器对象,以便创建 UserAgentApplication 实例。Enable logging in MSAL.js (JavaScript) by passing a logger object during the configuration for creating a UserAgentApplication instance. 此记录器对象具有以下属性:This logger object has the following properties:

  • localCallback:一个回调实例,可以由开发人员提供,目的是以自定义方式使用并发布日志。localCallback: a Callback instance that can be provided by the developer to consume and publish logs in a custom manner. 根据所需要的重定向日志的方式,实现 localCallback 方法。Implement the localCallback method depending on how you want to redirect logs.
  • level(可选):可配置的日志级别。level (optional): the configurable log level. 支持的日志级别为:ErrorWarningInfoVerboseThe supported log levels are: Error, Warning, Info, and Verbose. 默认为 InfoThe default is Info.
  • piiLoggingEnabled(可选):如果设置为 true,则记录个人数据和组织数据。piiLoggingEnabled (optional): if set to true, logs personal and organizational data. 默认情况下,此项设置为 false,不允许应用程序记录个人数据。By default this is false so that your application doesn't log personal data. 个人数据日志不会写入到默认的输出(例如控制台、Logcat 或 NSLog)中。Personal data logs are never written to default outputs like Console, Logcat, or NSLog.
  • correlationId(可选):唯一标识符,用于映射请求与响应,以便进行调试。correlationId (optional): a unique identifier, used to map the request with the response for debugging purposes. 默认为 RFC4122 版本 4 guid(128 位)。Defaults to RFC4122 version 4 guid (128 bits).
function loggerCallback(logLevel, message, containsPii) {
   console.log(message);
}

var msalConfig = {
    auth: {
        clientId: "<Enter your client id>",
    },
    system: {
        logger: new Msal.Logger(
            loggerCallback , {
                level: Msal.LogLevel.Verbose,
                piiLoggingEnabled: false,
                correlationId: '1234'
            }
        )
    }
}

var UserAgentApplication = new Msal.UserAgentApplication(msalConfig);

后续步骤Next steps

有关更多代码示例,请参阅 Microsoft 标识平台代码示例For more code samples, refer to Microsoft identity platform code samples.