Skip to content

V3 monolog 3 #91

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

Open
wants to merge 43 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9e30651
feat: add monolog 3 compatibility
luislard Apr 9, 2025
2f756aa
chore: wip
luislard Apr 9, 2025
cbb9d21
chore: making the necessary changes to adapt to monolg v3
luislard Apr 10, 2025
13844b7
chore: adding monolog v3 compatibility
luislard Apr 11, 2025
aaa4a92
chore: remove support for LogRecord
luislard Apr 11, 2025
4a2ee88
chore: fix cs and phpstan issues
luislard Apr 11, 2025
0506840
chore: remove unused files
luislard Apr 15, 2025
0df9147
chore: removed some typing in arguments to make phpstan pass
luislard Apr 15, 2025
bfb659a
chore: test adding custom cache key to the workflow
luislard Apr 15, 2025
0d6cc8b
chore: passing the lowest to composer args
luislard Apr 15, 2025
7970895
chore: try passing dependency versions
luislard Apr 15, 2025
30d0a5e
chore: fix missing var in matrix
luislard Apr 15, 2025
14d53f4
chore: change branch name on static analys workflow
luislard Apr 15, 2025
fd539b2
chore: after refactoring unit tests are passing
luislard Apr 15, 2025
672afcb
chore: making integration tests run for lowest and highest dependenci…
luislard Apr 15, 2025
14b3079
chore: integration tests passing
luislard Apr 15, 2025
3dddb08
chore: cs passing
luislard Apr 15, 2025
a84a09f
chore: ignoring some lines due to phpstan failing when managing missi…
luislard Apr 15, 2025
93d4442
chore: align phpstan with refactor
luislard Apr 15, 2025
7e2b99e
chore: refactor Logger::$level to Levels::$level
luislard Apr 16, 2025
5c625f5
chore: fix pr comments
luislard Apr 16, 2025
8f7c14b
chore: changing docker contexts
luislard Apr 16, 2025
25687a2
chore: silencing failing asserts due to WP 6.8 release
luislard Apr 16, 2025
de20878
Merge pull request #93 from inpsyde/refactor_pulling_levels_from_logg…
luislard Apr 16, 2025
310fe2c
chore: adding NullProcessor test
luislard Apr 16, 2025
55d6091
chore: remove the processing step from record factory
luislard Apr 16, 2025
0e84d43
Merge pull request #94 from inpsyde/remove_processing_step_from_recor…
luislard Apr 16, 2025
bb2ad40
Merge branch 'v3-monolog-3' into test_luis
luislard Apr 16, 2025
d4ded4a
chore: adding tests for NullProcessor and RecordFactory
luislard Apr 16, 2025
9650e2e
solves the boring warning: The repository at /var/www/html does not h…
overclokk Apr 17, 2025
05b8c4c
should fix: Notice: Function wp_is_block_theme was called incorrectly
overclokk Apr 17, 2025
a3241f5
cs:fix
overclokk Apr 17, 2025
e01c95b
removes commented lines
overclokk Apr 17, 2025
e59ae7b
it was only the themes folder
overclokk Apr 17, 2025
c317e11
cs:fix
overclokk Apr 17, 2025
f63328c
chore: add Levels test
luislard Apr 21, 2025
5924688
Merge pull request #96 from inpsyde/test_enea
luislard Apr 21, 2025
c8bc033
Merge branch 'v3-monolog-3' into test_luis
luislard Apr 21, 2025
2ed1434
chore: add return types
luislard Apr 21, 2025
57645eb
chore: add tests when passed record is LogRecord
luislard Apr 21, 2025
64ccfa2
Merge pull request #95 from inpsyde/test_luis
luislard Apr 22, 2025
dd4286f
test: adding unit test for PassthroughFormatter
yeremi Apr 25, 2025
622ed11
Merge pull request #98 from inpsyde/unit_test_yeremi
yeremi Apr 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ services:
php81:
container_name: wonolog-php-81
build:
context: ./php81
context: .
dockerfile: ./php81/Dockerfile
depends_on:
- db
environment:
Expand All @@ -36,7 +37,8 @@ services:
php82:
container_name: wonolog-php-82
build:
context: ./php82
context: .
dockerfile: ./php82/Dockerfile
depends_on:
- db
environment:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/docker/php81/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ RUN chmod +x /usr/bin/install-composer.sh && /usr/bin/install-composer.sh && mv
COPY wait-for.sh /usr/bin/wait-for.sh
RUN chmod +x /usr/bin/wait-for.sh

RUN git config --global --add safe.directory /var/www/html

WORKDIR /var/www/html
2 changes: 2 additions & 0 deletions .github/workflows/docker/php82/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ RUN chmod +x /usr/bin/install-composer.sh && /usr/bin/install-composer.sh && mv
COPY wait-for.sh /usr/bin/wait-for.sh
RUN chmod +x /usr/bin/wait-for.sh

RUN git config --global --add safe.directory /var/www/html

WORKDIR /var/www/html
5 changes: 4 additions & 1 deletion .github/workflows/php-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
fail-fast: false
matrix:
php-service: [ '81', '82' ]
dependency-versions: [ 'lowest', 'highest' ]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -34,4 +35,6 @@ jobs:

- name: Run integration tests
working-directory: .github/workflows/docker
run: docker compose run --rm php${{ matrix.php-service }} sh -c "wait-for.sh wonolog-db:3306 -t 15 -- composer update && composer tests:integration"
run: |
COMPOSER_DEPENDENCY_VERSIONS=${{ matrix.dependency-versions == 'lowest' && '--prefer-lowest' || '' }}
docker compose run --rm php${{ matrix.php-service }} sh -c "wait-for.sh wonolog-db:3306 -t 15 -- composer update $COMPOSER_DEPENDENCY_VERSIONS && composer tests:integration"
6 changes: 5 additions & 1 deletion .github/workflows/php-static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
strategy:
matrix:
php-version: ["8.1", "8.2", "8.3", "8.4"]
dependency-versions: ['highest', 'lowest']
with:
PHP_VERSION: ${{ matrix.php-version }}

Expand All @@ -44,9 +45,12 @@ jobs:

static-code-analysis-php:
if: ${{ (github.event_name != 'workflow_dispatch') || ((github.event.inputs.jobs == 'Run all') || (github.event.inputs.jobs == 'Run static analysis only')) }}
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-php.yml@main
uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-php.yml@support_dependency_versions_in_static_analysis
strategy:
matrix:
php-version: ["8.1", "8.2", "8.3", "8.4"]
dependency-versions: ["lowest", "highest"]
with:
PHP_VERSION: ${{ matrix.php-version }}
PHPSTAN_ARGS: "--no-progress --memory-limit=1G"
DEPENDENCY_VERSIONS: ${{ matrix.dependency-versions }}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"php": ">=8.1 < 8.5",
"psr/log": "^2.0||^3.0",
"wecodemore/wordpress-early-hook": "^1.3.0",
"monolog/monolog": "^2.3.5"
"monolog/monolog": "^2.0||^3.0"
},
"require-dev": {
"brain/monkey": "^2.6.1",
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ parameters:
paths:
- src
treatPhpDocTypesAsCertain: false
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#Class PHPMailer not found.#'
- '#Access to property \$\w+ on an unknown class PHPMailer#'
- '#has invalid return type Monolog\\LogRecord#'
- '#Instantiated class Monolog\\LogRecord not found#'
4 changes: 3 additions & 1 deletion src/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,9 @@ protected function shouldSetup(): bool

// We use WONOLOG_DISABLE instead of WONOLOG_ENABLE so that enabled is the default.
$disabled = getenv('WONOLOG_DISABLE');
defined('WONOLOG_DISABLE') and $disabled = WONOLOG_DISABLE;
if (defined('WONOLOG_DISABLE')) {
$disabled = WONOLOG_DISABLE;
}

/**
* Filters whether to completely disable Wonolog.
Expand Down
27 changes: 15 additions & 12 deletions src/DefaultHandler/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Inpsyde\Wonolog\DefaultHandler;

use Inpsyde\Wonolog\Levels;
use Inpsyde\Wonolog\LogLevel;
use Inpsyde\Wonolog\Processor;
use Monolog\Formatter\FormatterInterface;
Expand All @@ -22,7 +23,7 @@
use Monolog\Handler\NullHandler;
use Monolog\Handler\ProcessableHandlerInterface;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\LogRecord;
use Monolog\ResettableInterface;

class FileHandler implements
Expand Down Expand Up @@ -164,36 +165,37 @@ public function disableBuffering(): FileHandler
}

/**
* @param array $record
* @param array|LogRecord $record
* @return bool
*
* @psalm-suppress MixedArgumentTypeCoercion
* @phpstan-ignore-next-line
*/
public function handle(array $record): bool
public function handle(array|LogRecord $record): bool
{
$this->ensureHandler();

return $this->handler->handle($record);
}

/**
* @param array $record
* @param array|LogRecord $record
* @return bool
*
* @psalm-suppress MixedArgumentTypeCoercion
* @phpstan-ignore-next-line
*/
public function isHandling(array $record): bool
public function isHandling(array|LogRecord $record): bool
{
$this->ensureHandler();

return $this->handler->isHandling($record);
}

/**
* @param array<array> $records
* @param array<array>|array<LogRecord> $records
* @return void
*
* @psalm-suppress MixedArgumentTypeCoercion
* @phpstan-ignore-next-line
*/
public function handleBatch(array $records): void
{
Expand Down Expand Up @@ -230,7 +232,7 @@ public function pushProcessor(callable $callback): HandlerInterface
}

/**
* @return callable(array):array
* @return callable(array):array | callable(LogRecord):LogRecord
*
* @psalm-suppress MixedReturnTypeCoercion
* @psalm-suppress LessSpecificImplementedReturnType
Expand Down Expand Up @@ -278,7 +280,8 @@ public function getFormatter(): FormatterInterface
/** @var FormatterInterface|null $noopFormatter */
static $noopFormatter;

return $noopFormatter ?? $noopFormatter = new PassthroughFormatter();
return $noopFormatter
?? $noopFormatter = new PassthroughFormatter();
}

/**
Expand Down Expand Up @@ -336,12 +339,12 @@ private function ensureHandler(): void
if ($this->handler) {
return;
}

try {
$this->logFilePath = $this->logFilePath();
$level = $this->minLevel ?? LogLevel::defaultMinLevel();
if (!$level) {
$level = Logger::DEBUG;
/** @phpstan-ignore-next-line classConstant.deprecated */
$level = Levels::DEBUG;
}
$streamBuffer = $this->buffering || $this->bubble;
$handler = new StreamHandler($this->logFilePath, $level, $streamBuffer, null, true);
Expand Down
6 changes: 4 additions & 2 deletions src/DefaultHandler/PassthroughFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
namespace Inpsyde\Wonolog\DefaultHandler;

use Monolog\Formatter\FormatterInterface;
use Monolog\LogRecord;

class PassthroughFormatter implements FormatterInterface
{
/**
* @param array $record
* @param array|LogRecord $record
* @return array
* @phpstan-ignore-next-line
*/
public function format(array $record): array
public function format(array|LogRecord $record): array|LogRecord
{
return $record;
}
Expand Down
87 changes: 87 additions & 0 deletions src/Levels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Inpsyde\Wonolog;

/**
* This is a copy of the Logger class in Monolog v2
*/
class Levels
{
/**
* Detailed debug information
*/
public const DEBUG = 100;

/**
* Interesting events
*
* Examples: User logs in, SQL logs.
*/
public const INFO = 200;

/**
* Uncommon events
*/
public const NOTICE = 250;

/**
* Exceptional occurrences that are not errors
*
* Examples: Use of deprecated APIs, poor use of an API,
* undesirable things that are not necessarily wrong.
*/
public const WARNING = 300;

/**
* Runtime errors
*/
public const ERROR = 400;

/**
* Critical conditions
*
* Example: Application component unavailable, unexpected exception.
*/
public const CRITICAL = 500;

/**
* Action must be taken immediately
*
* Example: Entire website down, database unavailable, etc.
* This should trigger the SMS alerts and wake you up.
*/
public const ALERT = 550;

/**
* Urgent alert.
*/
public const EMERGENCY = 600;

/**
* This is a static variable and not a constant to serve as an extension point for custom levels
*
* @var array<int, string> $levels Logging levels with the levels as key
*
* @phpstan-var array<int, string> $levels Logging levels with the levels as key
*/
protected static array $levels = [
self::DEBUG => 'DEBUG',
self::INFO => 'INFO',
self::NOTICE => 'NOTICE',
self::WARNING => 'WARNING',
self::ERROR => 'ERROR',
self::CRITICAL => 'CRITICAL',
self::ALERT => 'ALERT',
self::EMERGENCY => 'EMERGENCY',
];

/**
* @return array<string, int>
*/
public static function allLevels(): array
{
return array_flip(static::$levels);
}
}
1 change: 0 additions & 1 deletion src/LogActionUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function update(LogData $log): void
) {
return;
}

try {
$context = $this->parseContext($log);
$this->channels
Expand Down
31 changes: 19 additions & 12 deletions src/LogLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@

namespace Inpsyde\Wonolog;

use Monolog\Logger;

/**
* Utility object used to build default min logging level based WordPress and environment settings.
* It also has a method to check the validity of a value as level identifier.
*/
abstract class LogLevel
{
public const DEBUG = Logger::DEBUG;
public const INFO = Logger::INFO;
public const NOTICE = Logger::NOTICE;
public const WARNING = Logger::WARNING;
public const ERROR = Logger::ERROR;
public const CRITICAL = Logger::CRITICAL;
public const ALERT = Logger::ALERT;
public const EMERGENCY = Logger::EMERGENCY;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const DEBUG = Levels::DEBUG;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const INFO = Levels::INFO;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const NOTICE = Levels::NOTICE;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const WARNING = Levels::WARNING;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const ERROR = Levels::ERROR;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const CRITICAL = Levels::CRITICAL;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const ALERT = Levels::ALERT;
/** @phpstan-ignore-next-line classConstant.deprecated */
public const EMERGENCY = Levels::EMERGENCY;

private static ?int $minLevel = null;

Expand All @@ -33,7 +39,7 @@ abstract class LogLevel
*/
final public static function allLevels(): array
{
return Logger::getLevels();
return Levels::allLevels();
}

/**
Expand All @@ -57,7 +63,8 @@ final public static function defaultMinLevel(): int
// If no valid level is defined via env var, then let's resort to WP constants.
if (!$minLevel) {
$const = defined('WP_DEBUG_LOG') ? 'WP_DEBUG_LOG' : 'WP_DEBUG';
$minLevel = (defined($const) && constant($const)) ? Logger::DEBUG : Logger::WARNING;
/** @phpstan-ignore-next-line classConstant.deprecated */
$minLevel = (defined($const) && constant($const)) ? Levels::DEBUG : Levels::WARNING;
}

self::$minLevel = $minLevel;
Expand Down
15 changes: 15 additions & 0 deletions src/MonologUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Inpsyde\Wonolog;

use Monolog\Logger;

class MonologUtils
{
public static function version(): int
{
return Logger::API;
}
}
Loading