-
-
Save RadoslavGeorgiev/71660cc233926e9f121ab07b5f248304 to your computer and use it in GitHub Desktop.
Parser Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// phpcs:disable | |
require_once __DIR__ . '/../../../wp-load.php'; | |
class Log { | |
public string $category; | |
public string $date; | |
public string $entry = ''; | |
public $json = null; | |
function __construct( $category, $date ) { | |
$this->category = $category; | |
$this->date = $date; | |
} | |
function add( string $entry ) { | |
if ( strlen( $this->entry ) > 0 ) { | |
$this->entry .= "\n"; | |
} | |
$this->entry .= $entry; | |
} | |
public function process() { | |
} | |
} | |
class Request { | |
public string $id; | |
public array $logs = []; | |
public bool $failed = false; | |
function __construct( string $id ) { | |
$this->id = $id; | |
} | |
function add_line( int $counter, string $category, string $date, string $entry ) { | |
if ( ! isset( $this->logs[ $counter ] ) ) { | |
$this->logs[ $counter ] = new Log( $category, $date ); | |
} | |
$this->logs[ $counter ]->add( $entry ); | |
} | |
public function process() { | |
// Ignore log counters. | |
$this->logs = array_values( $this->logs ); | |
if ( in_array( 'ERROR', wp_list_pluck( $this->logs, 'category') ) ) { | |
$this->failed = true; | |
} | |
foreach ( $this->logs as $log ) { | |
$log->process(); | |
} | |
} | |
} | |
class Log_Requests { | |
public array $requests = []; | |
function get_request( string $id ): Request { | |
$last_request = isset( $this->requests[ count( $this->requests ) - 1 ] ) | |
? $this->requests[ count( $this->requests ) - 1 ] | |
: null; | |
if ( $last_request && $last_request->id === $id ) { | |
return $last_request; | |
} | |
$request = new Request( $id ); | |
$this->requests[] = $request; | |
return $request; | |
} | |
function process(): void { | |
foreach ( $this->requests as $request ) { | |
$request->process(); | |
} | |
} | |
} | |
$requests = new Log_Requests(); | |
$filename = 'woopayments-2024-12-06-421f0e1c50b72a70a5fc34aa2d7d7089.log'; | |
$file = fopen( WP_CONTENT_DIR . '/uploads/wc-logs/' . $filename, 'r' ); | |
while ( $line = fgets( $file ) ) { | |
if ( ! preg_match( '/^(?P<date>[^\s]+)\s(?P<type>\w+)\s(?P<request_id>\w+)-(?P<counter>\d+)\s(?P<entry>.*)$/i', $line, $matches ) ) { | |
echo "Line could not be parsed."; | |
exit; | |
} | |
$request_id = $matches['request_id']; | |
$counter = intval( $matches['counter'] ); | |
$date = $matches['date']; | |
$category = $matches['type']; | |
$entry = $matches['entry']; | |
$request = $requests->get_request( $request_id ); | |
$request->add_line( $counter, $category, $date, $entry ); | |
} | |
fclose( $file ); | |
$requests->process(); | |
foreach ( $requests->requests as $request ) { | |
echo '<table border="1" style="margin-bottom:3em">'; | |
$emoji = $request->failed ? '❌' : '✅'; | |
echo '<tr' . ( $request->failed ? ' style="color:red"' : '') . '> | |
<th>' . $emoji . '</th> | |
<th>' . $request->id . '</th> | |
<th>' . $request->logs[0]->date . '</th> | |
</tr>'; | |
foreach ( $request->logs as $log ) { | |
echo '<tr' . ( 'ERROR' === $log->category ? ' style="color:red"' : '') . '> | |
<td>' . $log->category . '</td> | |
<td>' . $log->date . '</td> | |
<td><pre><code>' . $log->entry . '</code></pre></td> | |
</tr>'; | |
} | |
echo '</table>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment