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

70gysomp  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(143)

我的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"

然后我查看日志,命令如下:

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"}

我希望日志的格式是JSON格式的indentation
因此,我在online JSON formatter中复制粘贴了未缩进的JSON输出,但它只缩进了其中的一部分,使得整个输出毫无用处。

ki1q1bka

ki1q1bka1#

您的问题是Traefik不输出单个JSON文档,而是 * 每行 * 输出一个JSON文档。您可以使用xargsjq美化所有文档:

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

在您的示例中,这将导致以下输出(如果您的终端支持,即使语法突出显示也是如此):

{
  "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"
}

相关问题