简体   繁体   English

如何漂亮格式化JSON中的Traefik日志?

[英]How to pretty format the Traefik log in JSON?

I have a Traefik service with the following configuration:我有一个具有以下配置的 Traefik 服务:

version: "3.9"
services:
  reverse-proxy:
    image: traefik:v2.3.4
    networks:
      common:
    ports:
      - target: 80
        published: 80
        mode: host
      - target: 443
        published: 443
        mode: host
    command:
      - "--providers.docker.endpoint=unix:///var/run/docker.sock"
      - "--providers.docker.swarmMode=true"
      - "--providers.docker.exposedbydefault=false"
      - "--providers.docker.network=common"
      - "--entrypoints.web.address=:80"
#      - "--entrypoints.websecure.address=:443"
      - "--global.sendAnonymousUsage=true"
      # Set a debug level custom log file
      - "--log.level=DEBUG"
      - "--log.format=json"
      - "--log.filePath=/var/log/traefik.log"
      - "--accessLog.filePath=/var/log/access.log"
      # Enable the Traefik dashboard
      - "--api.dashboard=true"
#      - "traefik.constraint-label=common" TODO
    deploy:
      placement:
        constraints:
          - node.role == manager
      labels:
        # Expose the Traefik dashboard
        - "traefik.enable=true"
        - "traefik.http.routers.dashboard.service=api@internal"
        - "traefik.http.services.traefik.loadbalancer.server.port=888" # A port number required by Docker Swarm but not being used in fact
        - "traefik.http.routers.dashboard.rule=Host(`traefik.learnintouch.com`)"
        - "traefik.http.routers.traefik.entrypoints=web"
#        - "traefik.http.routers.traefik.entrypoints=websecure"
        # Basic HTTP authentication to secure the dashboard access
        - "traefik.http.routers.traefik.middlewares=traefik-auth"
        - "traefik.http.middlewares.traefik-auth.basicauth.users=stephane:$$apr1$$m72sBfSg$$7.NRvy75AZXAMtH3C2YTz/"
    volumes:
      # So that Traefik can listen to the Docker events
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "~/dev/docker/projects/common/volumes/logs/traefik.service.log:/var/log/traefik.log"
      - "~/dev/docker/projects/common/volumes/logs/traefik.access.log:/var/log/access.log"

Then I watch the log with the command:然后我用命令查看日志:

stephane@stephane-pc:~$ tail -f dev/docker/projects/common/volumes/logs/traefik.service.log 
{"level":"info","msg":"I have to go...","time":"2021-07-03T10:18:10Z"}
{"level":"info","msg":"Stopping server gracefully","time":"2021-07-03T10:18:10Z"}
{"entryPointName":"web","level":"debug","msg":"Waiting 10s seconds before killing connections.","time":"2021-07-03T10:18:10Z"}
{"entryPointName":"web","level":"error","msg":"accept tcp [::]:80: use of closed network connection","time":"2021-07-03T10:18:10Z"}

I expected the log to be formatted in JSON with indentation .我希望日志格式为 JSON indentation

So I copy-pasted the non indented JSON output in an online JSON formatter but it only indented part of it, making the whole thing useless.所以我在在线 JSON 格式化程序中复制粘贴了非缩进的 JSON output 但它只缩进了一部分,使整个东西变得无用。

Your problem is that Traefik does not output a single JSON document, but one JSON document per line .您的问题是 Traefik 不是 output 单个 JSON 文档,而是每行一个 JSON 文档。 You could beautify all documents using xargs and jq :您可以使用xargsjq美化所有文档:

tail -f dev/docker/projects/common/volumes/logs/traefik.service.log | xargs -n 1 -d "\n" -- bash -c 'echo "$1" | jq' _

In your example, this will result in this output (even with syntax highlighting if your terminal supports that):在您的示例中,这将导致此 output(如果您的终端支持,即使语法突出显示):

{
  "level": "info",
  "msg": "I have to go...",
  "time": "2021-07-03T10:18:10Z"
}
{
  "level": "info",
  "msg": "Stopping server gracefully",
  "time": "2021-07-03T10:18:10Z"
}
{
  "entryPointName": "web",
  "level": "debug",
  "msg": "Waiting 10s seconds before killing connections.",
  "time": "2021-07-03T10:18:10Z"
}
{
  "entryPointName": "web",
  "level": "error",
  "msg": "accept tcp [::]:80: use of closed network connection",
  "time": "2021-07-03T10:18:10Z"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM