How to prefix file name, line, function to glib debug logs

Viewed 211

I am working on one of the glib based applications. I am not able to find a way to have the file name, line number, function name as log prefix. I am just getting logs in below format:

(myApp:19627): myApp-DEBUG: 10:01:35.212: IP packet received on interface "enp0s20f0u1c2": (myApp:19627): myApp-DEBUG: 10:01:35.212: interface MAC: 00:0e:09:86:ed:c3 (myApp:19627): myApp-DEBUG: 10:01:35.212: src MAC: ec:8e:b5:d6:42:14 (myApp:19627): myApp-DEBUG: 10:01:35.212: dst MAC: 00:0e:09:86:ed:c3 (myApp:19627): myApp-INFO: 10:01:35.212: IPV4 packet:*

I have already enabled below env variables:

G_MESSAGES_PREFIXED=all  
G_MESSAGES_DEBUG=all
1 Answers

If the log messages are created using g_message() or any of the other helper macros, then the file name, line number and function are all written as structured log data, which means they appear in the systemd journal but not the stdout output.

See the output of journalctl --output=verbose and look for the GLIB_DOMAIN, CODE_FILE, CODE_LINE and CODE_FUNC fields.

If you’re outputting to something other than the systemd journal, then you will need to write your own log writer function in your app, and install it using g_log_set_writer_func(). It will receive the above fields from the log call sites, and can print them formatted as you wish.

Related