Skip to content

Commit e4def4b

Browse files
committed
Fix for issue #28, use channel name/config array indexes to fetch log connection and collection when LogCleanerUpper command uses the static model call: LogToDB::model(channel, connection, collection);
This is required when setting logging channels in the logging.php config file anyways.
1 parent 0e8ad2a commit e4def4b

File tree

4 files changed

+36
-43
lines changed

4 files changed

+36
-43
lines changed

phpunit.xml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
processIsolation="false"
9-
stopOnFailure="false">
10-
<testsuites>
11-
<testsuite name="Test Suite danielme85/laravel-log-to-db">
12-
<directory suffix=".php">./tests/</directory>
13-
</testsuite>
14-
</testsuites>
15-
<filter>
16-
<whitelist processUncoveredFilesFromWhitelist="true">
17-
<directory suffix=".php">./src</directory>
18-
<exclude>
19-
<directory>./vendor</directory>
20-
</exclude>
21-
</whitelist>
22-
</filter>
23-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
<exclude>
8+
<directory>./vendor</directory>
9+
</exclude>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="Test Suite danielme85/laravel-log-to-db">
13+
<directory suffix=".php">./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

readme.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Codecov](https://img.shields.io/codecov/c/github/danielme85/laravel-log-to-db.svg?style=flat-square)](https://codecov.io/gh/danielme85/laravel-log-to-db)
88
[![CodeFactor](https://www.codefactor.io/repository/github/danielme85/laravel-log-to-db/badge)](https://www.codefactor.io/repository/github/danielme85/laravel-log-to-db)
99

10-
Custom Laravel 6.x and 5.6+ Log channel handler that can store log events to SQL or MongoDB databases.
10+
Custom Laravel 5.6+ Log channel handler that can store log events to SQL or MongoDB databases.
1111
Uses Laravel native logging functionality.
1212

1313

@@ -20,18 +20,15 @@ Uses Laravel native logging functionality.
2020
* [Processors](#processors)
2121
* [Lumen Installation](#lumen-installation)
2222

23-
| Version | Tested with Laravel |
24-
| :---: | :---: |
25-
| v1.x | 5.6+ |
26-
| v2.1 | 6.x |
27-
| v2.2 | 6.x |
28-
| v2.3 | 6.x |
29-
| v2.4 | 7.x |
3023

31-
> :warning: this add-on is developed to be backwards compatible down to Laravel 5.6+, however as the above table indicates:
32-
> testing of new releases is only performed on specified major Laravel versions.
33-
> Please note that using Laravel 7 with mongodb (optional), currently requires and alpha version of laravel-mongodb.
24+
> :warning: this add-on is developed to be backwards compatible down to Laravel 5.6+
25+
> testing of new releases is only performed on the following major Laravel versions.
3426
27+
| Version | Laravel | PHP |
28+
| :---: | :---: | :---: |
29+
| v1.x | 5.6+ | 7.1,7.2 |
30+
| v2.x | 6.x | 7.2,7.3 |
31+
| v2.4 | 7.x | 7.2,7.3 |
3532

3633
## Installation
3734
Use the composer require or add to composer.json.
@@ -170,6 +167,7 @@ $logsFromChannel = LogDB::model('database')->get(); //Get logs from the 'databas
170167
$logsFromChannel = LogDB::model('customname')->get(); //Get logs from the 'customname' log channel.
171168
$logsFromMysql = LogToDB::model(null, 'mysql')->get(); //Get all logs from the mysql connection (from Laravel database config)
172169
$logsFromMongoDB = LogToDB::model(null, 'mongodb')->get(); //Get all logs from the mongodb connection (from Laravel database config)
170+
$logsFromMysqlTable = LogToDB::model(null, 'mysql', 'table')->get(); //Get all logs from the mysql table: 'table'
173171
```
174172

175173
## Custom Eloquent Model

src/Commands/LogCleanerUpper.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,22 @@ public function handle()
4949
$channels = $this->getLogToDbChannels();
5050

5151
if (!empty($channels)) {
52-
foreach ($channels as $channel) {
52+
foreach ($channels as $name => $channel) {
5353
$maxRecords = $channel['max_records'] ?? $config['purge_log_when_max_records'] ?? false;
5454
$maxHours = $channel['max_hours'] ?? $config['purge_log_when_max_records'] ?? false;
55-
$connection = $channel['connection'] ?? 'default';
56-
$collection = $channel['collection'] ?? 'log';
5755

5856
//delete based on numbers of records
5957
if (!empty($maxRecords) && $maxRecords > 0) {
60-
if (LogToDB::model($connection, $collection)->removeOldestIfMoreThan($maxRecords)) {
61-
$this->warn("Deleted oldest log records on channel: {$connection}->{$collection}, keep max number of records: {$maxRecords}");
58+
if (LogToDB::model($name)->removeOldestIfMoreThan($maxRecords)) {
59+
$this->warn("Deleted oldest records on log channel: {$name}, keep max number of records: {$maxRecords}");
6260
}
6361
}
6462

6563
//delete based on age
6664
if (!empty($maxHours) && $maxHours > 0) {
6765
$time = Carbon::now()->subHours($maxHours)->toDateTimeString();
68-
if (LogToDB::model($connection, $collection)->removeOlderThan($time)) {
69-
$this->warn("Deleted log records on channel: {$connection}->{$collection}, older than: {$time}");
66+
if (LogToDB::model($name)->removeOlderThan($time)) {
67+
$this->warn("Deleted oldest records on log channel: {$name}, older than: {$time}");
7068
}
7169
}
7270
}
@@ -81,9 +79,10 @@ private function getLogToDbChannels()
8179
$list = [];
8280
$logging = config('logging');
8381
if (!empty($logging) && isset($logging['channels']) && !empty($logging['channels'])) {
84-
foreach ($logging['channels'] as $log) {
85-
if (isset($log['via']) && $log['via'] === LogToDbHandler::class) {
86-
$list[] = $log;
82+
foreach ($logging['channels'] as $name => $channel) {
83+
//Only look for the relevant logging class in the config.
84+
if (isset($channel['via']) && $channel['via'] === LogToDbHandler::class) {
85+
$list[$name] = $channel;
8786
}
8887
}
8988
}

src/LogToDB.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ function __construct($loggingConfig = [])
8080
/**
8181
* Return a new LogToDB Module instance.
8282
*
83+
* If specifying 'channel, 'connection' and 'collection' would not be needed (they will be extracted from channel).
84+
* If specifying 'connection' and 'collection', 'channel' is not needed.
85+
*
8386
* @param string|null $channel
8487
* @param string|null $connection
8588
* @param string|null $collection

0 commit comments

Comments
 (0)