en en en en en en en en en en en nl en en en en en en en en en en en en en en en en en en en pt en en de it en en sv en de en de en de en de de nl en en en fr sv ca en en en de fr es fr ja en en fr en it en fr en fr en fr en en en fr en fr en en en en en en en en en en da en en en en en zh kn en en en en en en en en en en en en en en en en en en en en en en en id en en en
For setting up the path globally for all the log files you can call setPath method in your bootstrap file.
NOTE: If you want to use logger in a cron scripts or something like WordPress hook, you need to call setPath() at the very first step of the script execution, it means that your project might have multiple places where you need to set path for your logs. If you don't want to call setPath() you can just pass the path to a tiny_log() function as a third argument. See an example in the Usage section.
This package comes with a function tiny_log() where second and third arguments are not required.
You can also use Logger class if you want. It will do the same as using function.
use \Serhii\TinyLogger\Logger; Logger::new()->error('Some error message'); Logger::new()->info('Some info message'); Logger::new()->debug('Some error message');
For using one of the available options you can optionally pass certain flag to tiny_log() function as the second argument. If you also need to pass error type just separate them with the pipe | character. See the example with option pos:
tiny_log('Some error message', 'pos'); // just passing option tiny_log('Some error message', 'pos|error'); // 'pos' option with error type 'error' tiny_log('Some error message', 'pos|info'); // 'pos' option with error type 'info'
pos - Show position of the logger. In which file and on what line number it is. It is useful when you're debugging, to not forget where you put your logger. See the example of output:
[2020-01-12 04:09:16] info: Some log message goes here >>> /var/www/html/app/Services/App.php on line: 77.
Send logs with POST request
Tiny logger allows you to send logs as a json object on a specific endpoint. To enable this option you need to call enablePostRequest method on Logger class. To disable POST request use disablePostRequest method.
Now if error occurs, json will be sent to http://my-site.com/webhook endpoint with POST request.
{ "timestamp": "1611675632", "message": "Undefined variable at line 24 in \\App\\Models\\User class.", "type": "error" }
If you need to customize the json object structure, you can pass array as the second argument on enablePostRequest method.
use Serhii\TinyLogger\JsonFieldValue; use Serhii\TinyLogger\Logger; Logger::enablePostRequest('http://my-site.com/webhook', [ 'time' => JsonFieldValue::TIMESTAMP, 'errorMessage' => 'Error message: ' . JsonFieldValue::MESSAGE, 'errorType' => JsonFieldValue::ERROR_TYPE, 'token' => getenv('MY_AUTH_TOKEN') ]);
Now you'll get json like this:
{ "time": "1611675632", "errorMessage": "Error message: Undefined variable at line 24 in \\App\\Models\\User class.", "errorType": "error", "token": "29d62x7g656e6f9" }
Each JsonFieldValue constant will be replaced with its value. For example JsonFieldValue::MESSAGE will be replaced with the error message. JsonFieldValue::TIMESTAMP will be replaced with error timestamp.
NOTE: If you want to use logger in a cron scripts or something like WordPress hook, you need to call enablePostRequest at the very first step of the script execution.
To install all php dependencies you need to have Composer PHP package manager installed on your machine. Then you need to run the command below in your root directory of the project.