kong icon indicating copy to clipboard operation
kong copied to clipboard

How to modify the log printing format of error.log?

Open 1798972 opened this issue 2 years ago • 3 comments

  1. How to customize the format of plug-in running logs? Now the log information is separated by |, and the service address and other information will be automatically added later, which does not meet my requirements. I want to customize the display format of the log.
  2. How to add information such as thread id to the running log of the plug-in to locate the running log of a request? One way is to put an id information in the header of http as a flag, and then display the id in the log. But it needs to be manually added to the log statement every time, which is too troublesome. In Java projects, you can customize the log format to automatically add this id. How to implement it in kong?

THANKS!

1798972 avatar Sep 07 '22 16:09 1798972

Hi @1798972

NGINX does not support changing the format of error_log.

For customizing log data, I'd recommend checking out something from our {file,http,tcp,udp}-log family of plugins: https://docs.konghq.com/hub/#logging

All of these plugins allow you to extend the "default" request log entry by writing a lua expression that returns a value. Example: https://docs.konghq.com/hub/kong-inc/file-log/#custom-fields-by-lua

Additionally, if you are developing your own plugin, I'd recommend checking out the kong.log.set_serialize_value() PDK function. This function can be used within your plugin code to amend the log entry with almost any kind of extra contextual information you'd like: https://docs.konghq.com/gateway/latest/pdk/kong.log/#konglogset_serialize_valuekey-value-options

flrgh avatar Sep 07 '22 17:09 flrgh

take a look at: https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/errlog.md#raw_log

bungle avatar Sep 08 '22 01:09 bungle

take a look at: https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/errlog.md#raw_log Thank you. At present, my trouble is that the length of the information I output in the log is random, and this string of information at the end is appended to msg every time, which makes the format very ugly and makes browsing the log information very painful. image

1798972 avatar Sep 08 '22 02:09 1798972

Any news related to this? For instance, we need to split the EventSeverity into a dedicated field in order to not INDEX several levels like INFO, VERBOSE etc...

flowdopip avatar Aug 03 '23 21:08 flowdopip

@1798972 @flowdopip Direct write to error.log could be achieved with a bit of hack. Using FFI you may get the file descriptor of the log file with ngx_cycle->log->file->fd, and write it with fwrite. This fd works fine with log rotating/reconfiguration(changing log path).

Hope the solution helps. Feel free to reopen the issue if you still have concerns.

StarlightIbuki avatar Oct 11 '23 08:10 StarlightIbuki

@1798972 have you ever found a way to remove or modify that part of the log?

akira28 avatar May 24 '24 05:05 akira28