Skip to content

[WIP] Add a logger #1124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: development
Choose a base branch
from
185 changes: 185 additions & 0 deletions css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,7 @@ li.draggable-item .components-panel__body-toggle.components-button{
border-bottom: 0;
padding: 24px 0 0;
}

.fz-fallback-images {
display: flex;
flex-wrap: wrap;
Expand Down Expand Up @@ -2673,4 +2674,188 @@ button.feedzy-action-button {
width: 100%;
cursor: pointer;
height: unset;
}

/* Feedzy Logs */
.fz-logs {
padding: 10px;
margin: 10px 0;
}

.fz-logs h3 {
margin-bottom: 10px;
font-size: 1.25em;
color: #333;
}

/* Logs view container */
.fz-logs-view {
display: flex;
flex-direction: column;
gap: 2px;
}

/* Individual log container */
.fz-log-container {
display: flex;
gap: 10px;
background-color: #fff;
border: 1px solid #e0e0e0;
border-left-width: 5px;
padding: 8px 10px;
transition: background-color 0.1s ease;
font-size: 0.875em;
}

.fz-log-container:hover {
background-color: #f5f5f5;
}

/* Left section */
.fz-log-container__left {
flex: 0 0 50%;
min-width: 0;
}

/* Header with level and date */
.fx-log-container__header {
display: flex;
align-items: center;
gap: 4px;
margin-bottom: 4px;
font-size: 1.1em;
}

/* Log level styling */
.fx-log-container__header > *:first-child {
padding: 2px 6px;
border-radius: 3px;
font-size: 0.7em;
font-weight: 600;
letter-spacing: 0.3px;
text-transform: uppercase;
min-width: 50px;
text-align: center;
}

/* Date styling */
.fz-log-container__date {
color: #757575;
font-size: 0.85em;
}

/* Message styling */
.fz-log-container__message {
color: #212529;
line-height: 1.3;
word-wrap: break-word;
overflow-wrap: break-word;
font-size: 1em;
margin-top: 10px;
}

/* Right section - Context */
.fz-log-container__right {
flex: 1;
min-width: 0;
}

/* Context styling - compact */
.fz-log-container__context {
background-color: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 5px;
padding: 6px 8px;
font-family: 'Courier New', Consolas, Monaco, monospace;
font-size: 0.9em;
line-height: 1.3;
color: #495057;
white-space: pre-wrap;
word-break: break-all;
overflow-wrap: break-word;
}

.fz-log-container--error {
border-left-color: red;
}

.fz-log-container--info {
border-left-color: blue;
}

.fz-log-container--debug {
border-left-color: green;
}

.fz-log-container--warning {
border-left-color: yellow;
}

.fz-log-container--critical {
border-left-color: violet;
}

.fz-logs-header {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
margin-bottom: 1rem;
}

.fz-logs-header-actions {
display: flex;
flex-direction: row;
gap: 0.5rem;
}

.fz-logs-header-title {
display: flex;
flex-direction: row;
gap: 0.5rem;
align-items: baseline;
}

.fz-block__column {
display: flex;
flex-direction: column;
gap: 1rem;
}

.fz-group__row {
display: flex;
flex-direction: row;
gap: 1rem;
align-items: center;
}

.fz-group__left {
justify-content: flex-end;
}

.btn-outline-primary.fz-is-destructive {
color: #cc1818;
border-color: #cc1818;
}

.btn-outline-primary.fz-is-destructive:hover {
color: #cc1818;
border-color: #cc1818;
box-shadow: inset 0 0 0 1px #cc1818, inset 0 0 0 2px #f7f9fd;
background: #fdf7f7;
}

.fz-log-file-size-wrapper {
display: flex;
flex-direction: row;
gap: 0.5rem;
align-items: baseline;
}

.fz-log-file-size-wrapper .dashicons {
font-size: 1.9em;
}

.fz-hidden {
display: none;
}
31 changes: 2 additions & 29 deletions feedzy-rss-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ function feedzy_register_parrot( $plugins ) {
* @param string $type Error type.
* @param string $file File where the event occurred.
* @param int $line Line number where the event occurred.
*
* @deprecated 5.1.0 Use Feedzy_Rss_Feeds_Log instead.
*/
function feedzy_themeisle_log_event( $name, $msg, $type, $file, $line ) {
if ( FEEDZY_NAME === $name ) {
Expand All @@ -279,35 +281,6 @@ function feedzy_themeisle_log_event( $name, $msg, $type, $file, $line ) {
}
}

/**
* Store import job errors in metadata.
*
* @param string $name Name.
* @param string $msg Error message.
* @param string $type Error type.
*
* @return void
*/
function feedzy_import_job_logs( $name, $msg, $type ) {
if ( ! in_array( $type, apply_filters( 'feedzy_allowed_store_log_types', array( 'error' ) ), true ) ) {
return;
}
if ( ! wp_doing_ajax() || wp_doing_cron() ) {
return;
}
if ( apply_filters( 'feedzy_skip_store_error_logs', false ) ) {
return;
}
global $themeisle_log_event;

if ( ! empty( $themeisle_log_event ) && count( $themeisle_log_event ) >= 200 ) {
return;
}

$themeisle_log_event[] = $msg;
}
add_action( 'themeisle_log_event', 'feedzy_import_job_logs', 20, 3 );

add_filter(
'feedzy_rss_feeds_float_widget_metadata',
function () {
Expand Down
57 changes: 51 additions & 6 deletions includes/abstract/feedzy-rss-feeds-admin-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ public function rest_route() {
),
)
);

Feedzy_Rss_Feeds_Log::get_instance()->register_endpoints();
}

/**
Expand Down Expand Up @@ -861,7 +863,14 @@ function ( $time ) use ( $cache_time ) {
if ( ! $wp_filesystem->exists( $dir ) ) {
$done = $wp_filesystem->mkdir( $dir );
if ( false === $done ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::error(
sprintf( 'Unable to create SimplePie cache directory: %s', $dir ),
array(
'feed_url' => $feed_url,
'cache' => $cache,
'sc' => $sc,
)
);
}
}
$feed->set_cache_location( $dir );
Expand Down Expand Up @@ -902,14 +911,35 @@ function ( $time ) use ( $cache_time ) {
}

if ( ! empty( $error ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Error while parsing feed: %s', $error ), 'error', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::error(
sprintf( 'Error while parsing feed: %s', $error ),
array(
'feed_url' => $feed_url,
'cache' => $cache,
'sc' => $sc,
)
);

// curl: (60) SSL certificate problem: unable to get local issuer certificate.
if ( strpos( $error, 'SSL certificate' ) !== false ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Got an SSL Error (%s), retrying by ignoring SSL', $error ), 'debug', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::error(
sprintf( 'Got an SSL Error (%s), retrying by ignoring SSL', $error ),
array(
'feed_url' => $feed_url,
'cache' => $cache,
'sc' => $sc,
)
);
$feed = $this->init_feed( $feed_url, $cache, $sc, false );
} elseif ( is_string( $feed_url ) || ( is_array( $feed_url ) && 1 === count( $feed_url ) ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, 'Trying to use raw data', 'debug', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::debug(
sprintf( 'Using raw data for feed: %s', $feed_url ),
array(
'cache' => $cache,
'sc' => $sc,
)
);

$data = wp_remote_retrieve_body( wp_safe_remote_get( $feed_url, array( 'user-agent' => $default_agent ) ) );
$cloned_feed->set_raw_data( $data );
$cloned_feed->init();
Expand All @@ -920,7 +950,14 @@ function ( $time ) use ( $cache_time ) {
$feed = $cloned_feed;
}
} else {
do_action( 'themeisle_log_event', FEEDZY_NAME, 'Cannot use raw data as this is a multifeed URL', 'debug', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::debug(
'Cannot use raw data as this is a multifeed URL',
array(
'feed_url' => $feed_url,
'cache' => $cache,
'sc' => $sc,
)
);
}
}
return $feed;
Expand Down Expand Up @@ -1934,7 +1971,15 @@ public function feedzy_image_encode( $img_url ) {
}

$filtered_url = apply_filters( 'feedzy_image_encode', esc_url( $img_url ), $img_url );
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Changing image URL from %s to %s', $img_url, $filtered_url ), 'debug', __FILE__, __LINE__ );

Feedzy_Rss_Feeds_Log::debug(
'Change featured image via feedzy_image_encode',
array(
'old_url' => $img_url,
'new_url' => $filtered_url,
)
);

return $filtered_url;
}

Expand Down
30 changes: 27 additions & 3 deletions includes/admin/feedzy-rss-feeds-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,10 @@ function ( $item ) {
$settings['general']['auto-categories'] = array_values( $auto_categories );
$settings['general']['feedzy-telemetry'] = isset( $_POST['feedzy-telemetry'] ) ? absint( wp_unslash( $_POST['feedzy-telemetry'] ) ) : '';
$settings['general']['feedzy-delete-media'] = isset( $_POST['feedzy-delete-media'] ) ? absint( wp_unslash( $_POST['feedzy-delete-media'] ) ) : '';

$settings['logs']['level'] = isset( $_POST['logs-logging-level'] ) ? sanitize_text_field( wp_unslash( $_POST['logs-logging-level'] ) ) : '';
$settings['logs']['email'] = isset( $_POST['feedzy-email-error-address'] ) ? sanitize_email( wp_unslash( $_POST['feedzy-email-error-address'] ) ) : '';
$settings['logs']['send_email_report'] = isset( $_POST['feedzy-email-error-enabled'] ) ? absint( wp_unslash( $_POST['feedzy-email-error-enabled'] ) ) : '';
break;
case 'headers':
$settings['header']['user-agent'] = isset( $_POST['user-agent'] ) ? sanitize_text_field( wp_unslash( $_POST['user-agent'] ) ) : '';
Expand Down Expand Up @@ -1247,7 +1251,13 @@ private function add_proxy( $url ) {
if ( $settings && isset( $settings['proxy'] ) && is_array( $settings['proxy'] ) && ! empty( $settings['proxy'] ) ) {
// if even one constant is defined, escape.
if ( defined( 'WP_PROXY_HOST' ) || defined( 'WP_PROXY_PORT' ) || defined( 'WP_PROXY_USERNAME' ) || defined( 'WP_PROXY_PASSWORD' ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, 'Some proxy constants already defined; ignoring proxy settings', 'info', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::info(
'Some proxy constants already defined; ignoring proxy settings',
array(
'url' => $url,
'settings' => $settings['proxy'],
)
);

return;
}
Expand Down Expand Up @@ -1304,7 +1314,14 @@ public function http_request_args( $args ) {
public function add_user_agent( $ua ) {
$settings = apply_filters( 'feedzy_get_settings', null );
if ( $settings && isset( $settings['header']['user-agent'] ) && ! empty( $settings['header']['user-agent'] ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'Override user-agent from %s to %s', $ua, $settings['header']['user-agent'] ), 'info', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::info(
'Overriding user-agent',
array(
'old_user_agent' => $ua,
'new_user_agent' => $settings['header']['user-agent'],
)
);

$ua = $settings['header']['user-agent'];
}

Expand All @@ -1323,7 +1340,14 @@ public function add_user_agent( $ua ) {
public function send_through_proxy( $return_value, $uri, $check, $home ) {
$proxied = defined( 'FEEZY_URL_THRU_PROXY' ) ? FEEZY_URL_THRU_PROXY : null;
if ( $proxied && ( ( is_array( $proxied ) && in_array( $uri, $proxied, true ) ) || $uri === $proxied ) ) {
do_action( 'themeisle_log_event', FEEDZY_NAME, sprintf( 'sending %s through proxy', $uri ), 'info', __FILE__, __LINE__ );
Feedzy_Rss_Feeds_Log::info(
'Sending through proxy',
array(
'uri' => $uri,
'check' => $check,
'home' => $home,
)
);

return true;
}
Expand Down
Loading
Loading