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.
abort(404);
- 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()}}">
<head>
<meta charset="utf-8">
<title>Error 404</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
background:#f7f8fe;
font-family:'Calibri',sans-serif;
margin:1px 1%;
padding:0;
position:relative;
text-align:center;
}
</style>
</head>
<body>
<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>
</body>
</html>
2. Access an address of a page that not exist, for example:
//localhost:8000/present-gift
- 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:
APP_LOG_LEVEL=error
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.
<?php
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:
Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);
- 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

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
HTTP Errors and Logging

Last accessed pages

  1. Properties and Methods of the Form elements (594)
  2. PHP Strings (3263)
  3. Selection Tools (8145)
  4. Get Lower, Higher, and Closest Number (5342)
  5. Simple Admin Login PHP Script (10894)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide