99Custom Larvel 5.6+ Log channel handler that can store log events to SQL or MongoDB databases.
1010Uses Laravel native logging functionality.
1111
12- ### Installation
12+ ## Installation
1313```
1414require danielme85/laravel-log-to-db
1515```
@@ -19,7 +19,7 @@ If you are going to be using SQL database server to store log events you would n
1919php artisan migrate
2020```
2121
22- ### Configuration
22+ ## Configuration
2323Starting with Laravel 5.6 you will have a new settings file: "config/logging.php".
2424You will need to add an array under 'channels' for Log-to-DB here like so:
2525``` php
@@ -46,7 +46,7 @@ You will need to add an array under 'channels' for Log-to-DB here like so:
4646More info about some of these options: https://laravel.com/docs/5.6/logging#customizing-monolog-for-channels
4747
4848There are some default settings and more information about configuring the logger in the 'logtodb.php' config file.
49- This can be copied to your project so you can edit it with the vendor publish command.
49+ This could be copied to your project if you would like edit it with the vendor publish command.
5050```
5151php artisan vendor:publish
5252```
@@ -58,6 +58,42 @@ Log::channel('mongodb')->info("This thing just happened");
5858```
5959This logger works the same as any other across Laravel, for example you can add it to a stack.
6060You can log multiple levels to multiple DB connections... the possibilities are ENDLESS! 😎
61+
62+ #### Log Worker Queue
63+ It might be a good idea to save the log events with a Queue Worker. This way your server does not have to wait for
64+ the save process to finish. You would have to configure the Laravel Queue settings and run the Queue listener.
65+ https://laravel.com/docs/5.6/queues#running-the-queue-worker
66+
67+ ## Usage
68+ Since this is a custom log channel for Laravel, all "standard" ways of generating log events etc should work with
69+ the Laravel Log Facade. See https://laravel.com/docs/5.6/logging for more information.
70+
71+ #### Fetching Logs
72+ The logging by this channel is done trough the Eloquent Model builder.
73+ LogToDB::model($channel, $connection, $collection);
74+ You can skip all function variables and the default settings from the config/logtodb.php will be used.
75+ ``` php
76+ $model = LogToDB::model();
77+ $model->all(); //All logs for defualt channel/connection
78+ ```
79+
80+ Some more examples of getting logs
81+ ``` php
82+ $logs = LogToDB::model()->all();
83+ $logs = LogToDB::model()->where('id' = $id)->first();
84+ ```
85+
86+ When getting logs for specific channel or DB connection and collection you can either use the channel name matching
87+ config/logging.php or connection name from config/databases.php. You can also specify collection/table name if needed as
88+ the third function variable when fetching the model.
89+ ``` php
90+ $logsFromDefault = LogDB::model()->all();
91+ $logsFromChannel = LogDB::model('database')->all();
92+ $logsFromMysql = LogToDB::model(null, 'mysql')->all();
93+ $logsFromMongoDB = LogToDB::model(null, 'mongodb', 'log')->all();
94+ ```
95+
96+ ##### Advanced /config/logging.php example
6197``` php
6298'default' => env('LOG_CHANNEL', 'stack'),
6399
@@ -90,33 +126,4 @@ You can log multiple levels to multiple DB connections... the possibilities are
90126 ],
91127 //....
92128]
93- ```
94-
95- ### Usage
96- Since this is a custom log channel for Laravel, all "standard" ways of generating log events etc should work with
97- the Laravel Log Facade. See https://laravel.com/docs/5.6/logging for more information.
98-
99- #### Fetching Logs
100- The logging by this channel is done trough the Eloquent Model builder.
101- LogToDB::model($channel, $connection, $collection);
102- You can skip all function variables and the default settings from the config/logtodb.php will be used.
103- ``` php
104- $model = LogToDB::model();
105- $model->all(); //All logs for defualt channel/connection
106- ```
107-
108- Some more examples of getting logs
109- ``` php
110- $logs = LogToDB::model()->all();
111- $logs = LogToDB::model()->where('id' = $id)->first();
112- ```
113-
114- When getting logs for specific channel or DB connection and collection you can either use the channel name matching
115- config/logging.php or connection name from config/databases.php. You can also specify collection/table name if needed as
116- the third function variable when fetching the model.
117- ``` php
118- $logsFromDefault = LogDB::model()->all();
119- $logsFromChannel = LogDB::model('database')->all();
120- $logsFromMysql = LogToDB::model(null, 'mysql')->all();
121- $logsFromMongoDB = LogToDB::model(null, 'mongodb', 'log')->all();
122129```
0 commit comments