Skip to content

Commit 3434ae1

Browse files
authored
Merge pull request #136 from alleyinteractive/feature/cli-display-logs
Adding CLI command to display site logs
2 parents 6904f31 + b95ebff commit 3434ae1

File tree

4 files changed

+117
-4
lines changed

4 files changed

+117
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a
44
CHANGELOG](https://keepachangelog.com/en/1.0.0/).
55

6+
## 2.6.3
7+
8+
- Add CLI command to display site logs.
9+
610
## 2.6.2
711

812
- Fix issue with `spatie/backtrace` package.

inc/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ function () {
2828

2929
// wp-cli command.
3030
if ( defined( 'WP_CLI' ) && WP_CLI ) {
31-
\WP_CLI::add_command( 'ai-logger', __NAMESPACE__ . '\CLI' );
31+
new CLI();
3232
}

inc/class-cli.php

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Psr\Log\LogLevel;
1313
use WP_CLI;
1414

15+
use function Mantle\Support\Helpers\collect;
16+
1517
// phpcs:disable WordPressVIPMinimum.Classes.RestrictedExtendClasses.wp_cli
1618

1719
if ( ! class_exists( 'WP_CLI_Command' ) ) {
@@ -24,7 +26,114 @@
2426
* Cannot extend `WPCOM_VIP_CLI_Command` since this plugin can run
2527
* outside the context of a VIP site.
2628
*/
27-
class CLI extends \WP_CLI_Command {
29+
final class CLI {
30+
/**
31+
* Constructor.
32+
*/
33+
public function __construct() {
34+
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
35+
return;
36+
}
37+
38+
WP_CLI::add_command( 'ai-logger cleanup', [ $this, 'cleanup' ] );
39+
WP_CLI::add_command( 'ai-logger display-object', [ $this, 'display_object' ] );
40+
WP_CLI::add_command( 'ai-logger display', [ $this, 'display' ] );
41+
WP_CLI::add_command( 'ai-logger generate-for-object', [ $this, 'generate_for_object' ] );
42+
WP_CLI::add_command( 'ai-logger generate', [ $this, 'generate' ] );
43+
}
44+
45+
/**
46+
* Display the site-wide log.
47+
*
48+
* @synopsis [--count=<value>] [--offset=<value>] [--log-context=<value>] [--level=<value>] [--format=<value>]
49+
*
50+
* @param array $args Arguments for the command.
51+
* @param array $assoc_args Associated flags for the command.
52+
*/
53+
public function display( $args, $assoc_args ): int {
54+
$assoc_args = \wp_parse_args(
55+
$assoc_args,
56+
[
57+
'count' => 50,
58+
'offset' => 0,
59+
'format' => 'table',
60+
]
61+
);
62+
63+
$tax_query = [
64+
'relation' => 'AND',
65+
];
66+
67+
if ( ! empty( $assoc_args['log-context'] ) ) {
68+
$tax_query[] = [
69+
'taxonomy' => Post_Handler::TAXONOMY_LOG_CONTEXT,
70+
'field' => 'slug',
71+
'terms' => explode( ',', $assoc_args['log-context'] ),
72+
];
73+
}
74+
75+
if ( ! empty( $assoc_args['level'] ) ) {
76+
$tax_query[] = [
77+
'taxonomy' => Post_Handler::TAXONOMY_LOG_LEVEL,
78+
'field' => 'slug',
79+
'terms' => explode( ',', $assoc_args['level'] ),
80+
];
81+
}
82+
83+
$logs = get_posts( [ // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_posts_get_posts
84+
'fields' => 'ids',
85+
'offset' => (int) $assoc_args['offset'],
86+
'order' => 'DESC',
87+
'orderby' => 'date',
88+
'post_type' => Post_Handler::POST_TYPE,
89+
'posts_per_page' => (int) $assoc_args['count'],
90+
'suppress_filters' => false,
91+
'tax_query' => $tax_query, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
92+
] );
93+
94+
if ( empty( $logs ) ) {
95+
WP_CLI::line( 'No logs found.' );
96+
97+
return 0;
98+
}
99+
100+
$logs = collect( $logs )->map( function ( int $log_id ): ?array {
101+
$record = get_post_meta( $log_id, '_logger_record', true );
102+
103+
if ( ! $record ) {
104+
return null;
105+
}
106+
107+
return [
108+
'context' => $record['context']['context'] ?? '',
109+
'message' => $record['message'] ?? '',
110+
'level_name' => $record['level_name'] ?? '',
111+
'datetime' => isset( $record['datetime'] ) ? $record['datetime']->setTimezone( wp_timezone() )->format( 'm/d/Y H:i:s' ) : null,
112+
];
113+
} )->filter()->values()->all();
114+
115+
WP_CLI\Utils\format_items(
116+
$assoc_args['format'],
117+
array_map(
118+
fn ( array $log ) => [
119+
'level' => $log['level_name'],
120+
'message' => $log['message'],
121+
'context' => $log['context'],
122+
'timestamp' => $log['datetime'],
123+
],
124+
$logs
125+
),
126+
[
127+
'level',
128+
'message',
129+
'context',
130+
'timestamp',
131+
]
132+
);
133+
134+
return 0;
135+
}
136+
28137
/**
29138
* Display the log for a post.
30139
*
@@ -42,7 +151,7 @@ class CLI extends \WP_CLI_Command {
42151
* @param array $args Arguments for the command.
43152
* @param array $assoc_args Associated flags for the command.
44153
*/
45-
public function display( $args, $assoc_args ) {
154+
public function display_object( $args, $assoc_args ) {
46155
[ $object_type, $object_id ] = $args;
47156

48157
$assoc_args = \wp_parse_args(

logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Alley Logger
44
* Plugin URI: https://github.com/alleyinteractive/logger
55
* Description: A Monolog-based logging tool for WordPress. Supports storing log message in a custom post type or in individual posts and terms.
6-
* Version: 2.6.2
6+
* Version: 2.6.3
77
* Author: Alley Interactive
88
* Author URI: https://alley.com/
99
* Requires at least: 6.5

0 commit comments

Comments
 (0)