Skip to content

正在登录 n8n#

¥Logging in n8n

日志记录是调试的重要功能。n8n 使用 winston 日志库。

¥Logging is an important feature for debugging. n8n uses the winston logging library.

Log streaming

n8n 自托管企业版除了本文档中描述的日志记录选项外,还包含 日志流

¥n8n Self-hosted Enterprise tier includes Log streaming, in addition to the logging options described in this document.

设置#

¥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)

在配置文件中进行设置 使用环境变量 描述
n8n.log.level N8N_LOG_LEVEL 日志输出级别。可用选项(从低到高)为:错误、警告、信息和调试。默认值为 info。你可以在 此处 页面上了解更多关于这些选项的信息。
n8n.log.output N8N_LOG_OUTPUT 日志输出位置。可用选项为 consolefile。可以使用多个值,并用逗号分隔 (,)。默认使用 console
n8n.log.file.location N8N_LOG_FILE_LOCATION 日志文件位置,仅当日志输出设置为文件时使用。默认情况下,使用 <n8nFolderPath>/logs/n8n.log 权限。
n8n.log.file.fileSizeMax N8N_LOG_FILE_SIZE_MAX 每个日志文件的最大大小(以 MB 为单位)。默认情况下,n8n 使用 16 MB 内存。
n8n.log.file.fileCountMax N8N_LOG_FILE_COUNT_MAX 要保留的最大日志文件数。默认值为 100。使用 worker 时应设置此值。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Set the logging level to 'debug'
export N8N_LOG_LEVEL=debug

# Set log output to both console and a log file
export N8N_LOG_OUTPUT=console,file

# Set a save location for the log file
export N8N_LOG_FILE_LOCATION=/home/jim/n8n/logs/n8n.log

# Set a 50 MB maximum size for each log file
export N8N_LOG_FILE_SIZE_MAX=50

# Set 60 as the maximum number of log files to be kept
export N8N_LOG_FILE_COUNT_MAX=60

日志级别#

¥Log levels

n8n 使用标准日志级别来报告:

¥n8n uses standard log levels to report:

  • silent:不输出任何内容

¥silent: outputs nothing at all

  • error:仅输出错误信息,不输出其他任何信息

¥error: outputs only errors and nothing else

  • warn:输出错误和警告信息

¥warn: outputs errors and warning messages

  • info:包含有关进度的有用信息

¥info: contains useful information about progress

  • debug:最详细的输出。n8n 输出大量信息,帮助你调试问题。

¥debug: the most verbose output. n8n outputs a lot of information to help you debug issues.

开发#

¥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 之外的任何其他属性。

¥Convenience methods are provided for all logging levels, so new logs can be added whenever needed using the format Logger.<logLevel>('<message>', ...meta), where meta represents any additional properties desired beyond message.

在上面的示例中,我们使用了 above 中描述的标准日志级别。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
// You have to import the LoggerProxy. We rename it to Logger to make it easier

import {
	LoggerProxy as Logger
} from 'n8n-workflow';

// Info-level logging of a trigger function, with workflow name and workflow ID as additional metadata properties

Logger.info(`Polling trigger initiated for workflow "${workflow.name}"`, {workflowName: workflow.name, workflowId: workflow.id});

创建新日志记录器时,请记住以下一些实用标准:

¥When creating new loggers, some useful standards to keep in mind are:

  • 编写尽可能易于阅读的日志消息。例如,名称务必用引号括起来。

¥Craft log messages to be as human-readable as possible. For example, always wrap names in quotes.

  • 在日志消息和元数据中复制信息(例如上例中的工作流名称)非常有用,因为消息更容易搜索,元数据也便于筛选。

¥Duplicating information in the log message and metadata, like workflow name in the above example, can be useful as messages are easier to search and metadata enables easier filtering.

  • 在所有日志中包含多个 ID(例如,executionIdworkflowIdsessionId)。

¥Include multiple IDs (for example, executionId, workflowId, and sessionId) throughout all logs.

  • 使用节点类型而不是节点名称(或两者都使用),因为这样更一致,也更容易搜索。

¥Use node types instead of node names (or both) as this is more consistent, and so easier to search.

前端日志#

¥Front-end logs

目前,前端日志不可用。使用 LoggerLoggerProxy 会导致 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.