Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- Chg #109: Deprecate `Logger` methods `setTraceLevel()` and `setExcludedTracePaths()` in favor of context provider
usage (@vjik)
- New #111: Add `DateTime` and `DateTimeImmutable` support as time in log context (@vjik)
- New #112: Add `Message::category()` method and `Message::DEFAULT_CATEGORY` constant, deprecate
`CategoryFilter::DEFAULT` in favor it (@vjik)

## 2.0.0 May 22, 2022

Expand Down
4 changes: 2 additions & 2 deletions src/ContextProvider/ContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Log\ContextProvider;

use InvalidArgumentException;
use Yiisoft\Log\Message\CategoryFilter;
use Yiisoft\Log\Message;

/**
* @psalm-type Backtrace = list<array{
Expand Down Expand Up @@ -46,7 +46,7 @@ public function getContext(): array
'time' => microtime(true),
'trace' => $this->collectTrace($trace),
'memory' => memory_get_usage(),
'category' => CategoryFilter::DEFAULT,
'category' => Message::DEFAULT_CATEGORY,
];
}

Expand Down
19 changes: 19 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Log;

use LogicException;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;
Expand All @@ -18,6 +19,8 @@
*/
final class Message
{
public const DEFAULT_CATEGORY = 'application';

/**
* @var string Log message level.
*
Expand Down Expand Up @@ -99,6 +102,22 @@ public function context(string $name = null, mixed $default = null): mixed
return $this->context[$name] ?? $default;
}

/**
* Returns the log message category. {@see self::DEFAULT_CATEGORY} is returned if the category is not set.
*
* @return string The log message category.
*/
public function category(): string
{
$category = $this->context['category'] ?? self::DEFAULT_CATEGORY;
if (!is_string($category)) {
throw new LogicException(
'Invalid category value in log context. Expected "string", got "' . get_debug_type($category) . '".'
);
}
return $category;
}

/**
* Parses log message resolving placeholders in the form: "{foo}",
* where foo will be replaced by the context data in key "foo".
Expand Down
5 changes: 5 additions & 0 deletions src/Message/CategoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use InvalidArgumentException;

use Yiisoft\Log\Message;

use function gettype;
use function is_string;
use function rtrim;
Expand All @@ -18,6 +20,9 @@
*/
final class CategoryFilter
{
/**
* @deprecated Since 2.1, will be removed in 3.0. Use {@see Message::DEFAULT_CATEGORY} instead.
*/
public const DEFAULT = 'application';

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Message/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@ private function defaultFormat(Message $message, array $commonContext): string
$time = $this->getTime($message);
$prefix = $this->getPrefix($message, $commonContext);
$context = $this->getContext($message, $commonContext);
/** @var string $category */
$category = $message->context('category', CategoryFilter::DEFAULT);

return "{$time} {$prefix}[{$message->level()}][{$category}] {$message->message()}{$context}";
return "{$time} {$prefix}[{$message->level()}][{$message->category()}] {$message->message()}{$context}";
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,7 @@ private function filterMessages(array $messages): void
continue;
}

/** @var string $category */
$category = $message->context('category', '');

if ($this->categories->isExcluded($category)) {
if ($this->categories->isExcluded($message->category())) {
unset($messages[$i]);
continue;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Log\Tests;

use LogicException;
use PHPUnit\Framework\TestCase;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LogLevel;
Expand Down Expand Up @@ -167,4 +168,31 @@ public function testParseMessage(string $message, array $context, string $expect
$message = new Message(LogLevel::INFO, $message, $context);
$this->assertSame($expected, $message->message());
}

public static function dataCategory(): array
{
return [
'without-category' => [Message::DEFAULT_CATEGORY, []],
'null-category' => [Message::DEFAULT_CATEGORY, ['category' => null]],
'with-category' => ['test', ['category' => 'test']],
];
}

/**
* @dataProvider dataCategory
*/
public function testCategory(string $expected, array $context): void
{
$message = new Message(LogLevel::INFO, 'message', $context);
$this->assertSame($expected, $message->category());
}

public function testInvalidCategoryType(): void
{
$message = new Message(LogLevel::INFO, 'message', ['category' => 23.1]);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Invalid category value in log context. Expected "string", got "float".');
$message->category();
}
}