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

sologger_log_context

Provides functionality to turn raw logs output by Solana RPCs into structured logs for specified program IDs

3 releases

0.1.2 Mar 27, 2024
0.1.1 Jan 26, 2024
0.1.0 Oct 11, 2023

#52 in Magic Beans

Download history 23/week @ 2024-01-25 3/week @ 2024-02-15 21/week @ 2024-02-22 13/week @ 2024-02-29 3/week @ 2024-03-07 7/week @ 2024-03-14 72/week @ 2024-03-21 81/week @ 2024-03-28 18/week @ 2024-04-04 3/week @ 2024-04-11

174 downloads per month
Used in sologger_log_transformer

Apache-2.0

92KB
1K SLoC

sologger-log-context

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.

  • get_program_data: Returns the data mentioned in the provided log (for logs prefixed with "Program data: ")
  • parse_logs_from_string: Parses the provided payload and returns a vector of LogContexts. The payload in this case is the raw JSON response as a string from the Solana RPC log_subscription endpoint.
  • has_errors: Returns true if the log contains a program error

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:

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>.*)$)

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.

Dependencies

~3–5MB
~89K SLoC