正在登录 n8n(Logging in n8n)#
日志记录是调试的重要功能。n8n 使用 winston 日志库。
🌐 Logging is an important feature for debugging. n8n uses the winston logging library.
日志流
n8n 自宿主企业版除了本文档中描述的日志选项外,还包括日志流。
设置(Setup)#
要在 n8n 中设置日志记录,你需要设置以下环境变量(你也可以在配置文件中设置这些值)
🌐 To set up logging in n8n, you need to set the following environment variables (you can also set the values in the configuration file)
| Setting in the configuration file | Using environment variables | Description |
|---|---|---|
| n8n.log.level | N8N_LOG_LEVEL | The log output level. The available options are (from lowest to highest level) are error, warn, info, and debug. The default value is info. You can learn more about these options here. |
| n8n.log.output | N8N_LOG_OUTPUT | Where to output logs. The available options are console and file. Multiple values can be used separated by a comma (,). console is used by default. |
| n8n.log.file.location | N8N_LOG_FILE_LOCATION | The log file location, used only if log output is set to file. By default, <n8nFolderPath>/logs/n8n.log is used. |
| n8n.log.file.fileSizeMax | N8N_LOG_FILE_SIZE_MAX | The maximum size (in MB) for each log file. By default, n8n uses 16 MB. |
| n8n.log.file.fileCountMax | N8N_LOG_FILE_COUNT_MAX | The maximum number of log files to keep. The default value is 100. This value should be set when using workers. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
日志级别(Log levels)#
n8n 使用标准日志级别来报告:
🌐 n8n uses standard log levels to report:
silent:完全不输出任何内容error:只输出错误,不输出其他内容warn:输出错误和警告信息info:包含有关进展的有用信息debug:最详尽的输出。n8n 会输出大量信息来帮助你调试问题。
开发(Development)#
在开发过程中,添加日志消息是一种良好的做法。它有助于调试错误。要为开发配置日志记录,请按照以下指南操作。
🌐 During development, adding log messages is a good practice. It assists in debugging errors. To configure logging for development, follow the guide below.
实现详情(Implementation details)#
n8n 使用位于 workflow 包中的 LoggerProxy 类。通过传入一个 Logger 的实例来调用 LoggerProxy.init(),可以在使用前初始化该类。
🌐 n8n uses the LoggerProxy class, located in the workflow package. Calling the LoggerProxy.init() by passing in an instance of Logger, initializes the class before the usage.
初始化过程只会发生一次。start.ts 文件已经为你完成了这个过程。如果你是从零创建一个新命令,则需要初始化 LoggerProxy 类。
🌐 The initialization process happens only once. The start.ts file already does this process for you. If you are creating a new command from scratch, you need to initialize the LoggerProxy class.
一旦在 cli 包中创建了 Logger 实现,就可以通过从导出模块调用 getInstance 便捷方法来获取它。
🌐 Once the Logger implementation gets created in the cli package, it can be obtained by calling the getInstance convenience method from the exported module.
查看 start.ts 文件以了解此过程的工作原理。
🌐 Check the start.ts file to learn more about how this process works.
添加日志(Adding logs)#
一旦在项目中初始化了 LoggerProxy 类,你就可以在任何其他文件中导入它并添加日志。
🌐 Once the LoggerProxy class gets initialized in the project, you can import it to any other file and add logs.
为所有日志级别提供了便捷方法,因此可以根据需要随时添加新日志,格式为 Logger.<logLevel>('<message>', ...meta),其中 meta 表示除了 message 之外所需的任何附加属性。
在上面的例子中,我们使用了上文描述的标准日志级别。message 参数是一个字符串,而 meta 是一个数据对象。
🌐 In the example above, we use the standard log levels described above. The message argument is a string, and meta is a data object.
1 2 3 4 5 6 7 8 9 | |
创建新日志记录器时,请记住以下一些实用标准:
🌐 When creating new loggers, some useful standards to keep in mind are:
- 尽量将日志消息制作得易于人类阅读。例如,始终用引号括住名称。
- 在日志消息和元数据中复制信息(例如上例中的工作流名称)非常有用,因为消息更容易搜索,元数据也便于筛选。
- 在所有日志中包含多个 ID(例如,
executionId、workflowId和sessionId)。 - 使用节点类型而不是节点名称(或两者都使用),因为这样更一致,也更容易搜索。
前端日志(Front-end logs)#
目前,前端日志不可用。使用 Logger 或 LoggerProxy 会在 editor-ui 包中导致错误。该功能将在未来的版本中实现。
🌐 As of now, front-end logs aren't available. Using Logger or LoggerProxy would yield errors in the editor-ui package. This functionality will get implemented in the future versions.