Skip to content

Commit 2615ff9

Browse files
chore: use lowercase levels
1 parent da68a19 commit 2615ff9

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

includes/admin/feedzy-rss-feeds-log.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ class Feedzy_Rss_Feeds_Log {
8989
* @var array<int, string> Log levels.
9090
*/
9191
private static $levels = array(
92-
self::DEBUG => 'DEBUG',
93-
self::INFO => 'INFO',
94-
self::WARNING => 'WARNING',
95-
self::ERROR => 'ERROR',
92+
self::DEBUG => 'debug',
93+
self::INFO => 'info',
94+
self::WARNING => 'warning',
95+
self::ERROR => 'error',
9696
);
9797

9898
const PRIORITIES_MAPPING = array(
@@ -268,8 +268,13 @@ public function set_context( array $context ) {
268268
* @return void
269269
*/
270270
public static function log( $level, $message, array $context = array() ) {
271-
$instance = self::get_instance();
272-
$instance->add_log_record( $level, $message, $context );
271+
try {
272+
$instance = self::get_instance();
273+
if ( $instance ) {
274+
$instance->add_log_record( $level, $message, $context );
275+
}
276+
} catch ( Throwable $e ) {
277+
}
273278
}
274279

275280
/**
@@ -305,7 +310,7 @@ private function add_log_record( $level, $message, array $context = array() ) {
305310

306311
$log_entry = array(
307312
'timestamp' => gmdate( 'c' ),
308-
'level' => isset( self::$levels[ $level ] ) ? self::$levels[ $level ] : 'UNKNOWN',
313+
'level' => isset( self::$levels[ $level ] ) ? self::$levels[ $level ] : 'unknown',
309314
'message' => $message,
310315
'context' => $merged_context,
311316
);
@@ -809,7 +814,7 @@ public function get_email_address() {
809814
* @return bool Whether there are logs or stats to report.
810815
*/
811816
public function has_reportable_data() {
812-
$logs_entries = $this->get_recent_logs( 50, 'ERROR' );
817+
$logs_entries = $this->get_recent_logs( 50, 'error' );
813818
$stats = get_option( self::STATS_OPTION_KEY, array() );
814819

815820
return ( ! empty( $logs_entries ) && is_array( $logs_entries ) ) || ! empty( $stats );
@@ -823,7 +828,7 @@ public function has_reportable_data() {
823828
* @return array<int, array<string, mixed>> The error log entries.
824829
*/
825830
public function get_error_logs_for_email( $limit = 50 ) {
826-
$recent_logs = $this->get_recent_logs( $limit, 'ERROR' );
831+
$recent_logs = $this->get_recent_logs( $limit, 'error' );
827832
if ( is_wp_error( $recent_logs ) ) {
828833
return array();
829834
}

includes/layouts/feedzy-logs-viewer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
$all_filter_url = admin_url( 'admin.php?page=feedzy-settings&tab=logs' );
88

9-
$logs_type = isset( $_REQUEST['logs_type'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_REQUEST['logs_type'] ) ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification
9+
$logs_type_filter = isset( $_REQUEST['logs_type'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_REQUEST['logs_type'] ) ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification
1010

1111
$log_types = array(
1212
'debug' => __( 'Debug', 'feedzy-rss-feeds' ),
@@ -40,7 +40,7 @@ class="btn btn-ghost"
4040
</a>
4141
</div>
4242
<div class="fz-logs-header-actions">
43-
<a href="<?php echo esc_url( $all_filter_url ); ?>" class="btn <?php echo esc_attr( is_null( $logs_type ) ? 'btn-primary' : 'btn-secondary' ); ?>">
43+
<a href="<?php echo esc_url( $all_filter_url ); ?>" class="btn <?php echo esc_attr( is_null( $logs_type_filter ) ? 'btn-primary' : 'btn-secondary' ); ?>">
4444
<?php esc_html_e( 'All', 'feedzy-rss-feeds' ); ?>
4545
</a>
4646
<?php foreach ( $log_types as $log_type => $label ) : ?>
@@ -51,7 +51,7 @@ class="btn btn-ghost"
5151
),
5252
$all_filter_url
5353
);
54-
$is_selected = strtoupper( $log_type ) === $logs_type;
54+
$is_selected = $log_type === $logs_type_filter;
5555
?>
5656
<a href="<?php echo esc_url( $filter_url ); ?>" class="btn <?php echo esc_attr( $is_selected ? 'btn-primary' : 'btn-secondary' ); ?>">
5757
<?php echo esc_html( $label ); ?>
@@ -86,7 +86,7 @@ class="fz-log-container fz-log-container--<?php echo esc_attr( strtolower( $leve
8686
[
8787
<?php
8888
if ( $log['level'] ) {
89-
echo esc_html( $log['level'] );
89+
echo esc_html( strtoupper( $log['level'] ) );
9090
} else {
9191
echo esc_html( '-' );
9292
}

includes/layouts/settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
$email_error_address_placeholder = ( ! empty( $email_error_address ) ) ? $email_error_address : get_option( 'admin_email' );
3434

3535
if ( 'logs' === $active_tab ) {
36-
$logs_type = isset( $_REQUEST['logs_type'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_REQUEST['logs_type'] ) ) ) : null;// phpcs:ignore WordPress.Security.NonceVerification
36+
$logs_type = isset( $_REQUEST['logs_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['logs_type'] ) ) : null;// phpcs:ignore WordPress.Security.NonceVerification
3737
$logs = Feedzy_Rss_Feeds_Log::get_instance()->get_recent_logs( 50, $logs_type );
3838
}
3939

tests/test-log.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function test_debug_logging() {
147147

148148
$logs = $this->logger->get_recent_logs( 1 );
149149
$this->assertNotEmpty( $logs, 'Should have log entries' );
150-
$this->assertEquals( 'DEBUG', $logs[0]['level'] );
150+
$this->assertEquals( 'debug', $logs[0]['level'] );
151151
$this->assertEquals( 'Test debug message', $logs[0]['message'] );
152152
$this->assertEquals( 'debug_value', $logs[0]['context']['test_data'] );
153153
}
@@ -163,7 +163,7 @@ public function test_info_logging() {
163163

164164
$logs = $this->logger->get_recent_logs( 1 );
165165
$this->assertNotEmpty( $logs );
166-
$this->assertEquals( 'INFO', $logs[0]['level'] );
166+
$this->assertEquals( 'info', $logs[0]['level'] );
167167
$this->assertEquals( 'Test info message', $logs[0]['message'] );
168168
}
169169

@@ -178,7 +178,7 @@ public function test_warning_logging() {
178178

179179
$logs = $this->logger->get_recent_logs( 1 );
180180
$this->assertNotEmpty( $logs );
181-
$this->assertEquals( 'WARNING', $logs[0]['level'] );
181+
$this->assertEquals( 'warning', $logs[0]['level'] );
182182
$this->assertEquals( 'Test warning message', $logs[0]['message'] );
183183
}
184184

@@ -191,7 +191,7 @@ public function test_error_logging() {
191191

192192
$logs = $this->logger->get_recent_logs( 1 );
193193
$this->assertNotEmpty( $logs );
194-
$this->assertEquals( 'ERROR', $logs[0]['level'] );
194+
$this->assertEquals( 'error', $logs[0]['level'] );
195195
$this->assertEquals( 'Test error message', $logs[0]['message'] );
196196

197197
// Check that error count statistic is incremented
@@ -213,7 +213,7 @@ public function test_log_level_threshold() {
213213

214214
$logs = $this->logger->get_recent_logs( 10 );
215215
$this->assertCount( 1, $logs, 'Only error message should be logged' );
216-
$this->assertEquals( 'ERROR', $logs[0]['level'] );
216+
$this->assertEquals( 'error', $logs[0]['level'] );
217217
$this->assertEquals( 'Error message', $logs[0]['message'] );
218218
}
219219

@@ -293,9 +293,9 @@ public function test_get_recent_logs_filtered_by_level() {
293293
Feedzy_Rss_Feeds_Log::info( 'Info message' );
294294
Feedzy_Rss_Feeds_Log::error( 'Error message' );
295295

296-
$error_logs = $this->logger->get_recent_logs( 10, 'ERROR' );
296+
$error_logs = $this->logger->get_recent_logs( 10, 'error' );
297297
$this->assertCount( 1, $error_logs );
298-
$this->assertEquals( 'ERROR', $error_logs[0]['level'] );
298+
$this->assertEquals( 'error', $error_logs[0]['level'] );
299299
$this->assertEquals( 'Error message', $error_logs[0]['message'] );
300300
}
301301

@@ -332,7 +332,7 @@ public function test_get_all_logs_raw() {
332332
$raw_content = $this->logger->get_all_logs_raw();
333333
$this->assertIsType( 'string', $raw_content );
334334
$this->assertStringContains( 'Test message', $raw_content );
335-
$this->assertStringContains( '"level":"DEBUG"', $raw_content );
335+
$this->assertStringContains( '"level":"debug"', $raw_content );
336336
}
337337

338338
/**
@@ -431,7 +431,7 @@ public function test_get_error_logs_for_email() {
431431

432432
$error_logs = $this->logger->get_error_logs_for_email( 10 );
433433
$this->assertCount( 1, $error_logs );
434-
$this->assertEquals( 'ERROR', $error_logs[0]['level'] );
434+
$this->assertEquals( 'error', $error_logs[0]['level'] );
435435
$this->assertEquals( 'Error for email', $error_logs[0]['message'] );
436436
}
437437

0 commit comments

Comments
 (0)