Skip to content

Commit

Permalink
Display logs
Browse files Browse the repository at this point in the history
  • Loading branch information
av3nger committed Nov 6, 2023
1 parent a2c8dee commit 024f31d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
26 changes: 26 additions & 0 deletions app/modules/class-logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace CF_Images\App\Modules;

use CF_Images\App\Traits;

if ( ! defined( 'WPINC' ) ) {
die;
}
Expand All @@ -23,6 +25,8 @@
* @since 1.6.0
*/
class Logging extends Module {
use Traits\Ajax;

/**
* Log file.
*
Expand All @@ -45,6 +49,10 @@ public function init() {
}

add_action( 'cf_images_log', array( $this, 'log' ), 10, 5 );

if ( wp_doing_ajax() ) {
add_action( 'wp_ajax_cf_images_get_logs', array( $this, 'ajax_get_logs' ) );
}
}

/**
Expand Down Expand Up @@ -96,4 +104,22 @@ public function log( $message, ...$args ) {
flock( $fp, LOCK_UN );
fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}

/**
* Get logs.
*
* @since 1.6.0
*/
public function ajax_get_logs() {
$this->check_ajax_request( true );

if ( empty( $this->log_file ) ) {
wp_send_json_error( __( 'Log file not found.', 'cf-images' ) );
return;
}

$content = file_get_contents( $this->log_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions

wp_send_json_success( $content );
}
}
5 changes: 5 additions & 0 deletions assets/_src/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ $switch-background-active: $blue;
& input[type="checkbox"]:disabled:checked:before {
display: none;
}

& .cf-images-logs {
overflow-y: scroll;
max-height: 400px;
}
}

// Remove extra WordPress stuff.
Expand Down
39 changes: 33 additions & 6 deletions assets/_src/routes/misc/logs.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
/**
* External dependencies
*/
import { useEffect, useState } from 'react';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { post } from '../../js/helpers/post';

/**
* Cloudflare Images experimental settings routes.
*
* @class
*/
const Logs = () => {
const [error, setError] = useState('');
const [logs, setLogs] = useState('');

useEffect(() => {
post('cf_images_get_logs')
.then((response: ApiResponse) => {
if (!response.success && response.data) {
setError(response.data);
return;
}

setLogs(response.data);
})
.catch(window.console.log);
}, []);

return (
<section className="card">
<div className="card-content">
<div className="content">
<p className="title is-4">{__('Logs', 'cf-images')}</p>

<pre>
{__(
'Below is a list of links to resources that will help you get started or get additional help:',
'cf-images'
)}
</pre>
{error && (
<div className="notification is-warning">
<p>{error}</p>
</div>
)}

{logs && <pre className="cf-images-logs">{logs}</pre>}
</div>
</div>
</section>
Expand Down

0 comments on commit 024f31d

Please sign in to comment.