From 45bd4714ac38abad65b889ee0bf432da73573593 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Wed, 23 Mar 2022 17:47:03 +0100 Subject: [PATCH] feat: add SimpleLoggerFactory --- .editorconfig | 54 +++++++++++++++++++++++++++++++ .gitignore | 3 +- CHANGELOG.md | 13 ++++++-- composer.json | 20 +++++++----- phpcs.xml.dist | 48 ++++++++++++++++++++++++++++ src/Settings.php | 18 +++++++++++ src/SimpleLoggerFactory.php | 63 +++++++++++++++++++++++++++++++++++++ 7 files changed, 207 insertions(+), 12 deletions(-) create mode 100644 .editorconfig create mode 100644 phpcs.xml.dist create mode 100644 src/Settings.php create mode 100644 src/SimpleLoggerFactory.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..116d005 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,54 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# WordPress Coding Standards +# https://make.wordpress.org/core/handbook/coding-standards/ + +# Configuration for PhpStorm + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = tab +indent_size = 4 +tab_width = 4 +ij_continuation_indent_size = 4 + +[{*.ctp,*.hphp,*.inc,*.module,*.php,*.php4,*.php5,*.phtml}] +ij_php_align_assignments = true +ij_php_align_class_constants = true +ij_php_align_key_value_pairs = true +ij_php_align_phpdoc_comments = true +ij_php_align_phpdoc_param_names = true +ij_php_class_brace_style = end_of_line +ij_php_comma_after_last_array_element = true +ij_php_else_if_style = combine +ij_php_force_short_declaration_array_style = true +ij_php_getters_setters_naming_style = snake_case +ij_php_if_brace_force = always +ij_php_lower_case_boolean_const = true +ij_php_lower_case_null_const = true +ij_php_method_brace_style = end_of_line +ij_php_phpdoc_blank_line_before_tags = true +ij_php_phpdoc_blank_lines_around_parameters = true +ij_php_phpdoc_keep_blank_lines = false +ij_php_phpdoc_wrap_long_lines = true +ij_php_space_after_type_cast = true +ij_php_space_after_unary_not = true +ij_php_space_before_unary_not = true +ij_php_spaces_around_var_within_brackets = true +ij_php_spaces_within_array_initializer_braces = true +ij_php_spaces_within_catch_parentheses = true +ij_php_spaces_within_for_parentheses = true +ij_php_spaces_within_if_parentheses = true +ij_php_spaces_within_method_call_parentheses = true +ij_php_spaces_within_method_parentheses = true +ij_php_spaces_within_parentheses = true +ij_php_spaces_within_switch_parentheses = true +ij_php_spaces_within_while_parentheses = true +ij_php_ternary_operation_signs_on_next_line = true +ij_php_variable_naming_style = snake_case diff --git a/.gitignore b/.gitignore index 5e97e71..87cf4de 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea composer.lock build-coverage -docker-compose.yml \ No newline at end of file +docker-compose.yml +.phpcs-cache diff --git a/CHANGELOG.md b/CHANGELOG.md index d062e4d..4b9c27e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [1.7.0] - Unreleased +### Added +- Added `SimpleLoggerFactory` for basic logger use cases. + +### Changed +- Minimum PHP required is now >=7.0 + ## [1.6.2] - 2020-07-21 ### Fixed - Notice error when cannot create log file @@ -45,8 +52,8 @@ ### Changed - setDisableLog changes to disableLog -## [1.2.0] - 2018-10-29 -### Changed +## [1.2.0] - 2018-10-29 +### Changed - getWPDeskFileName renamed to getFileName - isWPDeskLogWorking renamed to isLogWorking ### Added @@ -65,4 +72,4 @@ ## [1.0.0] - 2018-10-28 ### Added - first stable version -- 80% coverage in integration tests \ No newline at end of file +- 80% coverage in integration tests diff --git a/composer.json b/composer.json index dcf1f22..8181625 100644 --- a/composer.json +++ b/composer.json @@ -6,24 +6,28 @@ "email": "krzysiek@wpdesk.pl" } ], - "require": { - "php": ">=5.6", - "psr/log": "^1.0.1", + "config": { + "platform": { + "php": "7.0.19" + }, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "require": { + "php": ">=7.0|^8", + "psr/log": "^1", "monolog/monolog": "^1.23", "wpdesk/wp-notice": "^3.0" }, "require-dev": { "phpunit/phpunit": "^5", - "wp-coding-standards/wpcs": "^0.14.1", "squizlabs/php_codesniffer": "^3.0.2", - "wimg/php-compatibility": "^8" + "wpdesk/wp-code-sniffer": "^1.2.3" }, "autoload": { - "classmap": ["src/deprecated"], "psr-4": {"WPDesk\\Logger\\": "src/"} }, - "autoload-dev": { - }, "scripts": { "phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never", "phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage", diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..78b4b74 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,48 @@ +<?xml version="1.0"?> +<ruleset name="WordPress Coding Standards for WP Desk Plugin"> + + <!-- + ############################################################################# + COMMAND LINE ARGUMENTS + https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml + ############################################################################# + --> + + <!-- Scan all files. --> + <file>./src</file> + + <!-- Only check PHP files. --> + <arg name="extensions" value="php"/> + + <!-- Show progress, show the error codes for each message (source). --> + <arg value="sp"/> + + <!-- Check up to 8 files simultaneously. --> + <arg name="parallel" value="8"/> + + <!-- Cache outcomes for better performance. Remember to add the file to .gitignore. --> + <arg name="cache" value="./.phpcs-cache"/> + + <!-- + ############################################################################# + USE THE WPDeskCS RULESET + ############################################################################# + --> + + <!-- Define plugin text domain for i18n. --> + <config name="text_domain" value="wpdesk-helper"/> + + <!-- This value should be aligned with WordPress support version declared in plugin header --> + <config name="minimum_supported_wp_version" value="5.0"/> + + <!-- Set value aligned with supported PHP Version for PHPCompatibilityWP check. --> + <config name="testVersion" value="7.0-"/> + + <rule ref="WPDeskPlugin"/> + + <!-- + ############################################################################## + CUSTOM RULES + ############################################################################## + --> +</ruleset> diff --git a/src/Settings.php b/src/Settings.php new file mode 100644 index 0000000..07fa978 --- /dev/null +++ b/src/Settings.php @@ -0,0 +1,18 @@ +<?php + +namespace WPDesk\Logger; + +use Psr\Log\LogLevel; + +final class Settings { + + /** @var string */ + public $level = LogLevel::DEBUG; + + /** @var bool */ + public $use_wc_log = true; + + /** @var bool */ + public $use_wp_log = true; + +} diff --git a/src/SimpleLoggerFactory.php b/src/SimpleLoggerFactory.php new file mode 100644 index 0000000..e815c8b --- /dev/null +++ b/src/SimpleLoggerFactory.php @@ -0,0 +1,63 @@ +<?php +declare(strict_types=1); + +namespace WPDesk\Logger; + +use Monolog\Handler\HandlerInterface; +use Monolog\Handler\NullHandler; +use Monolog\Logger; +use Monolog\Handler\ErrorLogHandler; +use WPDesk\Logger\WC\WooCommerceHandler; + +final class SimpleLoggerFactory implements LoggerFactory { + + /** @var Settings */ + private $options; + + /** @var string */ + private $channel; + + /** @var Logger */ + private $logger; + + public function __construct( string $channel, Settings $options = null ) { + $this->channel = $channel; + $this->options = $options ?? new Settings(); + } + + public function getLogger( string $channel, Settings $options = null ): Logger { + if ( $this->logger ) { + return $this->logger; + } + + $logger = new Logger( $this->channel ); + + $wc_handler = $this->get_wc_handler(); + if ( $this->options->use_wc_log ) { + $logger->pushHandler( $wc_handler ); + } + + if ( $this->options->use_wp_log || $wc_handler instanceof NullHandler ) { + $logger->pushHandler( $this->get_wp_handler() ); + } + + return $this->logger = $logger; + } + + private function get_wc_handler(): HandlerInterface { + if ( function_exists( 'wc_get_logger' ) ) { + return new WooCommerceHandler( wc_get_logger(), $this->options->level ); + } + + return new NullHandler(); + } + + private function get_wp_handler(): HandlerInterface { + if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { + return new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $this->options->level ); + } + + return new NullHandler(); + } + +} -- GitLab