Skip to content
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

Release 2.1.0 #35

Merged
merged 9 commits into from Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -17,7 +17,7 @@ jobs:
shell: bash
run: rm -rf vendor
- name: Install dependencies
uses: php-actions/composer@v5
uses: php-actions/composer@v4
with:
php_version: 7.4
composer_version: 2
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -7,3 +7,5 @@ node_modules/
*.zip
vendor/
.phpunit.result.cache
.phpunit.cache
coverage/
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -3,7 +3,7 @@
This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a
CHANGELOG](https://keepachangelog.com/en/1.0.0/).

## Unreleased
## 2.1.0

### Added

Expand Down
4 changes: 2 additions & 2 deletions ai-logger.php
Expand Up @@ -2,8 +2,8 @@
/**
* Plugin Name: AI Logger
* Plugin URI: https://github.com/alleyinteractive/logger
* Description: A logger tool that stores errors and messages as a custom post type
* Version: 2.0.0
* Description: A Monolog-based logging tool for WordPress. Supports storing log message in a custom post type or in individual posts and terms.
* Version: 2.1.0
* Author: Alley Interactive, Jared Cobb
* Author URI: https://alley.co/
* Requires at least: 5.4
Expand Down
2 changes: 1 addition & 1 deletion composer.json
@@ -1,7 +1,7 @@
{
"name": "alleyinteractive/logger",
"type": "wordpress-plugin",
"description": "A logger tool that stores errors and messages as a custom post type.",
"description": "A Monolog-based logging tool for WordPress. Supports storing log message in a custom post type or in individual posts and terms. ",
"homepage": "https://github.com/alleyinteractive/logger",
"license": "GPL-2.0-or-later",
"authors": [
Expand Down
66 changes: 0 additions & 66 deletions inc/autoload.php

This file was deleted.

4 changes: 2 additions & 2 deletions inc/bootstrap.php
Expand Up @@ -7,10 +7,10 @@

namespace AI_Logger;

require_once __DIR__ . '/autoload.php';
use function Mantle\Framework\generate_wp_autoloader;

try {
\spl_autoload_register( generate_autoloader( __NAMESPACE__, __DIR__ ) );
\spl_autoload_register( generate_wp_autoloader( __NAMESPACE__, __DIR__ ) );
} catch ( \Exception $exception ) {
wp_die( esc_html__( 'Error generating autoloader.', 'ai-logger' ) );
}
Expand Down
13 changes: 12 additions & 1 deletion inc/class-ai-logger-js.php
Expand Up @@ -73,8 +73,19 @@ public function log() {
$level = $args['level'] ?? 'info';
$args['context'] = $args['context'] ?? 'front-end';

$logger = ai_logger();

// Create the log entry.
ai_logger()->$level( $message, $args );
if ( ! method_exists( $logger, $level ) ) {
wp_send_json_error(
[
'error' => __( 'Invalid log level.', 'ai-logger' ),
],
400
);
}

$logger->$level( $message, $args );

// Respond.
wp_send_json_success(
Expand Down
18 changes: 0 additions & 18 deletions inc/class-ai-logger.php
Expand Up @@ -155,24 +155,6 @@ public function to_term( string $key, int $object_id, $level = Logger::DEBUG ):
);
}

/**
* Pass all unknown methods to the log handler.
*
* @param string $method Method called.
* @param array $args Arguments for the method.
*/
public static function __callStatic( string $method, array $args = [] ) {
return call_user_func_array(
[ static::instance()->get_logger(), 'log' ],
array_merge(
[
$method,
],
$args
)
);
}

/**
* System is unusable.
*
Expand Down
8 changes: 5 additions & 3 deletions inc/class-cli.php
Expand Up @@ -7,13 +7,15 @@

namespace AI_Logger;

use AI_Logger\Handler\Post_Handler;
use Monolog\Logger;
use Psr\Log\LogLevel;
use WP_CLI;

// phpcs:disable WordPressVIPMinimum.Classes.RestrictedExtendClasses.wp_cli

if ( ! class_exists( 'WP_CLI_Command' ) ) {
return;
}

/**
* AI_Logger CLI Command
*
Expand All @@ -39,7 +41,7 @@ class CLI extends \WP_CLI_Command {
* @param array $assoc_args Associated flags for the command.
*/
public function display( $args, $assoc_args ) {
list ( $object_type, $object_id ) = $args;
[ $object_type, $object_id ] = $args;

$assoc_args = \wp_parse_args(
$assoc_args,
Expand Down
29 changes: 10 additions & 19 deletions inc/handler/class-cli-handler.php
Expand Up @@ -7,36 +7,27 @@

namespace AI_Logger\Handler;

use Monolog\Handler\AbstractProcessingHandler;

/**
* WP-CLI Handler to pipe logs to the wp-cli output.
*/
class CLI_Handler implements Handler_Interface {
/**
* Clear the stored log, not applicable.
*/
public function clear() { }

class CLI_Handler extends AbstractProcessingHandler {
/**
* Write a log to the wp-cli.
*
* @param string $level Log level {@see Psr\Log\LogLevel}.
* @param string $message Log message.
* @param array $context Context to store.
* @link https://github.com/php-fig/log/blob/master/Psr/Log/AbstractLogger.php
*
* @param array $record Log Record.
*/
public function handle( string $level, string $message, array $context = [] ) {
protected function write( array $record ): void {
[ 'formatted' => $formatted ] = $record;

// Ignore if the request isn't through WP-CLI.
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}

\WP_CLI::log(
\sprintf(
'log %s %s: %s %s',
$level,
\current_time( 'H:i:s' ),
$message,
! empty( $context ) ? '(' . \wp_json_encode( $context ) . ')' : ''
)
);
\WP_CLI::log( $formatted );
}
}
46 changes: 23 additions & 23 deletions inc/handler/class-exception-handler.php
Expand Up @@ -7,41 +7,41 @@

namespace AI_Logger\Handler;

use Psr\Log\LogLevel;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;

/**
* 'Logs' critical errors to exceptions to allow for ease of error catching.
*
* Logs with a log level of emergency, alert, or critical will throw
* the `Handler_Exception` exception.
* By default, this will only throw an exception for critical errors and above.
*/
class Exception_Handler implements Handler_Interface {
class Exception_Handler extends AbstractProcessingHandler {
/**
* Clear the stored log, not applicable.
* Constructor.
*
* @param int|string $level The minimum logging level at which this handler will be triggered.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not.
*/
public function clear() { }
public function __construct( $level = Logger::CRITICAL, bool $bubble = true ) {
$this->setLevel( $level );
$this->bubble = $bubble;
}

/**
* Store a log entry to an exception.
*
* @param string $level Log level {@see Psr\Log\LogLevel}.
* @param string $message Log message.
* @param array $context Context to store.
* @link https://github.com/php-fig/log/blob/master/Psr/Log/AbstractLogger.php
*
* @param array $record Log Record.
*
* @throws Handler_Exception Thrown on high-level error.
*/
public function handle( $level, $message, array $context = [] ) {
if (
in_array(
$level,
[
LogLevel::EMERGENCY,
LogLevel::ALERT,
LogLevel::CRITICAL,
],
true
)
) {
throw new Handler_Exception( $message, $context );
}
protected function write( array $record ): void {
[
'context' => $context,
'message' => $message,
] = $record;

throw new Handler_Exception( $message, $context, $record );
}
}
22 changes: 20 additions & 2 deletions inc/handler/class-handler-exception.php
Expand Up @@ -18,15 +18,25 @@ class Handler_Exception extends \Exception {
*/
protected $context;

/**
* Exception record.
*
* @var array
*/
protected $record;

/**
* Constructor.
*
* @param string $message Exception message.
* @param array $context Exception context.
* @param array $record Log record.
*/
public function __construct( string $message = '', array $context = [] ) {
public function __construct( string $message = '', array $context = [], array $record = [] ) {
parent::__construct( $message );

$this->context = $context;
$this->record = $record;
}

/**
Expand All @@ -37,5 +47,13 @@ public function __construct( string $message = '', array $context = [] ) {
public function get_context(): array {
return $this->context;
}
}

/**
* Getter for the exception record.
*
* @return array
*/
public function get_record(): array {
return $this->record;
}
}
27 changes: 0 additions & 27 deletions inc/handler/class-post-handler.php
Expand Up @@ -9,7 +9,6 @@

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Psr\Log\LogLevel;

/**
* Post Log Handler
Expand Down Expand Up @@ -53,15 +52,6 @@ class Post_Handler extends AbstractProcessingHandler implements Handler_Interfac
*/
protected $original_site_id;

/**
* A predefined list of log levels that are permitted.
* These are stored as terms in the Level taxonomy.
*
* @var array
* @access protected
*/
protected $allowed_levels = [];

/**
* The time limit that this logger should wait before
* attempting to insert another UNIQUE log entry in seconds
Expand All @@ -80,23 +70,6 @@ class Post_Handler extends AbstractProcessingHandler implements Handler_Interfac
public function __construct( $level = Logger::DEBUG, bool $bubble = true ) {
parent::__construct( $level, $bubble );

/**
* Log levels according to RFC 5424.
*
* @link https://tools.ietf.org/html/rfc5424
*/
$this->allowed_levels = array(
LogLevel::EMERGENCY => __( 'Emergency', 'ai-logger' ),
LogLevel::ALERT => __( 'Alert', 'ai-logger' ),
LogLevel::CRITICAL => __( 'Critical', 'ai-logger' ),
LogLevel::ERROR => __( 'Error', 'ai-logger' ),
LogLevel::WARNING => __( 'Warning', 'ai-logger' ),
LogLevel::NOTICE => __( 'Notice', 'ai-logger' ),
LogLevel::INFO => __( 'Info', 'ai-logger' ),
LogLevel::DEBUG => __( 'Debug', 'ai-logger' ),
'log' => __( 'Log', 'ai-logger' ),
);

$this->throttle_limit = (int) apply_filters( 'ai_logger_throttle_limit', MINUTE_IN_SECONDS * 15 );
$this->original_site_id = \get_current_blog_id();

Expand Down