Skip to content

Commit ce7d019

Browse files
authored
Merge pull request #6 from OnrampLab/issue-5-feat-support-attaching-uuid-to-logs
support attaching tracking id to logs Close #5
2 parents 69c14dc + f0a9a16 commit ce7d019

File tree

7 files changed

+151
-16
lines changed

7 files changed

+151
-16
lines changed

README.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
# laravel-log-enhancement
22

33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
4-
[![CircleCI](https://circleci.com/gh/OnrampLab/next-starter.svg?style=shield)]()
4+
[![CircleCI](https://circleci.com/gh/OnrampLab/laravel-log-enhancement.svg?style=shield)](https://circleci.com/gh/OnrampLab/laravel-log-enhancement)
55
[![Total Downloads](https://img.shields.io/packagist/dt/onramplab/laravel-log-enhancement.svg?style=flat-square)](https://packagist.org/packages/onramplab/laravel-log-enhancement)
66

77
A library with logging enhancement. Including:
88

9-
- `LogWithClassPath` trait
10-
- It adds convinient methods for logging to add class path into context.
11-
- `LogglyHandler` class
12-
- It extends monolog's LogglyHandler with tags support
9+
- `LoggerFacade` facade
10+
- It extends default Laravel `Log` facade with logging adding class path and tracking id into context.
11+
- `LogWithClassPath` trait (*deprecated*)
12+
- It adds convinient methods for logging to add class path and tracking id into context.
13+
- `LogglyHandler` class
14+
- It extends monolog's LogglyHandler with tags support
1315

1416
## Install
1517

1618
```bash
1719
composer require onramplab/laravel-log-enhancement
1820
```
1921

20-
2122
## Usage
2223

23-
### LogWithClassPath Trait
24+
### LoggerFacade
25+
26+
Replace the class of `Log` alias to `LoggerFacade` in `config/app.php` as aliases.
27+
28+
```php
29+
'Log' => Onramplab\LaravelLogEnhancement\Facades\LoggerFacade::class,
30+
```
31+
32+
The log json will look like this:
33+
34+
```json
35+
{
36+
"message": "Test",
37+
"context": {
38+
"class_path": "App\\Fake",
39+
"tracking_id": "652c3456-1a17-42b8-9fa7-9bee65e655eb"
40+
},
41+
"level": 200,
42+
"level_name": "INFO",
43+
"channel": "local",
44+
"extra": {},
45+
"timestamp": "2021-01-04T22:47:56.598608-0800"
46+
}
47+
```
48+
49+
### LogWithClassPath Trait (*deprecated*)
50+
2451
Use `LogWithClassPath` trait to let it automatically put class path into log context. You can refer to following code example.
2552

2653
```php
@@ -44,7 +71,8 @@ The log json will look like this:
4471
{
4572
"message": "Test",
4673
"context": {
47-
"class_path": "App\\Fake"
74+
"class_path": "App\\Fake",
75+
"tracking_id": "652c3456-1a17-42b8-9fa7-9bee65e655eb"
4876
},
4977
"level": 200,
5078
"level_name": "INFO",
@@ -84,7 +112,6 @@ return [
84112

85113
```
86114

87-
88115
## Testing
89116

90117
Run the tests with:
@@ -93,17 +120,14 @@ Run the tests with:
93120
vendor/bin/phpunit
94121
```
95122

96-
97123
## Contributing
98124

99125
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
100126

101-
102127
## Security
103128

104129
If you discover any security-related issues, please email kos.huang@onramplab.com instead of using the issue tracker.
105130

106-
107131
## License
108132

109133
The MIT License (MIT). Please see [License File](/LICENSE.md) for more information.# laravel-log-enhancement

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"minimum-stability": "dev",
1414
"prefer-stable":true,
1515
"require": {
16-
"monolog/monolog": "^1.12|^2.0"
16+
"monolog/monolog": "^1.12|^2.0",
17+
"ramsey/uuid": "^4.0.0"
1718
},
1819
"require-dev": {
1920
"fzaninotto/faker": "^1.9@dev",

src/Concerns/LogWithClassPath.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
namespace Onramplab\LaravelLogEnhancement\Concerns;
33

44
use Illuminate\Support\Facades\Log;
5+
use Onramplab\LaravelLogEnhancement\Logger;
56

7+
/**
8+
* @deprecated deprecated since version 0.3.0
9+
*/
610
trait LogWithClassPath {
711
public function debug(string $message, array $context = [])
812
{
@@ -47,11 +51,13 @@ public function emergency(string $message, array $context = [])
4751

4852
protected function log(string $logLevel, string $message, array $context = [])
4953
{
54+
$logger = app()->make(Logger::class);
55+
5056
$className = get_class($this);
5157
$context = array_merge($context, [
5258
'class_path' => $className,
5359
]);
5460

55-
Log::log($logLevel, $message, $context);
61+
$logger->{$logLevel}($message, $context);
5662
}
5763
}

src/Facades/LoggerFacade.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Onramplab\LaravelLogEnhancement\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class LoggerFacade extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'laravel-log-enhancement-logger';
17+
}
18+
}

src/LaravelLogEnhancementServiceProvider.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Onramplab\LaravelLogEnhancement;
44

5+
use Illuminate\Contracts\Foundation\Application;
56
use Illuminate\Support\ServiceProvider;
67
use Illuminate\Support\Facades\Route;
8+
use Psr\Log\LoggerInterface;
79

810
class LaravelLogEnhancementServiceProvider extends ServiceProvider
911
{
@@ -57,8 +59,15 @@ private function routeConfiguration()
5759
public function register()
5860
{
5961
// Register facade
60-
$this->app->singleton('laravel-log-enhancement', function () {
61-
return new LaravelLogEnhancement;
62+
// $this->app->singleton('laravel-log-enhancement', function () {
63+
// return new LaravelLogEnhancement;
64+
// });
65+
$this->app->singleton(Logger::class, function (Application $app) {
66+
return new Logger($app->make(LoggerInterface::class));
67+
});
68+
69+
$this->app->bind('laravel-log-enhancement-logger',function(){
70+
return app()->make(Logger::class);
6271
});
6372
}
6473

src/Logger.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Onramplab\LaravelLogEnhancement;
4+
5+
use Psr\Log\LoggerInterface;
6+
use Ramsey\Uuid\Uuid;
7+
8+
class Logger
9+
{
10+
protected $debugId;
11+
protected $logger;
12+
13+
/**
14+
* Create an instance of the QueryLogger class
15+
*
16+
* @param \Psr\Log\LoggerInterface $logger
17+
* @return void
18+
*
19+
* @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException
20+
* @throws \InvalidArgumentException
21+
* @throws \Exception
22+
*/
23+
public function __construct(LoggerInterface $logger)
24+
{
25+
$this->logger = $logger;
26+
$this->debugId = Uuid::uuid4()->toString();
27+
//
28+
}
29+
30+
public function __call($name, $arguments)
31+
{
32+
$message = $arguments[0];
33+
$context = $arguments[1] ?? [];
34+
35+
// attach caller class
36+
$caller = debug_backtrace();
37+
$caller = $caller[2];
38+
$context['class_path'] = $caller['class'];
39+
40+
// attach tracking_id
41+
$context['tracking_id'] = $this->debugId;
42+
43+
$this->logger->log($name, $message, $context);
44+
}
45+
}

tests/Unit/LoggerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Onramplab\LaravelLogEnhancement\Tests\Unit\Concerns;
4+
5+
use Illuminate\Support\Facades\Log;
6+
use Onramplab\LaravelLogEnhancement\Tests\TestCase;
7+
use Onramplab\LaravelLogEnhancement\Logger;
8+
9+
class LoggerTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*
14+
* @return void
15+
*/
16+
public function log_should_include_class_path_and_uuid_in_context()
17+
{
18+
Log::spy();
19+
$logger = app()->make(Logger::class);
20+
21+
$logger->info('123');
22+
23+
Log
24+
::shouldHaveReceived('log', function ($logLevel, $message, $context) {
25+
return $logLevel === 'info'
26+
&& $message === '123'
27+
&& isSet($context['class_path'])
28+
&& isSet($context['tracking_id']);
29+
})
30+
->once();
31+
}
32+
}

0 commit comments

Comments
 (0)