Skip to content

Commit 4873c46

Browse files
Implement as Laravel Logging channel (#7)
* Play nice with Laravel Logging channels/drivers
1 parent e5ec5c5 commit 4873c46

21 files changed

+122
-612
lines changed

.github/workflows/laravel-test.yaml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
php: [ 7.2, 7.3, 7.4 ]
16-
#laravel: [~5.6.0, ~5.7.0, ~5.8.0, ~5.9.0, ^6.0, ^7.0, ^8.0]
15+
php: [ 7.2, 7.3, 7.4, 8.0 ]
1716
laravel: [ ^6.0, ^7.0, ^8.0 ]
1817
exclude:
1918
- php: 7.2
2019
laravel: ^8.0
20+
- php: 8.0
21+
laravel: ^6.0
22+
- php: 8.0
23+
laravel: ^7.0
2124

2225
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
2326

@@ -48,11 +51,15 @@ jobs:
4851
- name: Add local path composer dependency
4952
run: |
5053
cd laravel-app
51-
jq '.repositories[0].type="path"' composer.json > composer.temp.json
52-
jq '.repositories[0].url="../console-logg-package"' composer.json > composer.temp.json
53-
jq '.require."devthis/console-logg"="*"' composer.json > composer.temp.json
54-
mv composer.temp.json composer.json
55-
composer update --no-interaction --no-progress --ignore-platform-reqs
54+
export PHPV=$(php -r "echo implode('.', [PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION]);")
55+
cat <<< $(jq '.repositories[0].type="path"' composer.json) > composer.json
56+
cat <<< $(jq '.repositories[0].url="../console-logg-package"' composer.json) > composer.json
57+
cat <<< $(jq '.require."devthis/console-logg"="*"' composer.json) > composer.json
58+
cat <<< $(jq '.require."psr/log"="1.1.4"' composer.json) > composer.json
59+
cat <<< $(jq --arg phpv "$PHPV" '.config.platform.php=$phpv' composer.json) > composer.json
60+
cat <<< $(jq '.extra.symfony.require="5.4"' composer.json) > composer.json
61+
cat <<< $(jq '.require.php="^${{ matrix.php }}"' composer.json) > composer.json
62+
5663
5764
- name: Copy fixtures into Laravel app
5865
run: |
@@ -63,10 +70,20 @@ jobs:
6370
- name: Run Laravel package discovery
6471
run: |
6572
cd laravel-app
73+
ls -lha
74+
cat composer.json
75+
composer update --no-interaction --no-progress
6676
php artisan package:discover --ansi
6777
php artisan clear-compiled
6878
6979
- name: Run PHPT
80+
if: matrix.php != '8.0'
81+
run: |
82+
cd console-logg-package
83+
vendor/bin/phpunit tests/PhpTestCase/artisan-command-output.phpt --testdox
84+
85+
- name: Run PHPT
86+
if: matrix.php == '8.0'
7087
run: |
7188
cd console-logg-package
72-
vendor/bin/phpunit tests/PhpTestCase/ --testdox
89+
vendor/bin/phpunit tests/PhpTestCase/artisan-command-output-php8.phpt --testdox

.github/workflows/phpunit-tests.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ jobs:
2424
strategy:
2525
fail-fast: false
2626
matrix:
27-
php: [ 7.2, 7.3, 7.4 ]
27+
php: [ 7.2, 7.3, 7.4, 8.0 ]
2828
#laravel: [~5.6.0, ~5.7.0, ~5.8.0, ~5.9.0, ^6.0, ^7.0, ^8.0]
2929
laravel: [ ^6.0, ^7.0, ^8.0 ]
3030
exclude:
3131
- php: 7.2
3232
laravel: ^8.0
33+
- php: 8.0
34+
laravel: ^6.0
35+
- php: 8.0
36+
laravel: ^7.0
3337

3438
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
3539

README.md

Lines changed: 23 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ Powered by [Symfony's Console Logger](https://symfony.com/doc/current/components
2727

2828
# What does it do?
2929

30-
**ConsoleLogg provides output messages for your artisan commands, between your shared code which is using the built-in
31-
Laravel logger**
30+
**Logger channel for sharing messages between application and console commands**
3231

3332
Typically, this requires a hacky solution, such as coupling your shared services with a console logger, or configuring
3433
multiple driver channels.
3534

3635
With ConsoleLogg you can have logs for your artisan commands, and behave as usual with http/controllers.
3736

38-
**Use your Laravel logger throughout your application shared services as usual**
37+
**No code changes are required with logger usage**
3938

39+
Supported:
4040
- _Dependency Injection/autowiring_ `LoggerInterface $logger` & `$logger->debug("yeet")`
4141
- `logger()->critical("Send help")`
4242
- `Log::alert("She find pictures in my email")`
@@ -57,42 +57,33 @@ php artisan my:command -vvv
5757

5858
```shell script
5959
composer require devthis/console-logg
60-
61-
# to optionally copy config
62-
php artisan vendor:publish --provider="DevThis\ConsoleLogg\Providers\ConsoleLoggServiceProvider"
63-
```
60+
```
61+
62+
2. Enable logging channel `console-logg`
63+
64+
`config/logging.php`
65+
```diff
66+
'channels' => [
67+
'stack' => [
68+
'driver' => 'stack',
69+
- 'channels' => ['single'],
70+
+ 'channels' => ['console-logg', 'single'],
71+
'ignore_exceptions' => false,
72+
],
73+
```
74+
75+
[Laravel - Logging Docs](https://laravel.com/docs/8.x/logging#building-log-stacks)
6476

6577
### Compatibility
6678

6779
| Compatible | Laravel | PHP |
6880
|--------------------|-----------|-----------|
69-
| :heavy_check_mark: | 8.* | PHP >=7.1 |
81+
| :heavy_check_mark: | 8.* | PHP >=7.1 + 8.* |
7082
| :heavy_check_mark: | 7.* | PHP >=7.1 |
7183
| :heavy_check_mark: | 6.* | PHP >=7.1 |
72-
| :heavy_exclamation_mark: :soon: | > 5.6 | PHP >=7.1 |
73-
74-
**PHP 8 compatibility is currently being tested**
75-
76-
#### Compatibility assurance
77-
78-
<details>
79-
<summary>Click to find out how we ensure compatibility</summary>
80-
81-
Compatibility is thoroughly tested using a combination of real world tests (functional), and unit tests
82-
83-
[ConsoleLogg test suite](/test) is run in isolation
84-
85-
- Using PHP 7.1, 7.2, 7.3,
86-
7.4, [each major version of Laravel is independently tested against](actions?query=workflow%3A"PHPUnit+suite")
87-
- _Laravel 5.6, 5.7 & 5.8 are tested individually on top of this_
88-
89-
Unit tests ensure type-compatibility, expected behaviour is met & compatibility with each version of Laravel contracts
90-
91-
[TODO] Functional tests ensure real world expectations are through a real Laravel application
92-
93-
</details>
9484

9585
---
86+
9687
# Features
9788

9889
## Artisan serve supported
@@ -106,33 +97,14 @@ There are no traits, classes, interfaces that you need to use. ConsoleLogg does
10697

10798
The ConsoleLog Service Provider should be automatically added to your app, but if it hasn't, you can add it yourself to `config/app.php`
10899
```php
109-
// generally only required when you have composer intalled with --no-scripts
100+
// generally only required when you have composer installed with --no-scripts
110101

111102
'providers' => [
112103
//...
113104
\DevThis\ConsoleLogg\Providers\ConsoleLoggServiceProvider::class,
114105
];
115106
```
116107

117-
## Settings
118-
119-
See [config/console-logg.php](config/console-logg.php) for the raw configuration file
120-
121-
### Filtering
122-
123-
> default = `false`
124-
125-
If you choose to enable filtering, logs will only be output to artisan console commands if they have the context property `logg` set to `true`
126-
127-
eg.
128-
129-
```php
130-
logger()->info("Informative message #1", ['logg' => true]);
131-
logger()->alert("Nice one");
132-
```
133-
134-
With these logs being invoked by your artisan command, only `[info] Informative message #1` will be output
135-
136108
## Command-in-command
137109

138110
ConsoleLogg has (not yet) been tested for compatibility using artisan commands in a command
@@ -150,7 +122,7 @@ class MyConsoleApp extends Command
150122

151123
public function handle(): int
152124
{
153-
//other:command may invoke servies that use the Laravel Logger
125+
//other:command may invoke services that use the Laravel Logger
154126
//these logs will still output to this current console
155127
$this->call('other:command');
156128
//...

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
],
2121
"license": "MIT",
2222
"require": {
23-
"php": "7.1.3 - 8.0",
23+
"php": "7.*|8.*",
2424
"illuminate/log": ">=6.0",
2525
"illuminate/console": ">=6.0",
26-
"illuminate/support": ">=6.0"
26+
"illuminate/support": ">=6.0",
27+
"psr/log": ">=1.1.4"
2728
},
2829
"extra": {
2930
"laravel": {
@@ -43,6 +44,6 @@
4344
}
4445
},
4546
"require-dev": {
46-
"phpunit/phpunit": "^7.0"
47+
"phpunit/phpunit": "^8.0"
4748
}
4849
}

config/console-logg.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

phpunit.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
<directory suffix=".phpt">test</directory>
1616
</testsuite>
1717
</testsuites>
18-
18+
<php>
19+
<env name="LOG_CHANNEL" value="console-logg" force="true" />
20+
</php>
1921
<filter>
2022
<whitelist processUncoveredFilesFromWhitelist="true">
2123
<directory suffix=".php">./src</directory>

src/Binder/LogOutputBinder.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace DevThis\ConsoleLogg\Binder;
66

7+
use DevThis\ConsoleLogg\Console\ConsoleLogg;
78
use DevThis\ConsoleLogg\Interfaces\Binder\LogOutputBindedInterface;
8-
use DevThis\ConsoleLogg\Interfaces\Factories\FilterableConsoleLoggerFactoryInterface;
99
use Illuminate\Contracts\Config\Repository;
1010
use Illuminate\Log\LogManager;
1111
use Symfony\Component\Console\Output\OutputInterface;
@@ -17,33 +17,9 @@
1717
*/
1818
class LogOutputBinder implements LogOutputBindedInterface
1919
{
20-
/**
21-
* @var FilterableConsoleLoggerFactoryInterface
22-
*/
23-
private $consoleLoggerFactory;
24-
25-
/**
26-
* @var string|null
27-
*/
28-
private $defaultDriver = null;
29-
30-
/**
31-
* @var bool
32-
*/
33-
private $isFiltered;
34-
35-
public function __construct(FilterableConsoleLoggerFactoryInterface $consoleLoggerFactory, Repository $config)
36-
{
37-
$this->isFiltered = $config->get('console-logg.filtered') === true;
38-
$this->consoleLoggerFactory = $consoleLoggerFactory;
39-
}
40-
4120
public function attach(OutputInterface $output, LogManager $logManager): void
4221
{
43-
$this->defaultDriver = $logManager->getDefaultDriver();
44-
$logManager->setDefaultDriver('console-logg');
45-
46-
$consoleLogger = $this->consoleLoggerFactory->create($output, $this->isFiltered);
22+
$consoleLogger = new ConsoleLogg($output);
4723

4824
$logManager->extend(
4925
'console-logg',
@@ -56,6 +32,5 @@ function () use ($consoleLogger) {
5632
public function detach(LogManager $logManager): void
5733
{
5834
$logManager->forgetChannel('console-logg');
59-
$logManager->setDefaultDriver($this->defaultDriver);
6035
}
6136
}

src/Console/ConsoleLogg.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DevThis\ConsoleLogg\Console;
6+
7+
use Monolog\Handler\HandlerInterface;
8+
use Symfony\Component\Console\Logger\ConsoleLogger;
9+
10+
class ConsoleLogg extends ConsoleLogger implements HandlerInterface
11+
{
12+
public function getHandlers(): array
13+
{
14+
return [$this];
15+
}
16+
17+
public function getProcessors(): array
18+
{
19+
return [];
20+
}
21+
22+
public function isHandling(array $record): bool
23+
{
24+
return true;
25+
}
26+
27+
public function handle(array $record): bool
28+
{
29+
$this->log(strtolower($record['level_name'] ?? ''), $record['message'] ?? '', $record['context'] ?? []);
30+
31+
return true;
32+
}
33+
34+
public function handleBatch(array $records): void
35+
{
36+
foreach ($records as $record) {
37+
$this->handle($record);
38+
}
39+
}
40+
41+
public function close(): void
42+
{
43+
}
44+
}

src/Console/FilterableConsoleLogger.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)