Unit: Change Access Log Format

Created on 24 Nov 2020  ·  10Comments  ·  Source: nginx/unit

Hi guys,

I didn't find in the documentation a way to change the format of the access logs. I would like to change the order and add other fields (e.g. X-Forwarded-For, Host). How can I accomplish that?

Thank you!

enhancement

Most helpful comment

@robjr
Here's a patch to support the custom log format.
https://gist.github.com/hongzhidao/adecca91feb7179fedadf339ea751894

Please note it's just my personal try, not unit official.

Unit configuration.

access_log: {
    "path": "access log file",
    "format": "$remote_addr - - [$local_time] \"$request_uri\" $status $bytes_sent \"$http_referer\" \"$http_user_agent\""
}

Now, these variables are avaiable.
$host, $uri, $method, $remote_addr, $local_time, $request_uri,
$status, $bytes_sent, $http_referer, $http_user_agent,
${headers.header name}, ${args.arg name}, ${cookies.cookie name}

Feedbacks are welcome :)

All 10 comments

It's not possible yet. To add such feature we first need to implement more configuration variables.

@robjr

  1. Could you show your full format expected?
  2. With X-Forwarded-For, which usage do you prefer to? $http_x_forwared_for or $headers['X-Forwarded-For'].

Hi @hongzhidao,

  1. It would be great to have the output formatted as JSON or at least we could have it as an option. I'm looking for timestamp, remote address, host, request method, URI, status, user agent, referer, response size, request time, xforwarded.
  2. I prefer $headers['X-Forwarded-For'] as it looks more legible to me and it explicitly states that the value comes from the headers.

Thank you for looking into this @hongzhidao , @VBart

@robjr
Just add that there is another format. ${headers.X-Forwarded-For}
Syntax: ${headers.arbitray-head-name}

@robjr
Here's a patch to support the custom log format.
https://gist.github.com/hongzhidao/adecca91feb7179fedadf339ea751894

Please note it's just my personal try, not unit official.

Unit configuration.

access_log: {
    "path": "access log file",
    "format": "$remote_addr - - [$local_time] \"$request_uri\" $status $bytes_sent \"$http_referer\" \"$http_user_agent\""
}

Now, these variables are avaiable.
$host, $uri, $method, $remote_addr, $local_time, $request_uri,
$status, $bytes_sent, $http_referer, $http_user_agent,
${headers.header name}, ${args.arg name}, ${cookies.cookie name}

Feedbacks are welcome :)

Hi @hongzhidao ,

Awesome, I like this format ${headers.X-Forwarded-For}.
I just tested the patch locally and it is working like a charm. If we can add two other variables to include the HTTP version and another for the request time it would be perfect. After enough testing, I'm wondering if we can include this in one of the next releases. Thank you very much!

If we can add two other variables to include the HTTP version and another for the request time it would be perfect.

  1. The HTTP version can be implemented together with the above variables.
  2. It seems the request time information is related to Unit metric information, it's a big feature.
    https://github.com/nginx/unit/issues/384

I'm wondering if we can include this in one of the next releases.

Actually, from my personal impression, all these NGINX open source projects are quite stable, even if development versions.
And they are active with feedback. I'll keep updating the patch in this ticket, you are welcome to merge it in the latest version.

@robjr

Updated.
https://gist.github.com/hongzhidao/adecca91feb7179fedadf339ea751894

Added two variables. $http_version and $request_time. Welcome to test.

Thank you for including the $request_time variable. This is exactly what I was so much missing! Hope it is a part of the stable version soon.

Oh man! This is great. I came here looking to see if Unit could support logging custom formats (ie. outputting JSON like how Nginx does with escape=json).

Any chance more options like this can find its way into stable?

Was this page helpful?
0 / 5 - 0 ratings