tencent cloud

Cloud Log Service

ログ構造化-複数フォーマット

ダウンロード
フォーカスモード
フォントサイズ
最終更新日: 2026-10-12 14:51:40
AI翻訳

シナリオ説明

王さんはユーザー操作と結果のログを、単一行テキスト形式でCLSに収集します。ログの形式と内容は完全には同じではありません。王さんは一連のステートメントを作成し、異なる形式のログを構造化したいと考えています。 整理して確認したところ、ログは基本的に3つの形式に分けられます:1つ目はuin、requestid、action、Reqbodyの4つのフィールドを含み、2つ目はuin、requestid、actionの3つのフィールドを含み、3つ目はrequestid、action、TaskIdの3つのフィールドを含みます。

シナリオ分析

王さんの加工要件を整理すると、加工方針は以下の通りです:
1. 3つの形式のログにはいずれもrequestid、actionフィールドが含まれるため、正規表現を使用してrequestidとactionフィールドを抽出します。
2. uin、reqbody、TaskIdフィールドに対して特殊な処理を行います。まずこのフィールドが存在するかどうかを判断し、存在する場合は抽出を行います。

元のログ

[
{
"__CONTENT__": "2021-11-29 15:51:33.201 INFO request 7143a51d-caa4-4a6d-bbf3-771b4ac9e135 action: Describe uin: 15432829 reqbody {\\"Key\\": \\"config\\",\\"Values\\": \\"appisrunnning\\",\\"Action\\": \\"Describe\\",\\"RequestId\\": \\"7143a51d-caa4-4a6d-bbf3-771b4ac9e135\\",\\"AppId\\": 1302953499,\\"Uin\\": \\"100015432829\\"}"
},
{
"__CONTENT__": "2021-11-2915: 51: 33.272 ERROR request 2ade9fc4-2db2-49d8-b3e0-a6ea78ce8d96 has error action DataETL uin 15432829"
},
{
"__CONTENT__": "2021-11-2915: 51: 33.200 INFO request 6059b946-25b3-4164-ae93-9178c9e73d75 action: UploadData hUWZSs69yGc5HxgQ TaskId 51d-caa-a6d-bf3-7ac9e"
}
]

DSL加工関数

fields_set("requestid",regex_select(v("__CONTENT__"),regex="request [A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+",index=0,group=0))
fields_set("action",regex_select(v("__CONTENT__"),regex="action: \\S+|action \\S+",index=0,group=0))
t_if(regex_match(v("__CONTENT__"),regex="uin", full=False),fields_set("uin",regex_select(v("__CONTENT__"),regex="uin: \\d+|uin \\d+",index=0,group=0)))
t_if(regex_match(v("__CONTENT__"),regex="TaskId", full=False),fields_set("TaskId",regex_select(v("__CONTENT__"),regex="TaskId [A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+",index=0,group=0)))
t_if(regex_match(v("__CONTENT__"),regex="reqbody", full=False),fields_set("requestbody",regex_select(v("__CONTENT__"),regex="reqbody \\{[^\\}]+\\}")))
t_if(has_field("requestbody"),fields_set("requestbody",str_replace(v("requestbody"),old="reqbody",new="")))
fields_drop("__CONTENT__")
fields_set("requestid",str_replace(v("requestid"),old="request",new=""))
t_if(has_field("action"),fields_set("action",str_replace(v("action"),old="action:|action",new="")))
t_if(has_field("uin"),fields_set("uin",str_replace(v("uin"),old="uin:|uin",new="")))
t_if(has_field("TaskId"),fields_set("TaskId",str_replace(v("TaskId"),old="TaskId",new="")))

DSL加工関数の詳細解説

1. 新しいフィールドrequestidを作成し、正規表現を使用して「request 7143a51d-caa4-4a6d-bbf3-771b4ac9e135」をマッチングします。
fields_set("requestid",regex_select(v("__CONTENT__"),regex="request [A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+",index=0,group=0))
2. 新しいフィールドactionを作成し、正規表現を使用して「action: UploadData」または「action DataETL」をマッチングします。元のログには両方の形式が存在するためです。
fields_set("action",regex_select(v("__CONTENT__"),regex="action: \\S+|action \\S+",index=0,group=0))
__CONTENT__フィールドにuinという文字が含まれている場合、uinフィールドを新規作成し、正規表現「uin: \\d+|uin \\d+」を使用してuin: 15432829またはuin 15432829をマッチングします。
t_if(regex_match(v("__CONTENT__"),regex="uin", full=False),fields_set("uin",regex_select(v("__CONTENT__"),regex="uin: \\d+|uin \\d+",index=0,group=0)))
TaskIdという文字が含まれている場合、TaskIdフィールドを新規作成し、正規表現を使用して「TaskId 51d-caa-a6d-bf3-7ac9e」をマッチングします。
t_if(regex_match(v("__CONTENT__"),regex="TaskId", full=False),fields_set("TaskId",regex_select(v("__CONTENT__"),regex="TaskId [A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+",index=0,group=0)))
reqbodyという文字が含まれている場合、requestbodyフィールドを新規作成し、正規表現を使用してreqbody{...}をマッチングします。
t_if(regex_match(v("__CONTENT__"),regex="reqbody", full=False),fields_set("requestbody",regex_select(v("__CONTENT__"),regex="reqbody \\{[^\\}]+\\}")))
3. __CONTENT__フィールドを破棄します。
fields_drop("__CONTENT__")
以上で必要なフィールドを抽出しましたが、正規表現によるマッチングの過程で、不要な文字 action、uin、requestbody、requestid、TaskId が生成されました。そのため、str_replace()関数を使用して不要な文字を削除し、fields_set()関数を使用してフィールド値をリセットします。
1. requestbodyフィールドが存在する場合、フィールド値から不要なreqbody文字を削除します。
t_if(has_field("requestbody"),fields_set("requestbody",str_replace(v("requestbody"),old="reqbody",new="")))
2. フィールド値v("requestid")から不要なrequestidを削除します。各ログにはrequestidが含まれているため、このフィールドの存在確認は行いません。
fields_set("requestid",str_replace(v("requestid"),old="request",new=""))
3. actionフィールドが存在する場合、フィールド値から不要なaction:またはaction文字を削除します。
t_if(has_field("action"),fields_set("action",str_replace(v("action"),old="action:|action",new="")))
4. uinフィールドが存在する場合、フィールド値から不要なuin:またはuinを削除します。
t_if(has_field("uin"),fields_set("uin",str_replace(v("uin"),old="uin:|uin",new="")))
5. TaskIdフィールドが存在する場合、フィールド値から不要なTaskId文字を削除します。
t_if(has_field("tTaskId"),fields_set("TaskId",str_replace(v("TaskId"),old="TaskId",new="")))

加工結果

[
{"action":" Describe","requestid":" 7143a51d-caa4-4a6d-bbf3-771b4ac9e135","requestbody":" {\\"Key\\": \\"config\\",\\"Values\\": \\"appisrunnning\\",\\"Action\\": \\"Describe\\",\\"RequestId\\": \\"7143a51d-caa4-4a6d-bbf3-771b4ac9e135\\",\\"AppId\\": 1302953499,\\"Uin\\": \\"100015432829\\"}","uin":" 15432829"},
{"action":" DataETL","requestid":" 2ade9fc4-2db2-49d8-b3e0-a6ea78ce8d96","uin":" 15432829"},
{"action":" UploadData","requestid":" 6059b946-25b3-4164-ae93-9178c9e73d75","TaskId":" 51d-caa-a6d-bf3-7ac9e"}
]



ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック