配置日志级别和文件

Node.js 驱动程序支持通过两种类型的记录器来跟踪活动:

  • 浏览器记录器,可将日志存储在浏览器的内存缓冲区中。
  • Node logger, which by default, stores logs in a snowflake.log file and displays them in the console.

您可以使用以下代码从浏览器记录器切换到节点记录器。此示例切换到了节点记录器,并向控制台发送消息。

Logger.setInstance(new NodeLogger({ logFilePath: 'STDOUT'}));

支持的日志级别

Node.js 驱动程序日志级别:

  • OFF
  • ERROR
  • WARNING
  • INFO
  • DEBUG
  • TRACE

配置默认日志记录行为

You can configure standard logging by calling snowflake.configure, similar to the following:

import snowflake from 'snowflake-sdk';

snowflake.configure({
  logLevel: 'INFO',
  logFilePath: '/some/path/log_file.log',
  additionalLogToConsole: false
});

其中:

  • logLevel is the desired logging level.
  • logFilePath is the location of the log file or STDOUT for console output.
  • additionalLogToConsole is a Boolean value that indicates whether to send log messages also to the console when a logFilePath is specified. Default: true.

调试代码时使用 Easy Logging 功能

When debugging an application, increasing the log level can provide more granular information about what the application is doing. The Easy Logging feature simplifies debugging by letting you change the log level and the log file destination using a configuration file (default: sf_client_config.json).

通常只有在调试应用程序时才会更改日志级别。

This configuration file uses JSON to define the log_level and log_path logging parameters, as follows:

{
  "common": {
    "log_level": "INFO",
    "log_path": "/some-path/some-directory"
  }
}

其中:

  • log_level is the desired logging level.
  • log_path is the location to store the log files. The driver automatically creates a nodejs sub-directory in the specified log_path. For example, if you set log_path to /Users/me/logs, the drivers creates the /Users/me/logs/nodejs directory and stores the logs there.

驱动程序会按以下顺序查找配置文件的位置:

  • clientConfigFile connection parameter, containing the full path to the configuration file, such as the following:

    import snowflake from 'snowflake-sdk';
    
    const connection = snowflake.createConnection({
      account: account,
      username: user,
      password: password,
      application: application,
      clientConfigFile: '/some/path/client_config.json'
    });
  • SF_CLIENT_CONFIG_FILE environment variable, containing the full path to the configuration file (for example, export SF_CLIENT_CONFIG_FILE=/some_path/some-directory/client_config.json).

  • Node.js driver installation directory, where the file must be named sf_client_config.json.

  • User’s home directory, where the file must be named sf_client_config.json.

Note

To enhance security, the driver requires the logging configuration file on Unix-style systems to limit file permissions to allow only the file owner to modify the files (such as chmod 0600 or chmod 0644).

为尽量减少搜索配置文件的次数,驱动程序:

  • 仅为第一次连接读取配置文件。
  • for the first connection using the clientConfigFile parameter.