Laravel Course

- Log Storage

When you start a new Laravel project, error and exception handling is already configured. The App\Exceptions\Handler class is where all exceptions triggered by your application are logged and then rendered back to the user.

The debug option in config/app.php file determines how much information about an error is displayed to the user. This option is set to respect the value of the APP_DEBUG variable, which is stored in .env file.
- For local development, you should set the APP_DEBUG environment variable to true. In production environment, this value should be false.

For logging, Laravel utilizes the Monolog library. The log files are created in the storage/logs directory.

HTTP Exceptions - Custom HTTP Error Pages

To generate HTTP error codes like 404, 500, etc. anywhere in Laravel application, you can use the abort() method.
- Optionally, you may provide a custom response text:
abort(403, 'Unauthorized action.');

Custom HTTP Error Pages

Laravel makes it very easy to use custom error pages for each separate error codes. Just create an errors folder in resources/views/ directory, then crete a "Code_Error.blade.php" file for each HTTP Error you want to customize. Laravel framework will automatically detect the file in "views/errors/" folder and use it when that error occurs.
- Here is an example for http error code 404.

1. Create a view at resources/views/errors/404.blade.php, and copy the following code in that file:
<!doctype html>
<html lang="{{app()->getLocale()}}">
<meta charset="utf-8">
<title>Error 404</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
body {
margin:1px 1%;
<h1>Error 404 - Page not exists</h1>
<h2>Which does not exist cannot affect you.</h2>
<h3>If you want something that exists, your-self is the Truth in this present.</h3>
2. Access an address of a page that not exist, for example:
- It will display the content from the resources/views/errors/404.blade.php file.

Same way you can design error pages for http error codes 401, 403, 500.

Log Storage

Laravel supports writing log information to single files, daily files, the syslog, and the errorlog.
To configure which storage mechanism Laravel uses, modify the log option in config/app.php file.
- For example, to use daily log files instead of a single file, set the log value in app.php file to 'daily':
'log'=> 'daily'

Maximum Daily Log Files

When using the daily log mode, Laravel will retain five days of log files. To adjust the number of retained files, you may add a log_max_files value to your config/app.php file:
'log_max_files'=> 7

Log Levels

In Laravel you can use the following severity levels - from least severe to most severe: debug, info, notice, warning, error, critical, alert, emergency.
Laravel writes all log levels to storage. If you wish to configure a minimum severity that should be logged, change the value of the APP_LOG_LEVEL in ".env" file:
Or the log_level option in "config/app.php" file.
'log_level'=> 'error'
Laravel will log all levels greater than or equal to the specified severity. For example, if you set the value error, it will log: error, critical, alert, emergency messages.

Writing information to the logs

You may write information to the logs using the Log facade.
namespace App\Http\Controllers;

use App\User;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;

class UserController extends Controller{
  //Show the profile for the given user
  //@param  int  $id
  //@return view
  public function showProfile($id){
    Log::info('Showing user profile for user: '.$id);

    return view('user.profile', ['user'=> User::findOrFail($id)]);
- The log class provides the eight logging levels:
- An array of data may also be passed to the Log methods. This data will be formatted and displayed with the log message:
Log::notice('User failed to login.', ['id'=> $user->id]);

- Documentation: Laravel - Errors and Logging

Daily Test with Code Example

What attribute makes the input text box to not be modified by the user?
checked="checked" readonly="readonly" disabled="disabled"
<input type="text" value="fixed-value" readonly="readonly" name="a_name" />
What CSS property allows you to create rounded corners in your webpage design?
background-size border-size border-radius
.class {
  border:2px solid blue;
What instruction displays a confirmation dialog box to the viewer, who must then click OK or Cancel to proceed?
indexOf() confirm() prompt()
var ques = window.confirm("The result of 0+0 is 0?");
if (ques) alert("Corect");
else alert("Incorrect");
Indicate the PHP function that returns the lowest number of the parameter values.
floor() ceil() min()
$min_nr = min(12, 8, 25, 13);
echo $min_nr;        // 8
HTTP Errors and Logging

Last accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (66107)
  2. AJAX Course, free Lessons (18707)
  3. The Power of Now (1297)
  4. The Fifth Agreement (17547)
  5. PHP getElementById and getElementsByTagName (45393)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (96)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (58)
  3. JavaScript Course - Free lessons (45)
  4. SHA1 Encrypt data in JavaScript (44)
  5. Read Excel file data in PHP - PhpExcelReader (40)