https://github.com/forecho/laravel-trace-log
Trace Log for Laravel/Lumen
https://github.com/forecho/laravel-trace-log
laravel laravel-package tracelog
Last synced: about 1 year ago
JSON representation
Trace Log for Laravel/Lumen
- Host: GitHub
- URL: https://github.com/forecho/laravel-trace-log
- Owner: forecho
- License: mit
- Created: 2022-01-17T07:44:23.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-11T03:10:15.000Z (almost 4 years ago)
- Last Synced: 2025-06-11T23:47:18.798Z (about 1 year ago)
- Topics: laravel, laravel-package, tracelog
- Language: PHP
- Homepage:
- Size: 21.5 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Trace Log for Laravel/Lumen
## Install
```shell
composer require forecho/laravel-trace-log
```
if you use Lumen, you need add service provider to your `bootstrap/app.php` file.
```php
$app->register(Forecho\LaravelTraceLog\TraceLogServiceProvider::class);
```
## Configuration
This step is optional
```shell
php artisan vendor:publish --provider="Forecho\LaravelTraceLog\TraceLogServiceProvider"
```
if you use Lumen, you need add config file to your `config` directory.
```shell
cp vendor/forecho/laravel-trace-log/config/tracelog.php config/
```
and you need add code to your `bootstrap/app.php` file.
```php
$app->configure('tracelog');
```
**Note**
- The number of bits of the value corresponding to the key in `log_filter_half_hide_keys` must be greater than 2,
otherwise all data is hidden.
- If the configuration file causes an error, the filtering function will be invalid, the error message can be searched
for `request_params_filter_key_config_error` to view the log.
`additional_fields` is an array of additional fields that will be added to the log. eg:
```php
'additional_fields' => [
'user_id' => [App\Models\User::class, 'getId'],
'tag' => 'test', // value can use closure、string、array
],
```
add `getId()` method to User model, and the value of the field will be the return value of the method.
```php
namespace App\Models;
class User
{
public static function getId(): ?int
{
return data_get(Auth::user(), 'id');
}
}
```
## Usage
### Middleware(Optional)
change `App\Http\Kernel.php` file to add `TraceLogMiddleware` middleware.
```php
use Forecho\LaravelTraceLog\Middleware\TraceLogMiddleware;
protected $middlewareGroups = [
// ...
'api' => [
// ...
'request.tracelog'
],
];
protected $routeMiddleware = [
// ...
'request.tracelog' => TraceLogMiddleware::class
];
```
### Logging
```php
use Forecho\LaravelTraceLog\TraceLog;
TraceLog::warning('This is a warning message.', ['foo' => 'bar']);
TraceLog::error('This is an error message.', ['foo' => 'bar']);
TraceLog::info('This is an info message.', ['foo' => 'bar']);
TraceLog::debug('This is a debug message.', ['foo' => 'bar']);
```
### Get Trace ID
```php
use Forecho\LaravelTraceLog\TraceLog;
TraceLog::getTraceId();
```
### Curl Request
if you want next system use the same trace_id, you need add `trace_id` to your `header`
```php
use Forecho\LaravelTraceLog\TraceLog;
$key = config('tracelog.trace_id_header_key');
$headers = [
$key => TraceLog::getTraceId(),
]
```