kong
kong copied to clipboard
How to modify the log printing format of error.log?
- 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.
- 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!
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
take a look at: https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/errlog.md#raw_log
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.
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...
@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.
@1798972 have you ever found a way to remove or modify that part of the log?