[SOLVED] Does Traefik have a way to custom format the access log and get the Request_Id?

Hi, dear community partners

I am currently having a problem:

How does Traefik's access log log log_format to get the desired content, or how does it get the request-id of the access?

Hope to help, thank you very much!

Hi @jack

I'm not sure what you are referring to in request-id. The logging format can be adjust as to what is included. By default the accesslog is in CLF(common log format) but can also be json if you wish.

Fields can be kept or dropped and Headers can be logged.

See the below link for more details.
https://doc.traefik.io/traefik/observability/access-logs/#limiting-the-fieldsincluding-headers

HI @cakiwi
TraceID of the whole link, This variable is visible in the NGINX log.

accessLog:
      filePath: ""             
      format: json              
      bufferingSize: 0         
      filters:
        statusCodes: ["200-600"]   
        retryAttempts: true     
        minDuration: 20         
      fields:                   
        defaultMode: keep      
        names:                 
          ClientUsername: drop
        headers:           
          defaultMode: keep    
          names:                
            User-Agent: keep 
            Authorization: drop
            Content-Type: keep

I kept all of them in the Traefik log configuration, but the Traefik generic log format was missing what I wanted

I think that might be a nginx created field. Not sure if will help but Traefik support a bunch of tracing
backends:

https://doc.traefik.io/traefik/observability/tracing/overview/#tracing

Not like that, is to obtain "$request_id" in the access log, this variable is the built-in in nginx, current business need to get to this value, for the problem.
http://nginx.org/en/docs/http/ngx_http_core_module.html

Looks like you can leverage the tracing without a full jaeger setup. See this comment in github.

edit:
Here is a json access log and the header as received by a traefik/whoami service

traefik_1 | {"ClientAddr":"172.18.0.1:49866","ClientHost":"172.18.0.1","ClientPort":"49866","ClientUsername":"-","DownstreamContentSize":413,"DownstreamStatus":200,"Duration":39971759,"OriginContentSize":413,"OriginDuration":39636734,"OriginStatus":200,"Overhead":335025,"RequestAddr":"localhost","RequestContentSize":0,"RequestCount":2,"RequestHost":"localhost","RequestMethod":"GET","RequestPath":"/whoami","RequestPort":"-","RequestProtocol":"HTTP/1.1","RequestScheme":"http","RetryAttempts":0,"RouterName":"whoami@docker","ServiceAddr":"172.18.0.3:80","ServiceName":"whoami-10571@docker","ServiceURL":{"Scheme":"http","Opaque":"","User":null,"Host":"172.18.0.3:80","Path":"","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":""},"StartLocal":"2021-05-10T13:52:16.918115828Z","StartUTC":"2021-05-10T13:52:16.918115828Z","downstream_Content-Length":"413","downstream_Content-Type":"text/plain; charset=utf-8","downstream_Date":"Mon, 10 May 2021 13:52:16 GMT","entryPointName":"web","level":"info","msg":"","origin_Content-Length":"413","origin_Content-Type":"text/plain; charset=utf-8","origin_Date":"Mon, 10 May 2021 13:52:16 GMT","request_Accept":"/","request_User-Agent":"curl/7.68.0","request_X-Forwarded-Host":"localhost","request_X-Forwarded-Port":"80","request_X-Forwarded-Proto":"http","request_X-Forwarded-Server":"77cbda8be111","request_X-Real-Ip":"172.18.0.1","request_X-Request-Id":"5f67d067566073cf:39dd5a59db9a5ed0:5f67d067566073cf:0","time":"2021-05-10T13:52:16Z"}

curl localhost/whoami
Hostname: 4424944b2024
IP: 127.0.0.1
IP: 172.18.0.3
RemoteAddr: 172.18.0.2:57996
GET /whoami HTTP/1.1
Host: localhost
User-Agent: curl/7.68.0
Accept: /
Accept-Encoding: gzip
X-Forwarded-For: 172.18.0.1
X-Forwarded-Host: localhost
X-Forwarded-Port: 80
X-Forwarded-Proto: http
X-Forwarded-Server: 77cbda8be111
X-Real-Ip: 172.18.0.1
X-Request-Id: 5f67d067566073cf:39dd5a59db9a5ed0:5f67d067566073cf:0

Hi I tried to add this field in the configuration file, but it did not appear in the access log
image

Its not just the field. Jaeger needs to be enabled. Standby for a minimal configuration.

version: "3.8"

services:
  traefik:
    image: "traefik:v2.4"
    command:
    - --entrypoints.web.address=:80
    - --providers.docker=true
    - --providers.docker.exposedbydefault=false
    - --accesslog
    - --accesslog.format=json
    - --accesslog.fields.headers.defaultmode=keep
    - --accesslog.fields.headers.names.x-request-id=keep
    - --api
    - --log.level=INFO
    - --log.format=json
    - --tracing.jaeger=true
    - --tracing.jaeger.samplingParam=0
    - --tracing.jaeger.traceContextHeaderName=x-request-id
    ports:
      - published: 80
        target: 80
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
  whoami:
    image: traefik/whoami
    labels:
      traefik.enable: "true"
      traefik.http.routers.whoami.rule: PathPrefix(`/whoami`)

1 Like

Thank you very much. My problem has been solved. Thank you for your help :+1:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.