簡體   English   中英

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

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

我有一個具有以下配置的 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

所以我在在線 JSON 格式化程序中復制粘貼了非縮進的 JSON output 但它只縮進了一部分,使整個東西變得無用。

您的問題是 Traefik 不是 output 單個 JSON 文檔,而是每行一個 JSON 文檔。 您可以使用xargsjq美化所有文檔:

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

在您的示例中,這將導致此 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