g_log_structed 未显示在系统日志中

发布于 2025-01-13 16:50:25 字数 709 浏览 2 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2025-01-20 16:50:25

“结构化日志记录”和“日志记录”是两个相似但不同的概念。

目前,您可以对多个输出流进行结构化日志记录:例如文件(写入 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,它写入日志。

换句话说,如果您想无条件写入日志,则需要在记录任何消息之前调用以下命令:

int main(int argc, char *argv[]) {
    /* You can of course write your own GLogWriterFunc and use that here */
    g_log_set_writer_func (g_log_writer_journald, NULL, NULL);

    /* The following log message will go to journald */
    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;
}

"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 using g_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 issuing g_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 and g_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:

int main(int argc, char *argv[]) {
    /* You can of course write your own GLogWriterFunc and use that here */
    g_log_set_writer_func (g_log_writer_journald, NULL, NULL);

    /* The following log message will go to journald */
    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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文