Skip to content

Commit 169a205

Browse files
committed
Add support for and additional instructions to install Lumen.
1 parent 1b99d00 commit 169a205

File tree

5 files changed

+119
-8
lines changed

5 files changed

+119
-8
lines changed

readme.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Uses Laravel native logging functionality.
1818
* [Custom Eloquent Model](#custom-eloquent-model)
1919
* [Log Cleanup](#log-cleanup)
2020
* [Processors](#processors)
21+
* [Lumen Innstallation](#lumen-installation)
2122

2223

2324
## Installation
@@ -367,6 +368,106 @@ class PhpVersionProcessor implements ProcessorInterface {
367368
]
368369
```
369370

371+
## Lumen Installation
372+
373+
* Create a config folder in your projects root directory (if you don't already have one), then add a logging.php config file there.
374+
Use the [Laravel logging.php](https://github.com/laravel/laravel/blob/master/config/logging.php) config file as an example/starting point.
375+
You can also add all the other "missing" config files from the Laravel config folder to your Lumen project.
376+
377+
* To use these config files you have to load them in your applications bootstrap/app.php file (or add your own service provider file and load it in that same file).
378+
379+
You also need to make sure that all the needed basic config values for logtodb is set by either:
380+
* Copy over logtodb.php from the config folder of this addon,
381+
* or just add all your log-to-db options in your applications config/logging.php file (probably easiest). Just follow the
382+
configuration example above under the [configuration](#configuration) section.
383+
384+
```
385+
/*
386+
|--------------------------------------------------------------------------
387+
| Register Config Files
388+
|--------------------------------------------------------------------------
389+
|
390+
| Now we will register the "app" configuration file. If the file exists in
391+
| your configuration directory it will be loaded; otherwise, we'll load
392+
| the default version. You may register other files below as needed.
393+
|
394+
*/
395+
396+
$app->configure('app');
397+
//$app->configure('logtodb'); //if you copied over and want to use the base config from logtodb.
398+
$app->configure('logging');
399+
400+
```
401+
402+
Next step is to register the service provider, either in bootstrap/app.php or in app/Provider/AppServiceProvider.
403+
```
404+
/*
405+
|--------------------------------------------------------------------------
406+
| Register Service Providers
407+
|--------------------------------------------------------------------------
408+
|
409+
| Here we will register all of the application's service providers which
410+
| are used to bind services into the container. Service providers are
411+
| totally optional, so you are not required to uncomment this line.
412+
|
413+
*/
414+
415+
// $app->register(App\Providers\AppServiceProvider::class);
416+
// $app->register(App\Providers\AuthServiceProvider::class);
417+
// $app->register(App\Providers\EventServiceProvider::class);
418+
$app->register(\danielme85\LaravelLogToDB\ServiceProvider::class);
419+
```
420+
421+
After adding the service provider you should be able to run the database migration with:
422+
```
423+
php artisan migrate
424+
```
425+
Please note that you need a working db connection in Lumen at this point.
426+
427+
And then maybe it works... ¯\_(ツ)_
428+
429+
#### Using worker queue to write log to db with Lumen
430+
You would need to set up a queue driver in Lumen before you can use the queue (default is: QUEUE_CONNECTION=sync, which is basically no queue).
431+
More info about the [queues in Lumen doc](https://lumen.laravel.com/docs/7.x/queues) (they are mostly the same as Laravel).
432+
I would recommend the Redis queue driver but database should also work.
433+
434+
#### How to make Redis work in Lumen (in general).
435+
install per command
436+
```
437+
composer require predis/predis
438+
composer require illuminate/redis
439+
```
440+
441+
OR add to composer.json
442+
```
443+
...
444+
"require": {
445+
"predis/predis": "~1.0",
446+
"illuminate/redis": "5.0.*",
447+
...
448+
```
449+
Or install other alternatives to predis.
450+
451+
Add service provider and enable eloquent in your bootstrap/app.php file (Eloquent only needed if you use the model/model helper class to fetch new log event);
452+
```
453+
454+
$app->withFacades();
455+
$app->withEloquent();
456+
457+
$app->register(Illuminate\Redis\RedisServiceProvider::class);
458+
if (!class_exists('Redis')) {
459+
class_alias('Illuminate\Support\Facades\Redis', 'Redis');
460+
}
461+
```
462+
463+
Now you can set your .env values to use Redis for the queue and cache if you so please:
464+
```
465+
REDIS_CLIENT=predis
466+
QUEUE_CONNECTION=redis
467+
CACHE_DRIVER=redis
468+
```
469+
470+
370471
Development supported by:
371472
<br>
372473
![](https://media.giphy.com/media/3knKct3fGqxhK/giphy.gif)

src/Jobs/SaveNewLogEvent.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
use Illuminate\Bus\Queueable;
88
use Illuminate\Queue\InteractsWithQueue;
99
use Illuminate\Contracts\Queue\ShouldQueue;
10-
use Illuminate\Foundation\Bus\Dispatchable;
1110

1211
class SaveNewLogEvent implements ShouldQueue
1312
{
14-
use Dispatchable, InteractsWithQueue, Queueable;
13+
use InteractsWithQueue, Queueable;
1514

1615
/**
1716
* @var object
@@ -50,7 +49,7 @@ public function handle()
5049
$this->logToDb->getConfig('detailed')
5150
);
5251
$log->save();
53-
} catch (\Exception $e) {
52+
} catch (\Throwable $e) {
5453
throw new DBLogException($e->getMessage());
5554
}
5655
}

src/LogToDB.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ public function getModel()
122122
//Use custom model
123123
if (!empty($this->model)) {
124124
return new $this->model;
125-
}
126-
else if ($this->database['driver'] === 'mongodb') {
125+
} else if ($this->database['driver'] === 'mongodb') {
127126
//MongoDB has its own Model
128127
$mongo = new DBLogMongoDB();
129128
$mongo->bind($this->connection, $this->collection);
@@ -151,7 +150,7 @@ public function newFromMonolog(array $record)
151150
if (!empty($this->connection)) {
152151
if ($detailed && !empty($record['context']) && !empty($record['context']['exception'])) {
153152
$record['context'] = $this->parseIfException($record['context']);
154-
} else if (!$detailed){
153+
} else if (!$detailed) {
155154
$record['context'] = null;
156155
}
157156
if ($this->config['queue']) {
@@ -205,8 +204,11 @@ private function parseIfException($context)
205204
$exception = $context['exception'];
206205
if (is_object($exception)) {
207206
if (get_class($exception) === \Exception::class
207+
|| get_class($exception) === \Throwable::class
208208
|| is_subclass_of($exception, \Exception::class)
209-
|| strpos(get_class($exception), "Exception") !== false) {
209+
|| is_subclass_of($exception, \Throwable::class)
210+
|| strpos(get_class($exception), "Exception") !== false
211+
|| strpos(get_class($exception), "Throwable") !== false) {
210212

211213
$newexception = [];
212214

src/LogToDbCustomLoggingHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function write(array $record): void
6464
$log->newFromMonolog($record);
6565
} catch (DBLogException $e) {
6666
//do nothing if exception of self
67-
} catch (\Exception $e) {
67+
} catch (\Throwable $e) {
6868
//convert any runtime Exception while logging to a special class so we can avoid our own
6969
//exceptions for 99% less infinite loops!
7070
throw new DBLogException($e->getMessage());

src/ServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class ServiceProvider extends Provider {
2525
*/
2626
public function boot()
2727
{
28+
//config path is missing in Lumen
29+
//https://gist.github.com/mabasic/21d13eab12462e596120
30+
if (!function_exists('config_path')) {
31+
function config_path($path = '')
32+
{
33+
return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
34+
}
35+
}
36+
2837
$this->loadMigrationsFrom(__DIR__.'/migrations');
2938
$this->mergeConfigFrom(__DIR__.'/config/logtodb.php', 'logtodb');
3039
$this->publishes([

0 commit comments

Comments
 (0)