すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:SPLステートメントを使用したテキストログの収集

最終更新日:Dec 09, 2024

このトピックでは、Simple Log Service Processing Language (SPL) ステートメントを使用してログを収集する方法について説明します。 この場合、SPL文は処理プラグインと同様に機能します。

背景情報

SPLステートメントとネイティブプラグインの比較

SPLステートメントとデータ解析 (Regexモード) プラグインの比較

サンプルログ:

127.0.0.1 - - [07/Jul/2022:10:43:30 +0800] "POST /PutData?Category=YunOsAccountOpLog" 0.024 18204 200 37 "-" "aliyun-sdk-java"

データ解析 (正規表現モード) プラグイン

SPLステートメント

を指定します。Specify the([\d\.]+) \S + \[(\S +) \S +\] \"(\w +) ([\\"]*)\"([\d\.]+) (\d +) (\d +) (\d +) (\d +) (\d +) (\d +) \" ([^ \\"]*)\" \"([^\"]*)\"正規表現でデータを解析し、ログから次のフィールドを抽出します。ip、time、method、url、request_time、request_length、status、length、ref_url、およびブラウザ. 詳細については、「正規表現モードでのテキストログの収集」をご参照ください。

image

次のSPLステートメントを指定します。* | parse-regexpコンテンツ、'([\d\.]+) \S + \[(\S +) \S +\] \"((\w +) ([\\"]*)\"([\d\.]+) (\d +) (\d +) (\d +) (\d +) \" ([^ \\"]] *)\" \"([^\"]*)\"' をipとして、time、method、url、request_time、request_length、status、length、ref_url、browser | プロジェクト離れたコンテンツ. ステートメントでは、project-away命令はコンテンツフィールドを破棄し、parse-regexp命令は指定されたフィールドを抽出します。

出力プレビュー

{
    "ip": "127.0.0.1",
    "time": "07/Jul/2022:10:43:30",
    "method": "POST",
    "url": "/PutData?Category=YunOsAccountOpLog",
    "request_time": "0.024",
    "request_length": "18204",
    "status": "200",
    "length": "37",
    "ref_url": "-",
    "browser": "aliyun-sdk-java",
    "__time__": "1713184059"
}

SPL文とデータ解析 (区切り文字モード) プラグインの比較

サンプルログ:

127.0.0.1,07/Jul/2022:10:43:30 +0800,POST,PutData Category=YunOsAccountOpLog,0.024,18204,200,37,-,aliyun-sdk-java

データ解析 (区切り文字モード) プラグイン

SPLステートメント

プロセッサタイプパラメーターをデータ解析 (区切り文字モード) に設定します。 [区切り文字] ドロップダウンリストから [カスタム] を選択し、コンマ (,) を指定してフィールドを区切ります。 詳細については、「サーバーからのテキストログの収集」をご参照ください。

image

次のSPLステートメントを指定します。* | parse-csv content as ip, time, method, url, request_time, request_length, status, length, ref_url, browser | project-away content ステートメントでは、project-away命令はcontentフィールドを破棄し、parse-csv命令は指定されたフィールドを抽出します。

出力プレビュー

{
    "ip": "127.0.0.1",
    "time": "07/Jul/2022:10:43:30 +0800",
    "method": "POST",
    "url": "PutData?Category=YunOsAccountOpLog",
    "request_time": "0.024",
    "request_length": "18204",
    "status": "200",
    "length": "37",
    "ref_url": "-",
    "browser": "aliyun-sdk-java",
    "__time__": "1713231487"
}

SPLステートメントとデータ解析 (JSONモード) プラグインの比較

サンプルログ:

{"url": "POST /PutData?Category=YunOsAccountOpLog HTTP/1.1","ip": "10.200.98.220", "user-agent": "aliyun-sdk-java","request": "{\"status\":\"200\",\"latency\":\"18204\"}","time": "07/Jul/2022:10:30:28"}

データ解析 (JSONモード) プラグイン

SPLステートメント

詳細については、「JSONモードでのテキストログの収集」をご参照ください。

image

次のSPLステートメントを指定します。* | parse-json content | project-away content ステートメントでは、project-away命令はcontentフィールドを破棄し、parse-json命令は指定されたフィールドを抽出します。

出力プレビュー

{
    "url": "POST /PutData?Category=YunOsAccountOpLog HTTP/1.1",
    "ip": "10.200.98.220",
    "user-agent": "aliyun-sdk-java",
    "request": "{"status":"200","latency":"18204"}",
    "time": "07/Jul/2022:10:30:28"
}

SPL文とデータ解析 (Regexモード) と時間解析プラグインの組み合わせの比較

サンプルログ:

127.0.0.1 - - [2024-11-05T15:47:05 +0800] "POST /PutData?Category=YunOsAccountOpLog" 0.024 18204 200 37 "-" "aliyun-sdk-java"

データ解析 (Regexモード) と時間解析プラグインの組み合わせ

SPLステートメント

  • を指定します。Specify the([\d\.]+) \S + \[(\S +) \S +\] \"(\w +) ([\\"]*)\"([\d\.]+) (\d +) (\d +) (\d +) (\d +) (\d +) (\d +) \" ([^ \\"]*)\" \"([^\"]*)\"正規表現でデータを解析し、ログから次のフィールドを抽出します。ip、time、method、url、request_time、request_length、status、length、ref_url、およびブラウザ. 詳細については、「正規表現モードでのテキストログの収集」をご参照ください。

    image

  • [時間解析] プラグインの元のフィールドとしてtimeを指定し、時間形式として % Y-% m-% dT % H:% M:% Sを指定します。

    image

次のSPLステートメントを指定します。* | parse-regexpコンテンツ、'([\d\.]+) \S + \[(\S +)\] \"([\\"]*)\"([\w \] *)\" ([\d\.]+) (\d +) (\d +) (\d +) (\d |-) \"([^\"]*)\" \" ([^\"]*)\"' as ip, time、method、url、request_time、request_length、status、length、ref_url、browser | extend ts=date_parse(time、'% Y-% m-% dT % H:% i:% S) | extend __time __= cast(to_unixtime(ts) as INTEGER)-28800 | project-away ts | project-awayコンテンツ. ステートメントでは、project-away命令はコンテンツフィールドを破棄し、parse-regexp命令は指定されたフィールドを抽出し、date_parse命令はログ時刻を解析します。

SPL文とデータ解析 (正規表現モード) プラグインとデータフィルタリングプラグインの組み合わせの比較

サンプルログ:

127.0.0.1 - - [2024-11-05T15:47:05 +0800] "POST /PutData?Category=YunOsAccountOpLog" 0.024 18204 200 37 "-" "aliyun-sdk-java"

データ解析 (正規表現モード) プラグインとデータフィルタリングプラグインの組み合わせ

SPLステートメント

  • を指定します。Specify the([\d\.]+) \S + \[(\S +) \S +\] \"(\w +) ([\\"]*)\"([\d\.]+) (\d +) (\d +) (\d +) (\d +) (\d +) (\d +) \" ([^ \\"]*)\" \"([^\"]*)\"正規表現でデータを解析し、ログから次のフィールドを抽出します。ip、time、method、url、request_time、request_length、status、length、ref_url、およびブラウザ. 詳細については、「正規表現モードでのテキストログの収集」をご参照ください。

    image

  • データフィルタリングプラグインのホワイトリストにstatusおよびmethodフィールドを追加します。

    image

次のSPLステートメントを指定します。* | parse-regexpコンテンツ、'([\d\.]+) \S + \[(\S +) \S +\] \"((\w +) ([\\"]*)\"([\d\.]+) (\d +) (\d +) (\d +) (\d +) \" ([^ \\"]] *)\" \"([^\"]*)\"' をipとして、time、method、url、request_time、request_length、status、length、ref_url、browser | project-away content | ここでregexp_like(method、'^(POST | PUT)$') およびregexp_like(status、'^ 200$'). ステートメントでは、project-away命令はcontentフィールドを破棄し、parse-regexp命令は指定されたフィールドを抽出し、regexp_like関数は指定された正規表現に一致するデータを返します。

出力プレビュー

{
    "ip": "127.0.0.1",
    "time": "2024-11-05T15:47:05",
    "method": "POST",
    "url": "/PutData?Category=YunOsAccountOpLog",
    "request_time": "0.024",
    "request_length": "18204",
    "status": "200",
    "length": "37",
    "ref_url": "-",
    "browser": "aliyun-sdk-java",
    "__time__": "1713238839"
}

SPLステートメントとData Maskingプラグインの比較

サンプルログ:

{"account":"1812213231432969","password":"04a23f38"}

データマスキングプラグイン

SPLステートメント

データマスキングプラグインでマスクするフィールドとしてpasswordを指定します。

image

次のSPLステートメントを指定します。* | parse-regexp content, 'password ":"(\S +)"'as password | extend content=replace(content, password, ' ****** ') ステートメントでは、project-away命令はコンテンツフィールドを破棄し、parse-regexp命令は指定されたフィールドを抽出し、replace関数はマスクされたデータを返します。

出力プレビュー

{
    "content": "{"account":"1812213231432969","password":"******"}"
}

SPL文と拡張プラグインの比較

SPLステートメントとAdd Fieldプラグインの比較

サンプルログ:

this is a test log

フィールドプラグインの追加

SPLステートメント

デフォルトでは、ログはcontentフィールドに保存されます。 [フィールドの追加] プラグインを使用して、service:Aフィールドをログに追加します。

image

次のSPLステートメントを指定します。* | extend service='A' ステートメントでは、extend命令によってservice:Aフィールドが追加されます。

出力プレビュー

{
    "content": "this is a test log",
    "service": "A"
}

SPLステートメントとデータ解析 (JSONモード) プラグインとドロップフィールドプラグインの組み合わせの比較

サンプルログ:

{"key1": 123456, "key2": "abcd"}

データ解析 (JSONモード) とドロップフィールドのプラグインの組み合わせ

SPLステートメント

次のSPLステートメントを指定します。* | parse-json content | project-away content | project-away key1 ステートメントでは、project-away命令はcontentフィールドとkey1フィールドを破棄し、parse-json命令は指定されたフィールドを抽出します。

出力プレビュー

{
    "key2": "abcd"
}

SPLステートメントとデータ解析 (JSONモード) プラグインとフィールドの名前変更プラグインの組み合わせの比較

サンプルログ:

{"key1": 123456, "key2": "abcd"}

データ解析 (JSONモード) とフィールドの名前変更プラグインの組み合わせ

SPLステートメント

次のSPLステートメントを指定します。* | parse-json content | project-away content | project-rename new_key1=key1 ステートメントでは、project-away命令はcontentフィールドを破棄し、parse-json命令は指定されたフィールドを抽出し、project-rename命令はkey1フィールドの名前をnew_key1に変更します。

出力プレビュー

{
    "new_key1": "123456",
    "key2": "abcd"
}

SPLステートメントと、データ解析 (JSONモード) と正規表現を使用したフィルタログ (ログフィールド値の一致) プラグインの組み合わせとの比較

サンプルログ:

{"ip": "10.**.**.**", "method": "POST", "browser": "aliyun-sdk-java"}
{"ip": "10.**.**.**", "method": "POST", "browser": "chrome"}
{"ip": "192.168.**.**", "method": "POST", "browser": "aliyun-sls-ilogtail"}

データ解析 (JSONモード) と正規表現によるフィルタログ (ログフィールド値の一致) プラグインの組み合わせ

SPLステートメント

次のSPLステートメントを指定します。* | parse-json content | project-away content | ここで、regexp_like(ip, '10\.. * ') およびregexp_like(method, 'POST') で、regexp_like(browser, 'aliyun.*') ではありません。 ステートメントでは、project-away命令はcontentフィールドを破棄し、parse-json命令は指定されたフィールドを抽出し、regexp_like関数は指定された正規表現に一致するデータを返します。

出力プレビュー

{
    "ip": "10.**.**.**",
    "method": "POST",
    "browser": "chrome"
}

SPL文とデータ解析 (JSONモード) とフィールド値マッピングプラグインの組み合わせの比較

サンプルログ:

{"_ip_":"192.168.*.*","Index":"900000003"}
{"_ip_":"255.255.**.**","Index":"3"}

データ解析 (JSONモード) とフィールド値マッピングプラグインの組み合わせ

SPLステートメント

次のSPL文を指定します。* | parse-json content | project-away content | extend _processed_ip_= CASE WHEN _ip_ = '127.0.*.* 'THEN 'LocalHost-LocalHost' WHEN _ip_ = '192.168.* *' THEN 'default login' ELSE 'Not Detected END ステートメントでは、project-away命令はcontentフィールドを破棄し、parse-json命令は指定されたフィールドを抽出し、extend命令はフィールドを追加します。

出力プレビュー

{
    "_ip_": "192.168.*.*",
    "Index": "900000003",
    "_processed_ip_": "default login"
}

SPLステートメントとString Replacementプラグインの比較

サンプルログ:

hello,how old are you?  nice to meet you

文字列置換プラグイン

SPLステートメント

文字列置換プラグインを使用して、何歳ですか? をNULLにします。

image

次のSPLステートメントを指定します。* | extend content=replace(content, 'how old are you?', '') ステートメントでは、拡張命令は何歳ですか? をNULLにします。

出力プレビュー

{
    "content": "hello, nice to meet you"
}

SPLステートメントとデータエンコーディングプラグインの比較

サンプルログ:

this is a test log

Base64エンコーディング

BASE64 (エンコード) プラグイン

SPLステートメント

詳細については、「Base64エンコーディング」をご参照ください。

次のステートメントを指定します。* | extend content1=to_base64(cast(content as varbinary)) ステートメントでは、extend命令はcontent1フィールドを追加し、to_base64関数はBase64のデータをエンコードします。

出力プレビュー
{
    "content": "this is a test log",
    "content1": "dGhpcyBpcyBhIHRlc3QgbG9n"
}

MD5エンコーディング

MD5プラグイン

SPLステートメント

詳細については、「MD5エンコーディング」をご参照ください。

次のSPLステートメントを指定します。* | extend test=lower(to_hex(md5(cast(content as varbinary)))) 。 ステートメントでは、extend命令がtestフィールドを追加し、md5関数がデータのMD5ハッシュ値を計算します。

出力プレビュー
{
    "content": "this is a test log",
    "content1": "4f3c93e010f366eca78e00dc1ed08984"
}

新しい機能

数理計算

  • サンプルログ

    4
  • SPLステートメント

    cast関数は、フィールドの値を指定されたデータ型に変換します。 power、round、およびsqrt関数の詳細については、「数学計算関数」をご参照ください。

    *
    | extend val = cast(content as double)
    | extend power_test = power(val, 2)
    | extend round_test = round(val)
    | extend sqrt_test = sqrt(val)
  • 出力プレビュー

    {
        "content": "4",
        "power_test": 16.0,
        "round_test": 4.0,
        "sqrt_test": 2.0,
        "val": 4.0
    }

URLベースの計算

URLエンコードとデコード

  • サンプルログ

    https://homenew.console.aliyun.com/home/dashboard/ProductAndService
  • SPLステートメント

    url_encodeおよびurl_decode関数の詳細については、「URL関数」をご参照ください。

    *
    | extend encoded = url_encode(content)
    | extend decoded = url_decode(encoded)
  • 出力プレビュー

    {
        "content": "https://homenew.console.aliyun.com/home/dashboard/ProductAndService",
        "decoded": "https://homenew.console.aliyun.com/home/dashboard/ProductAndService",
        "encoded": "https%3A%2F%2Fhomenew.console.aliyun.com%2Fhome%2Fdashboard%2FProductAndService"
    }

URL抽出

  • サンプルログ

    https://sls.console.aliyun.com:443/lognext/project/dashboard-all/logsearch/nginx-demo?accounttraceid=d6241a173f88471c91d3405cda010ff5ghdw
  • SPLステートメント

    次のSPLステートメントで使用される関数の詳細については、「URL関数」をご参照ください。

    *
    | extend host = url_extract_host(content)
    | extend query = url_extract_query(content)
    | extend path = url_extract_path(content) 
    | extend protocol = url_extract_protocol(content) 
    | extend port = url_extract_port(content) 
    | extend param = url_extract_parameter(content, 'accounttraceid')
  • 出力プレビュー

    {
        "content": "https://sls.console.aliyun.com:443/lognext/project/dashboard-all/logsearch/nginx-demo?accounttraceid=d6241a173f88471c91d3405cda010ff5ghdw",
        "host": "sls.console.aliyun.com",
        "param": "d6241a173f88471c91d3405cda010ff5ghdw",
        "path": "/lognext/project/dashboard-all/logsearch/nginx-demo",
        "port": "443",
        "protocol": "https",
        "query": "accounttraceid=d6241a173f88471c91d3405cda010ff5ghdw"
    }

比較および論理演算子

  • サンプルログ

    {"num1": 199, "num2": 10, "num3": 9}
  • SPLステートメント

    cast関数はフィールドの値を指定されたデータ型に変換し、parse-json命令は指定されたフィールドを抽出します。

    *
    | parse-json content
    | extend compare_result = cast(num1 as double) > cast(num2 as double) AND cast(num2 as double) > cast(num3 as double
  • 出力プレビュー

    {
        "compare_result": "true",
        "content": "{"num1": 199, "num2": 10, "num3": 9}",
        "num1": "199",
        "num2": "10",
        "num3": "9"
    }