rsyslog 生成 UUID,符合 RFC4122。

huangapple go评论64阅读模式
英文:

rsyslog generate uuid as rfc4122

问题

我有以下的 rsyslog 配置和接收到的日志信息。我想为每条日志消息添加一个 UUID。

我目前生成 UUID 如下。然而,该 UUID 没有按照我想要的 rfc4122 格式进行格式化。我是不是做错了什么,还是有更好的方法可以实现这个目标?

当前:F2GCCB4C390142A4B9DBVBD8B9FD0ED2。我想要的:f2gccb4c-3901-42a4-b9db-vbd8b9fd0ed2

英文:

I've got the following rsyslog conf and the below log message I'm receiving. I would like to add an uuid to each log message.

I'm currently generating a uuid as follows. However, the uuid is not being formatted as rfc4122 which I would like to do. Am I doing something wrong, or could this be achieved in a better way?

Currently: F2GCCB4C390142A4B9DBVBD8B9FD0ED2. What I would like: f2gccb4c-3901-42a4-b9db-vbd8b9fd0ed2

/path/to/log/file.log

 11955 - [Mon Apr  6 20:40:03 2023] [Info   ] This message can contain anything [d54d13fa-4657-4891-f99d08674ee]

/etc/rsyslog.d/mylog.conf

template(name="jsonFormat" type="list") {
    property(outname="id" name="uuid" format="jsonf")
}

答案1

得分: 1

使用property replacer在模板中将字符串转换为小写的唯一方法。有3种使用方式。可能最易读的方式是使用一些RainerScript首先构建rfc4122 uuid格式的方式:

set $.uuid = substring($uuid, 0, 8) & "-" & substring($uuid, 8, 4);
template(name="myformat" type="list") {
 property(name="$.uuid" outname="uuid" caseconversion="lower" format="jsonf")
}

这将变量$.uuid设置为uuid属性的适当子串(从0索引开始,指定长度)的连接(&)。我只展示了前两部分,其余部分需要类似处理。然后,模板只是应用小写操作。

另一种方法是在模板中完成所有操作,但这非常冗长:

template(name="myformat" type="list") {
 constant(value=" \"uuid\":\"")
 property(name="uuid" position.from="1" position.to="8" caseconversion="lower")
 constant(value="-")
 property(name="uuid" position.from="9" position.to="12" caseconversion="lower")
 ...
}

请注意,位置从1开始,并且"to"是包含的。

最后,您可以使用更简洁的字符串模板并"执行"它,以将生成的字符串捕获到变量中:

template(name="myuuid" type="string" string="%uuid:1:8:lowercase%-%uuid:9:12:lowercase%")
set $.uuid = exec_template("myuuid");

这里的子串起始和结束都是包含的,从1开始。

英文:

The only way to lowercase a string is within a template using a property
replacer
.
There are 3 ways to use this. Probably the most readable is to use some
RainerScript to build up the rfc4122 uuid format first:

set $.uuid = substring($uuid,0,8) & "-" & substring($uuid,8,4);
template(name="myformat" type="list") {
 property(name="$.uuid" outname="uuid" caseconversion="lower" format="jsonf")
}

This sets variable $.uuid to the concatenation (&) of the appropriate
substrings (index from 0, length) of the uuid property. I have just shown
the first 2 parts; the rest needs to be done similarly.
The template then just applies the lowercasing.

Alternatively, it can all be done in the template, but it is very verbose:

template(name="myformat" type="list") {
 constant(value=" \"uuid\":\"")
 property(name="uuid" position.from="1" position.to="8" caseconversion="lower")
 constant(value="-")
 property(name="uuid" position.from="9" position.to="12" caseconversion="lower")
 ...
}

Note that positions start from 1, and the to is inclusive.

Finally, you can use a terser string template and "execute" it, to capture
the resulting string in a variable:

template(name="myuuid" type="string" string="%uuid:1:8:lowercase%-%uuid:9:12:lowercase%")
set $.uuid = exec_template("myuuid");

Here the substring is start and end inclusive, starting from 1.

huangapple
  • 本文由 发表于 2023年4月11日 01:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定