Crates.io | sologger_log_context |
lib.rs | sologger_log_context |
version | 0.2.1 |
source | src |
created_at | 2023-10-11 13:52:56.865624 |
updated_at | 2024-10-16 18:59:42.866772 |
description | Provides functionality to turn raw logs output by Solana RPCs into structured logs for specified program IDs |
homepage | |
repository | https://github.com/brytelands/sologger |
max_upload_size | |
id | 1000274 |
size | 1,360,380 |
Overview
This library provides functionality to turn raw logs output by Solana RPCs into structured logs for specified program IDs.
Usage
//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());
These json schema for the log context can be found here: LogContext-schema
For example, if we have a list of raw logs retrieved from the Solana RPC, we can parse them into structured logs using the LogContext::parse_logs function. The first parameter is the raw logs, the second parameter is the program ID, the third parameter is the programs selector, the fourth parameter is the slot, and the fifth parameter is the signature.
The LogContext::parse_logs function returns a vector of LogContexts. Each LogContext contains a vector of LogMessages. Each LogMessage contains a vector of LogFields. Each LogField contains a key and a value.
Here is an example of raw logs retrieved from the 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
Here is an example of the structured logs that are returned from the LogContext::parse_logs function:
{
"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"
}
Additional Usage
The LogContext also provides utility to retrieve specific information from a log line.
Technical Details
The parsing of the raw logs is done using a regular expression. The regular expression is defined in the LogContext::get_log_regex function. The regular expression is defined as follows:
(?<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>.*)$)
The LogContext attempts to loop through the raw logs returned from the Solana websocket log subscription frames, or groups of logs retrieved from a specific transaction or block. If logs are provided that are out of order or not from a contained unit such as a block, transaction or websocket frame, then the LogContext will most likely fail.