g_log_structed 未显示在系统日志中
我尝试使用 g_log_structed
将消息从 GLib 输出到 Linux 日志,但消息没有出现。
#define G_LOG_USE_STRUCTURED
#include <glib-2.0/glib.h>
#include <stdio.h>
/* Compile command
gcc mysource.c -lglib-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
*/
int main(int argc, char *argv[]) {
g_log_structured (G_LOG_DOMAIN,
G_LOG_LEVEL_INFO,
"CODE_FILE", "mysource.c",
"CODE_LINE", 312,
"MESSSAGE_ID", "123456",
"MESSAGE", "You have %d eggs", 12 + 2);
return 0;
}
运行程序后,我使用 journalctl
扫描日志,但找不到我的消息。我在乱搞什么?
I'm attempting to use g_log_structured
to output messages from GLib to the Linux journal, but the messages aren't appearing.
#define G_LOG_USE_STRUCTURED
#include <glib-2.0/glib.h>
#include <stdio.h>
/* Compile command
gcc mysource.c -lglib-2.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
*/
int main(int argc, char *argv[]) {
g_log_structured (G_LOG_DOMAIN,
G_LOG_LEVEL_INFO,
"CODE_FILE", "mysource.c",
"CODE_LINE", 312,
"MESSSAGE_ID", "123456",
"MESSAGE", "You have %d eggs", 12 + 2);
return 0;
}
After running the program, I scan the journal with journalctl
, but I can't find my message. What am I messing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“结构化日志记录”和“日志记录”是两个相似但不同的概念。
目前,您可以对多个输出流进行结构化日志记录:例如文件(写入 CSV)或标准输出(您可以在其中对某些字段进行着色)。这确实是通过定义 G_LOG_USE_STRUCTURED 和使用 g_log_structural() 来完成的。
另一方面,您需要某种回调来处理应用程序记录结构化消息时应该发生的情况。这就是我们所说的
GLogWriterFunc
。可以通过发出g_log_set_writer_func()
。 GLib 默认提供了一些这样的日志编写器回调,因为它们非常常见:g_log_writer_standard_streams
,它写入 stdout/stderr 和g_log_writer_journald
,它写入日志。换句话说,如果您想无条件写入日志,则需要在记录任何消息之前调用以下命令:
"Structured logging" and "logging to the journal" are 2 similar but different concepts.
On hand, you can do structured logging to several output streams: a file for example (which writes a CSV), or to stdout (where you might colorize certain fields). This is indeed done by defining
G_LOG_USE_STRUCTURED
and usingg_log_structured()
On the other hand, you need some kind of callback to handle what should happen whenever the application logs a structured message. This is what we call a
GLogWriterFunc
. This callback can be set by issuingg_log_set_writer_func()
. GLib provides a few such log writer callbacks by default since they are so common:g_log_writer_standard_streams
, which writes to stdout/stderr andg_log_writer_journald
, which writes to journald.In other words, if you want to unconditionally write to the journal, you need to call the following before logging any message: