Skip to content

Commit

Permalink
1. Update phpunit schema to new
Browse files Browse the repository at this point in the history
2. Moving to PHP 8.1 minimal version
3. Add stderr support
  • Loading branch information
roquie committed Oct 11, 2022
1 parent b706a6e commit f805fa4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 59 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright © 2020 spacetab.io, Inc. https://spacetab.io
Copyright © 2022 spacetab.io, Inc. https://spacetab.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Since `2.0.0` logger is non-blocking and based on Amp and Monolog.
I create this library with one target, — I'm sick of always copies
and paste same code to any of my microservices with logs.

This wrapper solves one problem with logs. And it name, — fucking brackets -> [] [] [] [].
This wrapper solves one problem with logs. And this problem, — fucking brackets -> [] [] [] [].

Example, how this wrapper present the logs:

Expand Down Expand Up @@ -79,7 +79,7 @@ $log->register();
$log->getMonolog()->info('Hello world');
```

4) Okay. It's cool. But I have write logs to multiple streams. How I do it?
4) Okay. It's cool. But I have written logs to multiple streams. How I do it?

```php
<?php
Expand All @@ -105,7 +105,7 @@ Simple? Yes, and without fucking brackets.

The MIT License

Copyright © 2020 spacetab.io, Inc. https://spacetab.io
Copyright © 2022 spacetab.io, Inc. https://spacetab.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=8.1",
"amphp/log": "^1.1"
},
"require-dev": {
"symfony/var-dumper": "^4.2 || ^5.0",
"phpunit/phpunit": "^9.1",
"symfony/var-dumper": "^4 || ^5 || ^6",
"phpunit/phpunit": "^9",
"phpstan/phpstan": "^0.12"
},
"autoload": {
Expand Down
41 changes: 15 additions & 26 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix=".php">./tests</directory>
<directory suffix=".php">./vendor</directory>
</exclude>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnError="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory suffix=".php">./tests</directory>
<directory suffix=".php">./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
66 changes: 44 additions & 22 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Spacetab\Logger;

Expand All @@ -11,7 +13,7 @@

final class Logger
{
public const CHANNEL = 'App';
public final const CHANNEL = 'App';

/**
* Pre-defined logger handlers.
Expand All @@ -20,19 +22,8 @@ final class Logger
*/
private array $handlers = [];

/**
* @var string
*/
private string $channel;

/**
* @var Monolog
*/
private Monolog $monolog;

/**
* @var string
*/
private string $level;

/**
Expand All @@ -43,7 +34,6 @@ final class Logger
*/
public function __construct(string $channel = self::CHANNEL, string $level = LogLevel::INFO)
{
$this->channel = $channel;
$this->level = $level;

$this->monolog = new Monolog($channel);
Expand All @@ -54,6 +44,7 @@ public function __construct(string $channel = self::CHANNEL, string $level = Log
*
* @param string $channel
* @param string $level
*
* @return Logger
*/
public static function new(string $channel = self::CHANNEL, string $level = LogLevel::INFO): Logger
Expand All @@ -65,13 +56,27 @@ public static function new(string $channel = self::CHANNEL, string $level = LogL
}

/**
* Return registered default logger.
* Return registered default logger (forward logs to stdout).
*
* @param string $channel
* @param string $level
* @return \Psr\Log\LoggerInterface
*
* @return LoggerInterface
*/
public static function default(string $channel = self::CHANNEL, string $level = LogLevel::INFO): LoggerInterface
{
return self::stdout($channel, $level);
}

/**
* Return registered logger which forward logs to stderr.
*
* @param string $channel
* @param string $level
*
* @return LoggerInterface
*/
public static function stdout(string $channel = self::CHANNEL, string $level = LogLevel::INFO): LoggerInterface
{
$log = new Logger($channel, $level);
$log->addStreamHandler();
Expand All @@ -80,10 +85,28 @@ public static function default(string $channel = self::CHANNEL, string $level =
return $log->getMonolog();
}

/**
* Return registered logger which forward logs to stderr.
*
* @param string $channel
* @param string $level
*
* @return LoggerInterface
*/
public static function stderr(string $channel = self::CHANNEL, string $level = LogLevel::INFO): LoggerInterface
{
$log = new Logger($channel, $level);
$log->addStreamHandler(ByteStream\getStderr());
$log->register();

return $log->getMonolog();
}

/**
* Add Monolog handler use callback.
*
* @param callable $callback
*
* @return Logger
*/
public function addHandler(callable $callback): self
Expand All @@ -109,22 +132,21 @@ public function register(): void
* Create Monolog logger without fucking brackets -> [] [] [] [] [] [] [] [] [] []
* if context and extra is empty.
*/
public function addStreamHandler(): void
public function addStreamHandler(?ByteStream\OutputStream $outputStream = null): void
{
$this->addHandler(function (string $level) {
$outputStream = $outputStream ?: ByteStream\getStdout();

$this->addHandler(function (string $level) use ($outputStream) {
$formatter = new ConsoleFormatter();
$formatter->ignoreEmptyContextAndExtra();

$handler = new StreamHandler(ByteStream\getStdout(), $level);
$handler = new StreamHandler($outputStream, $level);
$handler->setFormatter($formatter);

return $handler;
});
}

/**
* @return Monolog
*/
public function getMonolog(): Monolog
{
return $this->monolog;
Expand Down
15 changes: 11 additions & 4 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Spacetab\Logger\Tests;

Expand All @@ -10,14 +12,14 @@

class LoggerTest extends TestCase
{
public function testHowLoggerWorks()
public function testHowLoggerWorks(): void
{
global $globalMustDieOrNot;

$log = new Logger();
$log->addStreamHandler();
$log->addHandler(function (): HandlerInterface {
return new class() extends AbstractHandler implements HandlerInterface {
return new class() extends AbstractHandler {
public function handle(array $record): bool {
global $globalMustDieOrNot;
$globalMustDieOrNot[] = $record;
Expand All @@ -35,7 +37,7 @@ public function handle(array $record): bool {
}
}

public function testHowWorksStaticMethods()
public function testHowWorksStaticMethods(): void
{
$log = Logger::default();

Expand All @@ -46,5 +48,10 @@ public function testHowWorksStaticMethods()

$log = Logger::new();
$this->assertInstanceOf(Logger::class, $log);

$log = Logger::stderr();

$this->assertInstanceOf(\Monolog\Logger::class, $log);
$this->assertInstanceOf(LoggerInterface::class, $log);
}
}

0 comments on commit f805fa4

Please sign in to comment.