Skip to content

Commit 93637c2

Browse files
committed
first working version 👏 ... needs tests though 😰
1 parent 911cc40 commit 93637c2

13 files changed

+333
-96
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"extra": {
2626
"laravel": {
2727
"providers": [
28-
"danielme85\\LaravelLogToDB\\LogToDbServiceProvider"
28+
"danielme85\\LaravelLogToDB\\ServiceProvider"
2929
],
3030
"aliases": {
31-
"LogToDb": "danielme85\\LaravelLogToDB\\LogToDb"
31+
"LogToDB": "danielme85\\LaravelLogToDB\\LogToDB"
3232
}
3333
}
3434
}

readme.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Laravel Log-to-DB
2+
Custom Larvel 5.6+ Log channel handler that can store log events to SQL or MongoDB databases.
3+
Uses Laravel native logging functionality.
4+
5+
### Installation
6+
```php
7+
require danielme85/laravel-log-to-db
8+
```
9+
10+
11+
###Configuration
12+
Starting with Laravel 5.6 you will have a new settings file: "config/logging.php".
13+
You will need to add an array under 'channels' for Log-to-DB here like so:
14+
```php
15+
'database' => [
16+
'driver' => 'custom',
17+
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
18+
'level' => env('APP_LOG_LEVEL', 'debug'),
19+
]
20+
```
21+
You can give it whatever name you want as the array index instead of: 'database', as well as the log levels.
22+
This logger works the same as any other across Laravel, for example you can add it to a stack.
23+
You can log multiple levels to multiple DB connections... the possibilities are ENDLESS! 😎
24+
```php
25+
'default' => env('LOG_CHANNEL', 'stack'),
26+
27+
'channels' => [
28+
'stack' => [
29+
'driver' => 'stack',
30+
'channels' => ['database', 'mongodb', 'single'],
31+
],
32+
33+
'database' => [
34+
'driver' => 'custom',
35+
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
36+
'level' => env('APP_LOG_LEVEL', 'debug'),
37+
'connection' => 'default',
38+
'collection' => 'log'
39+
],
40+
41+
'mongodb' => [
42+
'driver' => 'custom',
43+
'via' => danielme85\LaravelLogToDB\LogToDbHandler::class,
44+
'level' => env('APP_LOG_LEVEL', 'debug'),
45+
'connection' => 'mongodb',
46+
'collection' => 'log'
47+
],
48+
49+
'single' => [
50+
'driver' => 'single',
51+
'path' => storage_path('logs/laravel.log'),
52+
'level' => env('APP_LOG_LEVEL', 'debug'),
53+
],
54+
//....
55+
]
56+
```
57+
58+
###Usage
59+
Since this is a custom log channel for Laravel, all "standard" ways of generating log events etc should work with
60+
the Laravel Log Facade. See https://laravel.com/docs/5.6/logging for more information.
61+
62+
####Viewing/Getting The Log
63+
The logging by this channel is done trough the Eloquent Model builder. To get access to the underlying models use:
64+
```php
65+
$log = danielme85\LaravelLogToDB\LogToDB::model();
66+
```
67+
68+
Some examples of getting logs
69+
```php
70+
//LogToDB::model() = The Eloquent model;
71+
72+
$logs = LogToDB::model()->all();
73+
$logs = LogToDB::model()->where('id' = $id)->first();
74+
75+
//or
76+
77+
$log = danielme85\LaravelLogToDB\LogToDB::model();
78+
$log->all();
79+
```
80+
81+
Getting log for specific DB connection when using multiple DB storage methods.
82+
```php
83+
$logsFromDatabase = LogToDB::model('mysql')->all();
84+
$logsFromMongoDB = LogToDB::model('mongodb')->all();
85+
```

scr/DbLog.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

scr/LogToDb.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/DBLog.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace danielme85\LaravelLogToDB;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* Class DbLog
9+
*
10+
* @package danielme85\LaravelLogToDB
11+
*/
12+
class DBLog extends Model
13+
{
14+
use LogToDbCreateObject;
15+
16+
public $timestamps = false;
17+
protected $connection;
18+
protected $table;
19+
20+
function __construct($connection = 'mysql', $table = 'log')
21+
{
22+
$this->connection = $connection;
23+
$this->table = $table;
24+
}
25+
}

src/DBLogMongoDB.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace danielme85\LaravelLogToDB;
4+
5+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
6+
7+
/**
8+
* Class DbLog
9+
*
10+
* @package danielme85\LaravelLogToDB
11+
*/
12+
class DBLogMongoDB extends Eloquent
13+
{
14+
use LogToDbCreateObject;
15+
16+
public $timestamps = false;
17+
protected $connection;
18+
protected $collection;
19+
20+
function __construct($connection = 'mongodb', $collection = 'log')
21+
{
22+
$this->connection = $connection;
23+
$this->collection = $collection;
24+
}
25+
}

src/LogToDB.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace danielme85\LaravelLogToDB;
4+
5+
/**
6+
* Class LogToDb
7+
*
8+
* @package danielme85\LaravelLogToDB
9+
*/
10+
class LogToDB
11+
{
12+
public $channelConnection;
13+
14+
public $detailed = false;
15+
16+
public $maxRows = false;
17+
18+
//DB Connection to use
19+
public $connection = 'default';
20+
21+
public $collection;
22+
23+
private $database = null;
24+
25+
/**
26+
* LogToDB constructor.
27+
*/
28+
function __construct($channelConnection = null, $collection = 'log')
29+
{
30+
$this->channelConnection = $channelConnection;
31+
$this->collection = $collection;
32+
33+
//Set config data
34+
$config = config('logtodb');
35+
if (!empty($config)) {
36+
if (isset($config['connection'])) {
37+
$this->connection = $config['connection'];
38+
}
39+
if (isset($config['detailed'])) {
40+
$this->detailed = $config['detailed'];
41+
}
42+
if (isset($config['max_rows'])) {
43+
$this->maxRows = $config['max_rows'];
44+
}
45+
}
46+
47+
$dbconfig = config('database.connections');
48+
49+
if (!empty($this->channelConnection)) {
50+
if (isset($dbconfig[$this->channelConnection])) {
51+
$this->connection = $this->channelConnection;
52+
}
53+
}
54+
55+
//set the actual connection instead of default
56+
if ($this->connection === 'default') {
57+
$this->connection = config('database.default');
58+
}
59+
60+
if (isset($dbconfig[$this->connection])) {
61+
$this->database = $dbconfig[$this->connection];
62+
}
63+
64+
if (empty($this->database)) {
65+
new \ErrorException("Required configs missing: The LogToDB class needs a database correctly setup in the configs: databases.php and logtodb.php");
66+
}
67+
}
68+
69+
/**
70+
* @return string
71+
*/
72+
public static function model($channelConnection = null) {
73+
$model = new self($channelConnection);
74+
return $model->getModel();
75+
}
76+
77+
/**
78+
* @return DBLogMongoDB | DBLog;
79+
*/
80+
private function getModel() {
81+
if ($this->database['driver'] === 'mongodb') {
82+
//MongoDB has its own Model
83+
return new DBLogMongoDB($this->connection, $this->collection);
84+
}
85+
else {
86+
//Use the default Laravel Eloquent Model
87+
return new DBLog($this->connection, $this->collection);
88+
}
89+
}
90+
91+
/**
92+
* Create a Eloquent Model
93+
*
94+
* @param $record
95+
* @return LogToDb self
96+
*/
97+
public function newFromMonolog(array $record) : self
98+
{
99+
100+
if ($this->database['driver'] === 'mongodb') {
101+
//MongoDB has its own Model
102+
$log = new DBLogMongoDB($this->connection, $this->collection);
103+
}
104+
else {
105+
//Use the default Laravel Eloquent Model
106+
$log = new DBLog($this->connection, $this->collection);
107+
}
108+
109+
if (isset($record['message'])) {
110+
$log->message = $record['message'];
111+
}
112+
/**
113+
* Storing the error log details takes quite a bit of space in sql database compared to a log file,
114+
* so this can be disabled in the config.
115+
*/
116+
if ($this->detailed) {
117+
if (isset($record['context'])) {
118+
if (!empty($record['context'])) {
119+
$log->context = $record['context'];
120+
}
121+
}
122+
}
123+
if (isset($record['level'])) {
124+
$log->level = $record['level'];
125+
}
126+
if (isset($record['level_name'])) {
127+
$log->level_name = $record['level_name'];
128+
}
129+
if (isset($record['channel'])) {
130+
$log->channel = $record['channel'];
131+
}
132+
if (isset($record['datetime'])) {
133+
$log->datetime = $record['datetime'];
134+
}
135+
if (isset($record['extra'])) {
136+
if (!empty($record['extra'])) {
137+
$log->extra = $record['extra'];
138+
}
139+
}
140+
$log->unix_time = time();
141+
142+
d($this);
143+
144+
$log->save();
145+
146+
return $this;
147+
}
148+
149+
}

scr/LogToDbCreateObject.php renamed to src/LogToDbCreateObject.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
*/
1010
trait LogToDbCreateObject
1111
{
12-
public $timestamps = false;
13-
protected $table = 'log';
14-
1512
/**
1613
* Context Accessor
1714
*

0 commit comments

Comments
 (0)