Skip to content

正在登录 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)

| 配置文件中的设置 | 使用环境变量 | 描述 |

|-----------------------------------|-----------------------------|-------------|

| n8n.log.level | N8N_LOG_LEVEL | 日志输出级别。可用选项(从最低到最高级别)包括 error、warn、info 和 debug。默认值是 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。使用工作线程时应设置此值。 |

 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:完全没有输出
  • 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
// 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:

  • 尽量将日志消息制作得易于人类阅读。例如,始终用引号括住名称。
  • 在日志消息和元数据中复制信息(例如上例中的工作流名称)非常有用,因为消息更容易搜索,元数据也便于筛选。
  • 在所有日志中包含多个 ID(例如,executionIdworkflowIdsessionId)。
  • 使用节点类型而不是节点名称(或两者都使用),因为这样更一致,也更容易搜索。

前端日志(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.