Skip to content

Commit 7701b85

Browse files
authored
Merge pull request #2 from OnrampLab/issue-1-feat-can-log-classpath-to-context
can log classpath to context Close #1
2 parents c562d54 + a608c38 commit 7701b85

File tree

10 files changed

+184
-53
lines changed

10 files changed

+184
-53
lines changed

.circleci/config.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: 2.1
2+
3+
commands:
4+
run_test:
5+
description: "Run test"
6+
steps:
7+
- checkout
8+
- restore_cache:
9+
key: dependency-cache-composer-{{ checksum "composer.json" }}
10+
- run:
11+
name: Install php extensions
12+
command: |
13+
sudo docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd
14+
sudo docker-php-ext-install pcntl pdo_mysql
15+
- run:
16+
name: Install composer packages
17+
command: |
18+
composer install
19+
- save_cache:
20+
key: dependency-cache-composer-{{ checksum "composer.json" }}
21+
paths:
22+
- ~/app/vendor
23+
- run:
24+
name: Do static analysis
25+
command: |
26+
vendor/bin/phpstan
27+
- run:
28+
name: Test
29+
command: |
30+
vendor/bin/phpunit -d memory_limit=386M
31+
32+
jobs:
33+
run_test:
34+
working_directory: ~/app
35+
docker:
36+
- image: circleci/php:7.4.3
37+
steps:
38+
- run_test
39+
40+
workflows:
41+
version: 2
42+
build-deploy:
43+
jobs:
44+
- run_test:
45+
filters:
46+
tags:
47+
only: /^v.*/
48+
branches:
49+
only: /.*/

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/composer.lock
55
/.DS_Store
66
/.idea
7-
/.php_cs.cache
7+
/.php_cs.cache
8+
/.phpunit.result.cache

composer.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "onramplab/laravel-log-enhancement",
3-
"description": "Your Package Description here",
3+
"description": "An enhanced logging package for Laravel",
44
"type": "library",
55
"license": "MIT",
66
"keywords": [],
@@ -14,13 +14,15 @@
1414
"prefer-stable":true,
1515
"require": {},
1616
"require-dev": {
17-
"symfony/thanks": "^1.0",
18-
"phpunit/phpunit": "^7.4@dev",
19-
"mockery/mockery": "^1.0@dev",
20-
"orchestra/testbench": "^3.8@dev",
21-
"orchestra/database": "^3.8@dev",
22-
"illuminate/support": "^5.8@dev",
23-
"fzaninotto/faker": "^1.9@dev"
17+
"fzaninotto/faker": "^1.9@dev",
18+
"illuminate/support": "^7.0",
19+
"mockery/mockery": "^1.3.2",
20+
"orchestra/database": "^5.0",
21+
"orchestra/testbench": "^5.0",
22+
"phpstan/phpstan": "^0.12.64",
23+
"phpunit/phpunit": "^8.0",
24+
"sempro/phpunit-pretty-print": "1.2.2",
25+
"symfony/thanks": "^1.0"
2426
},
2527
"autoload": {
2628
"psr-4": {

phpstan.neon

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
includes:
2+
- ./vendor/nunomaduro/larastan/extension.neon
3+
4+
parameters:
5+
6+
paths:
7+
- src
8+
9+
# The level 8 is the highest level
10+
level: 0
11+
12+
# ignoreErrors:
13+
# - '#Unsafe usage of new static#'
14+
15+
excludes_analyse:
16+
- ./*/*/Tests/*.php
17+
- ./*/*/factories/*.php
18+
- ./*/*/Migrations/*.php
19+
20+
checkMissingIterableValueType: false

phpunit.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<phpunit bootstrap="vendor/autoload.php"
33
backupGlobals="false"
44
backupStaticAttributes="false"
5+
printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter"
56
colors="true"
67
verbose="true"
78
convertErrorsToExceptions="true"
@@ -30,4 +31,4 @@
3031
<env name="DB_CONNECTION" value="sqlite"/>
3132
<env name="DB_DATABASE" value=":memory:"/>
3233
</php>
33-
</phpunit>
34+
</phpunit>

src/Concerns/LogWithClassPath.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace Onramplab\LaravelLogEnhancement\Concerns;
3+
4+
use Illuminate\Support\Facades\Log;
5+
6+
trait LogWithClassPath {
7+
public function debug(string $message, array $context = [])
8+
{
9+
$this->log('debug', $message, $context);
10+
}
11+
12+
public function info(string $message, array $context = [])
13+
{
14+
$this->log('info', $message, $context);
15+
}
16+
17+
public function notice(string $message, array $context = [])
18+
{
19+
$this->log('notice', $message, $context);
20+
}
21+
22+
public function warning(string $message, array $context = [])
23+
{
24+
$this->log('warning', $message, $context);
25+
}
26+
27+
public function error(string $message, array $context = [])
28+
{
29+
$this->log('error', $message, $context);
30+
}
31+
32+
public function critical(string $message, array $context = [])
33+
{
34+
$this->log('critical', $message, $context);
35+
}
36+
37+
38+
public function alert(string $message, array $context = [])
39+
{
40+
$this->log('alert', $message, $context);
41+
}
42+
43+
public function emergency(string $message, array $context = [])
44+
{
45+
$this->log('emergency', $message, $context);
46+
}
47+
48+
protected function log(string $logLevel, string $message, array $context = [])
49+
{
50+
$className = get_class($this);
51+
$context = array_merge($context, [
52+
'class_path' => $className,
53+
]);
54+
55+
Log::log($logLevel, $message, $context);
56+
}
57+
}

tests/Feature/ExampleTest.php

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

tests/TestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public function setup() : void
1111
{
1212
parent::setUp();
1313
$this->withoutExceptionHandling();
14-
$this->artisan('migrate', ['--database' => 'testing']);
14+
// $this->artisan('migrate', ['--database' => 'testing']);
1515

16-
$this->loadMigrationsFrom(__DIR__ . '/../src/database/migrations');
17-
$this->loadLaravelMigrations(['--database' => 'testing']);
16+
// $this->loadMigrationsFrom(__DIR__ . '/../src/database/migrations');
17+
// $this->loadLaravelMigrations(['--database' => 'testing']);
1818

19-
$this->withFactories(__DIR__.'/../src/database/factories');
19+
// $this->withFactories(__DIR__.'/../src/database/factories');
2020
}
2121

2222
protected function getEnvironmentSetUp($app)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Onramplab\LaravelLogEnhancement\Tests\Unit\Concerns;
4+
5+
use Onramplab\LaravelLogEnhancement\Tests\TestCase;
6+
use Illuminate\Support\Facades\Log;
7+
use Onramplab\LaravelLogEnhancement\Concerns\LogWithClassPath;
8+
9+
class LogWithClassPathTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*
14+
* @return void
15+
*/
16+
public function log_should_include_class_path_in_context()
17+
{
18+
$this->object = new Fake();
19+
Log::spy();
20+
21+
$this->object->run();
22+
23+
Log
24+
::shouldHaveReceived('log', function ($logLevel, $message, $context) {
25+
return $logLevel === 'info'
26+
&& $message === 'Test'
27+
&& $context['class_path'] === 'Onramplab\LaravelLogEnhancement\Tests\Unit\Concerns\Fake';
28+
})
29+
->once();
30+
}
31+
}
32+
33+
class Fake {
34+
use LogWithClassPath;
35+
36+
public function run()
37+
{
38+
$this->info('Test');
39+
}
40+
}

tests/Unit/ExampleTest.php

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

0 commit comments

Comments
 (0)