gpt4 book ai didi

varnish - 如何在编写自定义 Varnish 模块时记录错误?

转载 作者:行者123 更新时间:2023-12-03 17:45:08 26 4
gpt4 key购买 nike

我正在学习 varnish 以及如何使用内联 c 代码扩展 varnish vmod。我从编写自己的自定义 Varnish 模块开始。我想从我的自定义模块中记录错误和失败。我如何做到这一点?

我可以从 C 可用的各种日志库中进行选择。但我想检查是否有任何内置的 varnish 库可以使用它。下面是我的 vmod c 文件的示例代码。

#include "vrt.h"
#include "cache/cache.h"
#include "vcc_if.h"
#include <jansson.h>

#define JSON_ERROR "-1"
#define JSON_LOC "/etc/example.json"


VCL_STRING
vmod_validate_mymod(VRT_CTX) {
(void) ctx;
char *return_code = "0";
json_t *jobj;
json_error_t error;
jobj = json_load_file(JSON_LOC,0,&error);
if (!jobj) {
// error log here
return JSON_ERROR;
}
return return_code;
}


当上面代码中的 if 条件为真时,我希望将错误日志行添加到自定义日志文件中。请帮忙。

最佳答案

你想要VSLb :

VSLb(ctx->vsl, SLT_VCL_Log, "%d", 5);
如果您需要构建更大的字符串,或者需要分配,请使用 WS_*函数,它们的分配在请求结束时自动释放。
看看如何 std.log()做到了吗: https://github.com/varnishcache/varnish-cache/blob/389d7ba28e0d0e3a2d5c30a959aa517e5166b246/vmod/vmod_std.c#L145-L153

关于varnish - 如何在编写自定义 Varnish 模块时记录错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55758142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com