Skip to content

Commit

Permalink
chore: improve logging replacing reusables with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
abaicus committed Nov 9, 2023
1 parent ddc7de1 commit d0e66c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion assets/src/dashboard/parts/connected/settings/Lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const Lazyload = ({
<div className="relative inline-block">
<Button
disabled={isLoading}
className="gap-3 my-2 py-3 h-auto rounded text-inherit border-gray-400 border-2 border-solid font-medium"
className="gap-3 mt-2 py-3 h-auto rounded text-inherit border-gray-400 border-2 border-solid font-medium"
onClick={() => {
setPhPicker( ! phPicker );
}}
Expand Down
13 changes: 9 additions & 4 deletions inc/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
class Optml_Logger {

const LOG_TYPE_OFFLOAD = 'offload';
const LOG_TYPE_ROLLBACK = 'rollback';

const LOG_SEPARATOR = '==========';

/**
* Upload directory.
*
Expand All @@ -18,8 +23,8 @@ class Optml_Logger {
* @var array
*/
private $log_filenames = [
'offload' => 'offload.log',
'rollback' => 'rollback.log',
self::LOG_TYPE_OFFLOAD => 'offload.log',
self::LOG_TYPE_ROLLBACK => 'rollback.log',
];

/**
Expand Down Expand Up @@ -212,7 +217,7 @@ public function fetch_logs_ajax_handler() {
}

// Check for necessary parameters
if ( ! isset( $_REQUEST['type'] ) || ! in_array( $_REQUEST['type'], ['offload', 'rollback'], true ) ) {
if ( ! isset( $_REQUEST['type'] ) || ! in_array( $_REQUEST['type'], array_keys( $this->log_filenames ), true ) ) {
wp_die( __( 'Invalid log type', 'optimole-wp' ) );
}

Expand All @@ -226,7 +231,7 @@ public function fetch_logs_ajax_handler() {
$type = $_REQUEST['type'];

// Fetch the logs
$logs = $this->get_log( $type, $lines, '==========' );
$logs = $this->get_log( $type, $lines, self::LOG_SEPARATOR );

// Return the logs
echo $logs;
Expand Down
40 changes: 20 additions & 20 deletions inc/media_offload.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ public function rollback_and_update_images( $image_ids ) {
do_action( 'optml_log', ' error get url' );
}

self::$instance->logger->add_log( 'rollback', 'Image ID: ' . $id . ' has error getting URL.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_ROLLBACK, 'Image ID: ' . $id . ' has error getting URL.' );
continue;
}

Expand All @@ -779,7 +779,7 @@ public function rollback_and_update_images( $image_ids ) {
do_action( 'optml_log', ' download_url error ' );
}

self::$instance->logger->add_log( 'rollback', 'Image ID: ' . $id . ' has error getting URL.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_ROLLBACK, 'Image ID: ' . $id . ' has error getting URL.' );
continue;
}

Expand All @@ -792,7 +792,7 @@ public function rollback_and_update_images( $image_ids ) {
}
update_post_meta( $id, 'optimole_rollback_error', 'true' );

self::$instance->logger->add_log( 'rollback', 'Image ID: ' . $id . ' has invalid extension.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_ROLLBACK, 'Image ID: ' . $id . ' has invalid extension.' );
continue;
}

Expand Down Expand Up @@ -832,7 +832,7 @@ public function rollback_and_update_images( $image_ids ) {
}
update_post_meta( $id, 'optimole_rollback_error', 'true' );

self::$instance->logger->add_log( 'rollback', 'Image ID: ' . $id . ' faced wp_handle_sideload error.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_ROLLBACK, 'Image ID: ' . $id . ' faced wp_handle_sideload error.' );
continue;
}

Expand Down Expand Up @@ -889,7 +889,7 @@ public function rollback_and_update_images( $image_ids ) {
}
$success_back++;

self::$instance->logger->add_log( 'rollback', 'Image ID: ' . $id . ' has been rollbacked.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_ROLLBACK, 'Image ID: ' . $id . ' has been rolled back.' );

$original_url = self::get_original_url( $id );
if ( $original_url === false ) {
Expand Down Expand Up @@ -1120,7 +1120,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', $meta );
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has invalid meta.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has invalid meta.' );
return $meta;
}
if ( false === Optml_Filters::should_do_image( $meta['file'], self::$filters[ Optml_Settings::FILTER_TYPE_OPTIMIZE ][ Optml_Settings::FILTER_FILENAME ] ) ) {
Expand All @@ -1134,7 +1134,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', 'error getting original url' );
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has invalid original url.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has invalid original url.' );
return $meta;
}

Expand Down Expand Up @@ -1167,7 +1167,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', 'missing file' );
do_action( 'optml_log', $local_file );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has missing file.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has missing file.' );
return $meta;
}

Expand All @@ -1176,7 +1176,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', 'invalid extension' );
do_action( 'optml_log', $extension );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has invalid extension.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has invalid extension.' );
return $meta;
}
if ( false === Optml_Filters::should_do_extension( self::$filters[ Optml_Settings::FILTER_TYPE_OPTIMIZE ][ Optml_Settings::FILTER_EXT ], $extension ) ) {
Expand All @@ -1196,7 +1196,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
}
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has invalid signed url.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has invalid signed url.' );
return $meta;
}
$decoded_response = json_decode( $generate_url_response['body'], true );
Expand All @@ -1208,7 +1208,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
}
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has invalid table id or upload url.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has invalid table id or upload url.' );
return $meta;
}
$table_id = $decoded_response['tableId'];
Expand All @@ -1223,7 +1223,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', $local_file );
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has missing file.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has missing file.' );
return $meta;
}
if ( $upload_signed_url !== 'found_resource' ) {
Expand All @@ -1236,7 +1236,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', $result );
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has upload error.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has upload error.' );
return $meta;
}
$file_size = filesize( $local_file );
Expand All @@ -1259,7 +1259,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', $result_update );
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has dynamo update error.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has dynamo update error.' );
return $meta;
}
}
Expand All @@ -1277,7 +1277,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
$request->call_upload_api( $original_url, 'true', $table_id );
update_post_meta( $attachment_id, 'optimole_offload_error', 'true' );

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has optimization error.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has optimization error.' );
return $meta;
}
unlink( $local_file );
Expand Down Expand Up @@ -1312,7 +1312,7 @@ public function generate_image_meta( $meta, $attachment_id ) {
do_action( 'optml_log', 'success offload' );
}

self::$instance->logger->add_log( 'offload', 'Image ID: ' . $attachment_id . ' has been offloaded.' );
self::$instance->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Image ID: ' . $attachment_id . ' has been offloaded.' );
$attachment_page_id = wp_get_post_parent_id( $attachment_id );

if ( $attachment_page_id !== false && $attachment_page_id !== 0 ) {
Expand Down Expand Up @@ -1614,8 +1614,8 @@ public static function get_image_count( $action, $refresh, $images = [] ) {
self::$instance->settings->update( $option, $in_progress ? 'enabled' : 'disabled' );

$type = 'offload_images' === $action ? 'offload' : 'rollback';
self::$instance->logger->add_log( $type, '==========' );
self::$instance->logger->add_log( $type, 'Starter with a total count of ' . intval( $count ) . '.' );
self::$instance->logger->add_log( $type, Optml_Logger::LOG_SEPARATOR );
self::$instance->logger->add_log( $type, 'Started with a total count of ' . intval( $count ) . '.' );
self::record_process_meta( $count );

if ( true === $in_progress ) {
Expand All @@ -1639,8 +1639,8 @@ public static function get_image_count( $action, $refresh, $images = [] ) {
self::$instance->settings->update( $option, $in_progress ? 'enabled' : 'disabled' );

$type = 'offload_images' === $action ? 'offload' : 'rollback';
self::$instance->logger->add_log( $type, '==========' );
self::$instance->logger->add_log( $type, 'Starter with a total count of ' . intval( $count ) . '.' );
self::$instance->logger->add_log( $type, Optml_Logger::LOG_SEPARATOR );
self::$instance->logger->add_log( $type, 'Started with a total count of ' . intval( $count ) . '.' );
self::record_process_meta( $count );

if ( true === $in_progress ) {
Expand Down
4 changes: 2 additions & 2 deletions tests/test-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function test_instance_creation() {
* Test adding a log.
*/
public function test_add_log() {
$this->assertTrue( $this->logger->add_log( 'offload', 'Test offload message' ) );
$this->assertTrue( $this->logger->add_log( 'rollback', 'Test rollback message' ) );
$this->assertTrue( $this->logger->add_log( Optml_Logger::LOG_TYPE_OFFLOAD, 'Test offload message' ) );
$this->assertTrue( $this->logger->add_log( Optml_Logger::LOG_TYPE_ROLLBACK, 'Test rollback message' ) );
}

/**
Expand Down

0 comments on commit d0e66c1

Please sign in to comment.