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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
HTTP Errors and Logging

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141747)
  2. Rectangle, Oval, Polygon - Star (3322)
  3. PHP PDO - prepare and execute (9187)
  4. jQuery Ajax - load() method (10835)
  5. Creating XML data - E4X in ActionScript 3 (3088)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (471)
  2. CSS cursor property - Custom Cursors (78)
  3. The Mastery of Love (69)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)