Skip to content

Commit

Permalink
Fix for issue #28, use channel name/config array indexes to fetch log…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
danielme85 committed Sep 3, 2020
1 parent 0e8ad2a commit e4def4b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 43 deletions.
37 changes: 15 additions & 22 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite danielme85/laravel-log-to-db">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
<exclude>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
<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">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory>./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Test Suite danielme85/laravel-log-to-db">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
20 changes: 9 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![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)
[![CodeFactor](https://www.codefactor.io/repository/github/danielme85/laravel-log-to-db/badge)](https://www.codefactor.io/repository/github/danielme85/laravel-log-to-db)

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


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

| Version | Tested with Laravel |
| :---: | :---: |
| v1.x | 5.6+ |
| v2.1 | 6.x |
| v2.2 | 6.x |
| v2.3 | 6.x |
| v2.4 | 7.x |

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

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

## Custom Eloquent Model
Expand Down
19 changes: 9 additions & 10 deletions src/Commands/LogCleanerUpper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,22 @@ public function handle()
$channels = $this->getLogToDbChannels();

if (!empty($channels)) {
foreach ($channels as $channel) {
foreach ($channels as $name => $channel) {
$maxRecords = $channel['max_records'] ?? $config['purge_log_when_max_records'] ?? false;
$maxHours = $channel['max_hours'] ?? $config['purge_log_when_max_records'] ?? false;
$connection = $channel['connection'] ?? 'default';
$collection = $channel['collection'] ?? 'log';

//delete based on numbers of records
if (!empty($maxRecords) && $maxRecords > 0) {
if (LogToDB::model($connection, $collection)->removeOldestIfMoreThan($maxRecords)) {
$this->warn("Deleted oldest log records on channel: {$connection}->{$collection}, keep max number of records: {$maxRecords}");
if (LogToDB::model($name)->removeOldestIfMoreThan($maxRecords)) {
$this->warn("Deleted oldest records on log channel: {$name}, keep max number of records: {$maxRecords}");
}
}

//delete based on age
if (!empty($maxHours) && $maxHours > 0) {
$time = Carbon::now()->subHours($maxHours)->toDateTimeString();
if (LogToDB::model($connection, $collection)->removeOlderThan($time)) {
$this->warn("Deleted log records on channel: {$connection}->{$collection}, older than: {$time}");
if (LogToDB::model($name)->removeOlderThan($time)) {
$this->warn("Deleted oldest records on log channel: {$name}, older than: {$time}");
}
}
}
Expand All @@ -81,9 +79,10 @@ private function getLogToDbChannels()
$list = [];
$logging = config('logging');
if (!empty($logging) && isset($logging['channels']) && !empty($logging['channels'])) {
foreach ($logging['channels'] as $log) {
if (isset($log['via']) && $log['via'] === LogToDbHandler::class) {
$list[] = $log;
foreach ($logging['channels'] as $name => $channel) {
//Only look for the relevant logging class in the config.
if (isset($channel['via']) && $channel['via'] === LogToDbHandler::class) {
$list[$name] = $channel;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/LogToDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function __construct($loggingConfig = [])
/**
* Return a new LogToDB Module instance.
*
* If specifying 'channel, 'connection' and 'collection' would not be needed (they will be extracted from channel).
* If specifying 'connection' and 'collection', 'channel' is not needed.
*
* @param string|null $channel
* @param string|null $connection
* @param string|null $collection
Expand Down

0 comments on commit e4def4b

Please sign in to comment.