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 defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
HTTP Errors and Logging

Last accessed pages

  1. Splat Operator in PHP (4466)
  2. MouseEvent - Events for Mouse (2897)
  3. GraidleChart Create Graphic Charts (1993)
  4. Set custom message for required and field validation (1380)
  5. Area and Perimeter Calculator for 2D shapes (10136)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (497)
  2. PHP-MySQL free course, online tutorials PHP MySQL code (91)
  3. Read Excel file data in PHP - PhpExcelReader (55)
  4. The Mastery of Love (43)
  5. The Fifth Agreement (42)