#json-parser #logging #solana #json #parser #log-error #json-response

sologger_log_context

提供将 Solana RPC 输出的原始日志转换为指定程序 ID 的结构化日志的功能

3 个版本

0.1.2 2024年3月27日
0.1.1 2024年1月26日
0.1.0 2023年10月11日

#18魔法豆

Download history 7/week @ 2024-06-02 11/week @ 2024-06-09 2/week @ 2024-06-16 3/week @ 2024-06-30 36/week @ 2024-07-07 9/week @ 2024-07-21 31/week @ 2024-07-28

每月 77 次下载
sologger_log_transformer 中使用

Apache-2.0

92KB
1K SLoC

sologger-log-context

概述

此库提供将 Solana RPC 输出的原始日志转换为指定程序 ID 的结构化日志的功能。

用法

    //Provide the ProgramSelector with the Program IDs for which you want to parse logs.
    //If you want to parse logs for all programs, use ProgramsSelector::new(&["*".to_string()])
    let programs_selector = ProgramsSelector::new(&["9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7".to_string()]);
    //Provide the raw logs, transfer error, programs selector, slot, and signature to the LogContext::parse_logs function.
    let log_contexts = LogContext::parse_logs(&logs, "".to_string(), &programs_selector, 1, "12345".to_string());

这些日志上下文的 json 架构可以在此处找到: LogContext-schema

例如,如果我们从 Solana RPC 获取了一组原始日志,我们可以使用 LogContext::parse_logs 函数将它们解析为结构化日志。第一个参数是原始日志,第二个参数是程序 ID,第三个参数是程序选择器,第四个参数是槽,第五个参数是签名。

LogContext::parse_logs 函数返回一个 LogContexts 向量。每个 LogContext 包含一个 LogMessages 向量。每个 LogMessage 包含一个 LogFields 向量。每个 LogField 包含一个键和一个值。

以下是从 Solana RPC 获取的原始日志的示例

Program 9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7 invoke [1]
Program log: Instruction: Initialize
Program 11111111111111111111111111111111 invoke [2]
Program 11111111111111111111111111111111 success
Program log: Initialized new event. Current value
Program 9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7 consumed 59783 of 200000 compute units
Program 9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7 success
Program AbcdefGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]
Program log: Create
Program AbcdefGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 5475 of 200000 compute units
Program failed to complete: Invoked an instruction with data that is too large (12178014311288245306 > 10240)
Program AbcdefGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL failed: Program failed to complete

以下是从 LogContext::parse_logs 函数返回的结构化日志的示例

{
  "log_messages":[
    "Instruction: Initialize",
    "Initialized new event. Current value"
  ],
  "data_logs":[

  ],
  "raw_logs":[
    "Program 9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7 invoke [1]",
    "Program log: Instruction: Initialize",
    "Program log: Initialized new event. Current value",
    "Program 9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7 consumed 59783 of 200000 compute units",
    "Program 9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7 success"
  ],
  "errors":[

  ],
  "transaction_error":"",
  "program_id":"9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7",
  "parent_program_id":"",
  "depth":1,
  "id":0,
  "instruction_index":0,
  "invoke_result":"",
  "slot":1,
  "signature":"12345"
}
{
  "log_messages":[

  ],
  "data_logs":[

  ],
  "raw_logs":[
    "Program 11111111111111111111111111111111 invoke [2]",
    "Program 11111111111111111111111111111111 success"
  ],
  "errors":[

  ],
  "transaction_error":"",
  "program_id":"11111111111111111111111111111111",
  "parent_program_id":"9RX7oz3WN5VRTqekBBHBvEJFVMNRnrCmVy7S6B6S5oU7",
  "depth":2,
  "id":1,
  "instruction_index":0,
  "invoke_result":"",
  "slot":1,
  "signature":"12345"
}
{
  "log_messages":[
    "Create"
  ],
  "data_logs":[

  ],
  "raw_logs":[
    "Program AbcdefGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]",
    "Program log: Create",
    "Program AbcdefGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 5475 of 200000 compute units",
    "Program failed to complete: Invoked an instruction with data that is too large (12178014311288245306 > 10240)",
    "Program AbcdefGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL failed: Program failed to complete"
  ],
  "errors":[
    "Invoked an instruction with data that is too large (12178014311288245306 > 10240)",
    "Program failed to complete"
  ],
  "transaction_error":"",
  "program_id":"AbcdefGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
  "parent_program_id":"",
  "depth":1,
  "id":2,
  "instruction_index":1,
  "invoke_result":"",
  "slot":1,
  "signature":"12345"
}

其他用法

LogContext 还提供从日志行检索特定信息的实用工具。

  • get_program_data:返回提供的日志中提到的数据(对于以 "Program data: " 前缀的日志)
  • parse_logs_from_string:解析提供的有效负载并返回 LogContexts 向量。在这种情况下,有效负载是来自 Solana RPC 日志订阅端点的原始 JSON 响应字符串。
  • has_errors:如果日志包含程序错误,则返回 true

技术细节

原始日志的解析使用正则表达式完成。正则表达式在LogContext::get_log_regex函数中定义。正则表达式定义如下

regex-vis

(?<programInvoke>^Program (?<invokeProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) invoke \[(?<level>\d+)\]$)|(?<programSuccessResult>^Program (?<successResultProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) success$)|(?<programFailedResult>^Program (?<failedResultProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) failed: (?<failedResultErr>.*)$)|(?<programCompleteFailedResult>^Program failed to complete: (?<failedCompleteError>.*)$)|(?<programLog>^^Program log: (?<logMessage>.*)$)|(?<programData>^Program data: (?<data>.*)$)|(?<programConsumed>^Program (?<consumedProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) consumed (?<consumedComputeUnits>\d*) of (?<allComputedUnits>\d*) compute units$)|(?<programConsumption>^^Program consumption: (?<computeUnitsRemaining>.*)$)|(?<logTruncated>^Log truncated$)|(?<programReturn>^Program return: (?<returnProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) (?<returnMessage>.*)$)

LogContext尝试遍历从Solana websocket日志订阅帧返回的原始日志,或者从特定交易或区块检索的日志组。如果提供的日志顺序混乱或不是来自包含单元(如区块、交易或websocket帧),则LogContext很可能会失败。

依赖关系

~3–5MB
~91K SLoC