Skip to content
Snippets Groups Projects
Commit a17169a1 authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

Merge branch 'feature/add-simple-logger-factory' into 'master'

feat: add SimpleLoggerFactory

See merge request !16
parents f1d4afb9 45bd4714
Branches
Tags
1 merge request!16feat: add SimpleLoggerFactory
Pipeline #7311 failed
# 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
......@@ -3,3 +3,4 @@
composer.lock
build-coverage
docker-compose.yml
.phpcs-cache
## [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
......
......@@ -6,24 +6,28 @@
"email": "krzysiek@wpdesk.pl"
}
],
"config": {
"platform": {
"php": "7.0.19"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require": {
"php": ">=5.6",
"psr/log": "^1.0.1",
"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",
......
<?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>
<?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;
}
<?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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment