Graylog 扩展格式日志驱动程序

gelf 日志驱动程序是一种方便的格式,被许多工具所理解,例如 Graylog, Logstash, 和 Fluentd。许多工具使用这种格式。

在GELF中,每条日志消息都是一个包含以下字段的字典:

  • 版本
  • 主机(最初发送消息的人)
  • 时间戳
  • 消息的简短和完整版本
  • 您自己配置的任何自定义字段

用法

要将gelf驱动程序设置为默认的日志记录驱动程序,请在daemon.json文件中将log-driverlog-opt键设置为适当的值,该文件位于Linux主机上的/etc/docker/或Windows Server上的C:\ProgramData\docker\config\daemon.json。有关使用daemon.json配置Docker的更多信息,请参阅daemon.json

以下示例将日志驱动程序设置为gelf并设置gelf-address选项。

{
  "log-driver": "gelf",
  "log-opts": {
    "gelf-address": "udp://1.2.3.4:12201"
  }
}

重启Docker以使更改生效。

注意

log-opts 配置文件中的配置选项在 daemon.json 配置文件中必须以字符串形式提供。因此,布尔值和数值(例如 gelf-tcp-max-reconnect 的值)必须用引号(")括起来。

您可以通过在使用docker container createdocker run时设置--log-driver标志来为特定容器设置日志驱动程序:

$ docker run \
      --log-driver gelf --log-opt gelf-address=udp://1.2.3.4:12201 \
      alpine echo hello world

GELF 选项

gelf 日志驱动程序支持以下选项:

OptionRequiredDescriptionExample value
gelf-addressrequiredThe address of the GELF server. tcp and udp are the only supported URI specifier and you must specify the port.--log-opt gelf-address=udp://192.168.0.42:12201
gelf-compression-typeoptionalUDP Only The type of compression the GELF driver uses to compress each log message. Allowed values are gzip, zlib and none. The default is gzip. Note that enabled compression leads to excessive CPU usage, so it's highly recommended to set this to none.--log-opt gelf-compression-type=gzip
gelf-compression-leveloptionalUDP Only The level of compression when gzip or zlib is the gelf-compression-type. An integer in the range of -1 to 9 (BestCompression). Default value is 1 (BestSpeed). Higher levels provide more compression at lower speed. Either -1 or 0 disables compression.--log-opt gelf-compression-level=2
gelf-tcp-max-reconnectoptionalTCP Only The maximum number of reconnection attempts when the connection drop. An positive integer. Default value is 3.--log-opt gelf-tcp-max-reconnect=3
gelf-tcp-reconnect-delayoptionalTCP Only The number of seconds to wait between reconnection attempts. A positive integer. Default value is 1.--log-opt gelf-tcp-reconnect-delay=1
tagoptionalA string that's appended to the APP-NAME in the gelf message. By default, Docker uses the first 12 characters of the container ID to tag log messages. Refer to the 日志标签选项文档 for customizing the log tag format.--log-opt tag=mailer
labelsoptionalApplies when starting the Docker daemon. A comma-separated list of logging-related labels this daemon accepts. Adds additional key on the extra fields, prefixed by an underscore (_). Used for advanced 日志标签选项.--log-opt labels=production_status,geo
labels-regexoptionalSimilar to and compatible with labels. A regular expression to match logging-related labels. Used for advanced 日志标签选项.--log-opt labels-regex=^(production_status|geo)
envoptionalApplies when starting the Docker daemon. A comma-separated list of logging-related environment variables this daemon accepts. Adds additional key on the extra fields, prefixed by an underscore (_). Used for advanced 日志标签选项.--log-opt env=os,customer
env-regexoptionalSimilar to and compatible with env. A regular expression to match logging-related environment variables. Used for advanced 日志标签选项.--log-opt env-regex=^(os|customer)

注意

gelf 驱动程序不支持 TCP 连接的 TLS。发送到受 TLS 保护的输入的消息可能会静默失败。

示例

此示例配置容器以使用在端口12201上运行的GELF服务器192.168.0.42

$ docker run -dit \
    --log-driver=gelf \
    --log-opt gelf-address=udp://192.168.0.42:12201 \
    alpine sh