Skip to content

Commit

Permalink
Add log* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jul 29, 2023
1 parent 110cc89 commit 7596cdc
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Expand Up @@ -146,6 +146,44 @@ validate(

See more about validator rules in [yiisoft/validator](https://github.com/yiisoft/validator)

### `log_message(string $level, string|stringable $message, array $context = []): void

- `$level` is a log level. Available levels: `emergency`, `alert`, `critical`, `error`, `warning`, `notice`, `info`, `debug`.
- You can use `\Psr\Log\LogLevel` constants:
- `\Psr\Log\LogLevel::EMERGENCY`
- `\Psr\Log\LogLevel::ALERT`
- `\Psr\Log\LogLevel::CRITICAL`
- `\Psr\Log\LogLevel::ERROR`
- `\Psr\Log\LogLevel::WARNING`
- `\Psr\Log\LogLevel::NOTICE`
- `\Psr\Log\LogLevel::INFO`
- `\Psr\Log\LogLevel::DEBUG`.
- `$message` is a log message
- `$context` is a log context

Also, you can use already level-specific functions:
- `log_emergency(string|Stringable $message, array $context = []): void`
- `log_alert(string|Stringable $message, array $context = []): void`
- `log_critical(string|Stringable $message, array $context = []): void`
- `log_error(string|Stringable $message, array $context = []): void`
- `log_warning(string|Stringable $message, array $context = []): void`
- `log_notice(string|Stringable $message, array $context = []): void`
- `log_info(string|Stringable $message, array $context = []): void`
- `log_debug(string|Stringable $message, array $context = []): void`

```php
log_message('info', 'Some info message');
log_message('error', 'Could not authenticate user with ID {user_id}', ['user_id' => $userId]);

log_info('Info message');
log_error('Error message');
log_warning('Warning message');
log_notice('Notice message');
log_debug('Debug message');
log_critical('Critical message');
log_alert('Alert message');
log_emergency('Emergency message');
```

## Looking for more modules?

Expand Down
58 changes: 58 additions & 0 deletions src/logger.php
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

function log_error(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::ERROR, $message, $context);
}

function log_info(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::INFO, $message, $context);
}

function log_warning(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::WARNING, $message, $context);
}

function log_debug(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::DEBUG, $message, $context);
}

function log_notice(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::NOTICE, $message, $context);
}

function log_critical(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::CRITICAL, $message, $context);
}

function log_emergency(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::EMERGENCY, $message, $context);
}

function log_alert(string|Stringable $message, array $context = []): void
{
log_message(LogLevel::ALERT, $message, $context);
}

function log_message(
string $level,
string|stringable $message,

Check failure on line 50 in src/logger.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

InvalidClass

src/logger.php:50:5: InvalidClass: Class, interface or enum stringable has wrong casing (see https://psalm.dev/007)

Check failure on line 50 in src/logger.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-ubuntu-latest

InvalidClass

src/logger.php:50:5: InvalidClass: Class, interface or enum stringable has wrong casing (see https://psalm.dev/007)
array $context = [],
): void {
/**
* @var LoggerInterface $logger

Check failure on line 54 in src/logger.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

UnnecessaryVarAnnotation

src/logger.php:54:13: UnnecessaryVarAnnotation: The @var Psr\Log\LoggerInterface annotation for $logger is unnecessary (see https://psalm.dev/212)

Check failure on line 54 in src/logger.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-ubuntu-latest

UnnecessaryVarAnnotation

src/logger.php:54:13: UnnecessaryVarAnnotation: The @var Psr\Log\LoggerInterface annotation for $logger is unnecessary (see https://psalm.dev/212)
*/
$logger = container(LoggerInterface::class);
$logger->log($level, $message, $context);
}
86 changes: 86 additions & 0 deletions tests/LoggerTest.php
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Xepozz\Shortcut\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Stringable;
use Xepozz\Shortcut\State;
use Xepozz\Shortcut\Tests\Support\StringableClass;
use Yiisoft\Test\Support\Container\SimpleContainer;

final class LoggerTest extends FunctionsTestCase
{
public function testFunctionExist()
{
$this->assertTrue(function_exists('log_error'));
$this->assertTrue(function_exists('log_info'));
$this->assertTrue(function_exists('log_warning'));
$this->assertTrue(function_exists('log_debug'));
$this->assertTrue(function_exists('log_notice'));
$this->assertTrue(function_exists('log_critical'));
$this->assertTrue(function_exists('log_emergency'));
$this->assertTrue(function_exists('log_alert'));
$this->assertTrue(function_exists('log_message'));
}

public function testLogMessage(): void
{
$this->initEnvironment();
$this->expectNotToPerformAssertions();

log_message('error', 'test message', ['some context']);
}

#[DataProvider('dataLogFunctions')]
public function testLogFunctions(string $function, string|Stringable $message, array $context): void
{
$this->initEnvironment();
$this->expectNotToPerformAssertions();

$function($message, $context);
}

public static function dataLogFunctions(): iterable
{
yield 'log_error' => ['log_error', 'message', []];
yield 'log_error' => ['log_error', new StringableClass('message'), ['key' => 'value']];

yield 'log_info' => ['log_info', 'message', []];
yield 'log_info' => ['log_info', new StringableClass('message'), ['key' => 'value']];

yield 'log_warning' => ['log_warning', 'message', []];
yield 'log_warning' => ['log_warning', new StringableClass('message'), ['key' => 'value']];

yield 'log_debug' => ['log_debug', 'message', []];
yield 'log_debug' => ['log_debug', new StringableClass('message'), ['key' => 'value']];

yield 'log_notice' => ['log_notice', 'message', []];
yield 'log_notice' => ['log_notice', new StringableClass('message'), ['key' => 'value']];

yield 'log_alert' => ['log_alert', 'message', []];
yield 'log_alert' => ['log_alert', new StringableClass('message'), ['key' => 'value']];

yield 'log_critical' => ['log_critical', 'message', []];
yield 'log_critical' => ['log_critical', new StringableClass('message'), ['key' => 'value']];

yield 'log_emergency' => ['log_emergency', 'message', []];
yield 'log_emergency' => ['log_emergency', new StringableClass('message'), ['key' => 'value']];
}

public function bootstrapFiles(): iterable
{
yield __DIR__ . '/../src/container.php';
yield __DIR__ . '/../src/logger.php';
}

protected function initEnvironment(): void
{
State::$container = new SimpleContainer([
LoggerInterface::class => new NullLogger(),
]);
}
}
19 changes: 19 additions & 0 deletions tests/Support/StringableClass.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Xepozz\Shortcut\Tests\Support;

use Stringable;

final class StringableClass implements Stringable
{
public function __construct(private string $string)
{
}

public function __toString()
{
return $this->string;
}
}

0 comments on commit 7596cdc

Please sign in to comment.