Skip to content

Commit 1efdd3a

Browse files
committed
starting logging docs
1 parent cb7b1ba commit 1efdd3a

File tree

3 files changed

+115
-105
lines changed

3 files changed

+115
-105
lines changed

documentation.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
- [URL Generation](/docs/{{version}}/urls)
2828
- [Session](/docs/{{version}}/session)
2929
- [Validation](/docs/{{version}}/validation)
30-
- [Errors & Logging](/docs/{{version}}/errors)
30+
- [Error Handling](/docs/{{version}}/errors)
31+
- [Logging](/docs/{{version}}/logging)
3132
- ## Frontend
3233
- [Blade Templates](/docs/{{version}}/blade)
3334
- [Localization](/docs/{{version}}/localization)

errors.md

+3-104
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,26 @@
1-
# Errors & Logging
1+
# Error Handling
22

33
- [Introduction](#introduction)
44
- [Configuration](#configuration)
5-
- [Error Detail](#error-detail)
6-
- [Log Storage](#log-storage)
7-
- [Log Severity Levels](#log-severity-levels)
8-
- [Custom Monolog Configuration](#custom-monolog-configuration)
95
- [The Exception Handler](#the-exception-handler)
106
- [Report Method](#report-method)
117
- [Render Method](#render-method)
128
- [Reportable & Renderable Exceptions](#renderable-exceptions)
139
- [HTTP Exceptions](#http-exceptions)
1410
- [Custom HTTP Error Pages](#custom-http-error-pages)
15-
- [Logging](#logging)
1611

1712
<a name="introduction"></a>
1813
## Introduction
1914

2015
When you start a new Laravel project, error and exception handling is already configured for you. The `App\Exceptions\Handler` class is where all exceptions triggered by your application are logged and then rendered back to the user. We'll dive deeper into this class throughout this documentation.
2116

22-
For logging, Laravel utilizes the [Monolog](https://github.com/Seldaek/monolog) library, which provides support for a variety of powerful log handlers. Laravel configures several of these handlers for you, allowing you to choose between a single log file, rotating log files, or writing error information to the system log.
23-
2417
<a name="configuration"></a>
2518
## Configuration
2619

27-
<a name="error-detail"></a>
28-
### Error Detail
29-
3020
The `debug` option in your `config/app.php` configuration file determines how much information about an error is actually displayed to the user. By default, this option is set to respect the value of the `APP_DEBUG` environment variable, which is stored in your `.env` file.
3121

3222
For local development, you should set the `APP_DEBUG` environment variable to `true`. In your production environment, this value should always be `false`. If the value is set to `true` in production, you risk exposing sensitive configuration values to your application's end users.
3323

34-
<a name="log-storage"></a>
35-
### Log Storage
36-
37-
Out of the box, Laravel supports writing log information to `single` files, `daily` files, the `syslog`, and the `errorlog`. To configure which storage mechanism Laravel uses, you should modify the `log` option in your `config/app.php` configuration file. For example, if you wish to use daily log files instead of a single file, you should set the `log` value in your `app` configuration file to `daily`:
38-
39-
'log' => 'daily'
40-
41-
#### Maximum Daily Log Files
42-
43-
When using the `daily` log mode, Laravel will only retain five days of log files by default. If you want to adjust the number of retained files, you may add a `log_max_files` configuration value to your `app` configuration file:
44-
45-
'log_max_files' => 30
46-
47-
<a name="log-severity-levels"></a>
48-
### Log Severity Levels
49-
50-
When using Monolog, log messages may have different levels of severity. By default, Laravel writes all log levels to storage. However, in your production environment, you may wish to configure the minimum severity that should be logged by adding the `log_level` option to your `app.php` configuration file.
51-
52-
Once this option has been configured, Laravel will log all levels greater than or equal to the specified severity. For example, a default `log_level` of `error` will log **error**, **critical**, **alert**, and **emergency** messages:
53-
54-
'log_level' => env('APP_LOG_LEVEL', 'error'),
55-
56-
> {tip} Monolog recognizes the following severity levels - from least severe to most severe: `debug`, `info`, `notice`, `warning`, `error`, `critical`, `alert`, `emergency`.
57-
58-
<a name="custom-monolog-configuration"></a>
59-
### Custom Monolog Configuration
60-
61-
If you would like to have complete control over how Monolog is configured for your application, you may use the application's `configureMonologUsing` method. You should place a call to this method in your `bootstrap/app.php` file right before the `$app` variable is returned by the file:
62-
63-
$app->configureMonologUsing(function ($monolog) {
64-
$monolog->pushHandler(...);
65-
});
66-
67-
return $app;
68-
69-
#### Customizing The Channel Name
70-
71-
By default, Monolog is instantiated with name that matches the current environment, such as `production` or `local`. To change this value, add the `log_channel` option to your `app.php` configuration file:
72-
73-
'log_channel' => env('APP_LOG_CHANNEL', 'my-app-name'),
74-
7524
<a name="the-exception-handler"></a>
7625
## The Exception Handler
7726

@@ -99,6 +48,8 @@ For example, if you need to report different types of exceptions in different wa
9948
return parent::report($exception);
10049
}
10150

51+
> {tip} Instead of making a lot of `instanceof` checks in your `report` method, consider using [reportable exceptions](/docs/{{version}}/errors#renderable-exceptions)
52+
10253
#### The `report` Helper
10354

10455
Sometimes you may need to report an exception but continue handling the current request. The `report` helper function allows you to quickly report an exception using your exception handler's `report` method without rendering an error page:
@@ -204,55 +155,3 @@ The `abort` helper will immediately raise an exception which will be rendered by
204155
Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a `resources/views/errors/404.blade.php`. This file will be served on all 404 errors generated by your application. The views within this directory should be named to match the HTTP status code they correspond to. The `HttpException` instance raised by the `abort` function will be passed to the view as an `$exception` variable:
205156

206157
<h2>{{ $exception->getMessage() }}</h2>
207-
208-
<a name="logging"></a>
209-
## Logging
210-
211-
Laravel provides a simple abstraction layer on top of the powerful [Monolog](https://github.com/seldaek/monolog) library. By default, Laravel is configured to create a log file for your application in the `storage/logs` directory. You may write information to the logs using the `Log` [facade](/docs/{{version}}/facades):
212-
213-
<?php
214-
215-
namespace App\Http\Controllers;
216-
217-
use App\User;
218-
use Illuminate\Support\Facades\Log;
219-
use App\Http\Controllers\Controller;
220-
221-
class UserController extends Controller
222-
{
223-
/**
224-
* Show the profile for the given user.
225-
*
226-
* @param int $id
227-
* @return Response
228-
*/
229-
public function showProfile($id)
230-
{
231-
Log::info('Showing user profile for user: '.$id);
232-
233-
return view('user.profile', ['user' => User::findOrFail($id)]);
234-
}
235-
}
236-
237-
The logger provides the eight logging levels defined in [RFC 5424](https://tools.ietf.org/html/rfc5424): **emergency**, **alert**, **critical**, **error**, **warning**, **notice**, **info** and **debug**.
238-
239-
Log::emergency($message);
240-
Log::alert($message);
241-
Log::critical($message);
242-
Log::error($message);
243-
Log::warning($message);
244-
Log::notice($message);
245-
Log::info($message);
246-
Log::debug($message);
247-
248-
#### Contextual Information
249-
250-
An array of contextual data may also be passed to the log methods. This contextual data will be formatted and displayed with the log message:
251-
252-
Log::info('User failed to login.', ['id' => $user->id]);
253-
254-
#### Accessing The Underlying Monolog Instance
255-
256-
Monolog has a variety of additional handlers you may use for logging. If needed, you may access the underlying Monolog instance being used by Laravel:
257-
258-
$monolog = Log::getMonolog();

logging.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Logging
2+
3+
- [Introduction](#introduction)
4+
- [Configuration](#configuration)
5+
- [Building Log Stacks](#building-log-stacks)
6+
- [Customizing Monolog](#customizing-monolog)
7+
- [Writing Log Messages](#writing-log-messages)
8+
9+
<a name="introduction"></a>
10+
## Introduction
11+
12+
To help you learn more of what's happening within your application, Laravel provides robust logging services that allow you to log messages and errors to files, the system error log, or even to Slack to notify your entire team.
13+
14+
Under the hood, Laravel utilizes the [Monolog](https://github.com/Seldaek/monolog) library, which provides support for a variety of powerful log handlers. Laravel makes it a cinch to configure these handlers, allowing you to mix and match them to customize your application's log handling.
15+
16+
<a name="configuration"></a>
17+
## Configuration
18+
19+
All of the configuration for your application's logging system is housed in the `config/logging.php` configuration file. This file allows you to configure your application's log channels, so be sure to review each of the available channels and their options. Of course, we'll review a few common options below.
20+
21+
By default, Laravel will use the `stack` channel when logging messages. The `stack` channel type is used to aggregate multiple log handlers into a single channel. For more information on building stacks, check out the [documentation below](#building-log-stacks).
22+
23+
#### Customizing The Channel Name
24+
25+
By default, Monolog is instantiated with a "channel name" that matches the current environment, such as `production` or `local`. To change this value, add a `name` option to your channel's configuration:
26+
27+
'stack' => [
28+
'driver' => 'stack',
29+
'name' => 'channel-name',
30+
'channels' => ['single', 'slack'],
31+
],
32+
33+
<a name="building-log-stacks"></a>
34+
### Building Log Stacks
35+
36+
Here Test
37+
38+
<a name="log-severity-levels"></a>
39+
#### Log Severity Levels
40+
41+
Log messages may have different levels of severity. By default, Laravel typically writes all log levels to storage. However, in your production environment, you may wish to configure the minimum severity that should be logged.
42+
43+
Once this option has been configured, Laravel will log all levels greater than or equal to the specified severity. For example, a default `log_level` of `error` will log **error**, **critical**, **alert**, and **emergency** messages:
44+
45+
'log_level' => env('APP_LOG_LEVEL', 'error'),
46+
47+
> {tip} Monolog recognizes the following severity levels - from least severe to most severe: `debug`, `info`, `notice`, `warning`, `error`, `critical`, `alert`, `emergency`.
48+
49+
<a name="customizing-monolog"></a>
50+
### Customizing Monolog
51+
52+
If you would like to have complete control over how Monolog is configured for your application, you may use the application's `configureMonologUsing` method. You should place a call to this method in your `bootstrap/app.php` file right before the `$app` variable is returned by the file:
53+
54+
$app->configureMonologUsing(function ($monolog) {
55+
$monolog->pushHandler(...);
56+
});
57+
58+
return $app;
59+
60+
<a name="writing-log-messages"></a>
61+
## Writing Log Messages
62+
63+
Laravel provides a simple abstraction layer on top of the powerful [Monolog](https://github.com/seldaek/monolog) library. By default, Laravel is configured to create a log file for your application in the `storage/logs` directory. You may write information to the logs using the `Log` [facade](/docs/{{version}}/facades):
64+
65+
<?php
66+
67+
namespace App\Http\Controllers;
68+
69+
use App\User;
70+
use Illuminate\Support\Facades\Log;
71+
use App\Http\Controllers\Controller;
72+
73+
class UserController extends Controller
74+
{
75+
/**
76+
* Show the profile for the given user.
77+
*
78+
* @param int $id
79+
* @return Response
80+
*/
81+
public function showProfile($id)
82+
{
83+
Log::info('Showing user profile for user: '.$id);
84+
85+
return view('user.profile', ['user' => User::findOrFail($id)]);
86+
}
87+
}
88+
89+
The logger provides the eight logging levels defined in [RFC 5424](https://tools.ietf.org/html/rfc5424): **emergency**, **alert**, **critical**, **error**, **warning**, **notice**, **info** and **debug**.
90+
91+
Log::emergency($message);
92+
Log::alert($message);
93+
Log::critical($message);
94+
Log::error($message);
95+
Log::warning($message);
96+
Log::notice($message);
97+
Log::info($message);
98+
Log::debug($message);
99+
100+
#### Contextual Information
101+
102+
An array of contextual data may also be passed to the log methods. This contextual data will be formatted and displayed with the log message:
103+
104+
Log::info('User failed to login.', ['id' => $user->id]);
105+
106+
#### Accessing The Underlying Monolog Instance
107+
108+
Monolog has a variety of additional handlers you may use for logging. If needed, you may access the underlying Monolog instance being used by Laravel:
109+
110+
$monolog = Log::getMonolog();

0 commit comments

Comments
 (0)