Skip to content

Commit

Permalink
Translate errors.md (v5.5) (#371)
Browse files Browse the repository at this point in the history
* Translate errors.md (v5.5)

* 11/25 修正翻譯問題
  • Loading branch information
Nationalcat authored and neighborhood999 committed Nov 25, 2017
1 parent a0bbd26 commit 6db540a
Showing 1 changed file with 65 additions and 65 deletions.
130 changes: 65 additions & 65 deletions errors.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
# Errors & Logging

- [Introduction](#introduction)
- [Configuration](#configuration)
- [Error Detail](#error-detail)
- [Log Storage](#log-storage)
- [Log Severity Levels](#log-severity-levels)
- [Custom Monolog Configuration](#custom-monolog-configuration)
- [The Exception Handler](#the-exception-handler)
- [Report Method](#report-method)
- [Render Method](#render-method)
- [Reportable & Renderable Exceptions](#renderable-exceptions)
- [HTTP Exceptions](#http-exceptions)
- [Custom HTTP Error Pages](#custom-http-error-pages)
- [Logging](#logging)
# 錯誤與日誌

- [介紹](#introduction)
- [設定檔](#configuration)
- [錯誤細節](#error-detail)
- [日誌儲存](#log-storage)
- [日誌的嚴重性級別](#log-severity-levels)
- [自訂 Monolog 設定](#custom-monolog-configuration)
- [異常處理](#the-exception-handler)
- [Report 方法](#report-method)
- [Render 方法](#render-method)
- [可報告與可見的例外](#renderable-exceptions)
- [HTTP 例外](#http-exceptions)
- [自訂 HTTP 錯誤頁面](#custom-http-error-pages)
- [日誌](#logging)

<a name="introduction"></a>
## Introduction
## 介紹

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.
當你開始一個新的 Laravel 專案時,都已經為你設定好錯誤與異常處理。`App\Exceptions\Handler` 類別是記錄應用程式所觸發的所有異常並將其呈現給使用者的地方。在這個文件中,我們將會深入這個類別。

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.
關於日誌,Laravel 使用 [Monolog](https://github.com/Seldaek/monolog) 函式庫,它提供了支援各種強大日誌的處理程序。Laravel 為你設定了其中幾個處理程序,可以讓你在單一或多個日誌檔案,或將錯誤訊息寫入系統日誌之間做選擇。

<a name="configuration"></a>
## Configuration
## 設定

<a name="error-detail"></a>
### Error Detail
### 錯誤細節

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.
`config/app.php` 設定檔中的 `debug` 選項會確認實際有多少錯誤資訊顯示給使用者。預設這個選項會遵守存放在 `.env` 檔案的 `APP_DEBUG` 環境變數的值。

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.
在本機開發的時候,你應該將 `APP_DEBUG` 環境變數設定為 `true`。在你的上線環境中,這個值應該永遠為 `false`。如果在正式上線主機上設定為 `true`,你可能會將設定檔的敏感資訊暴露給應用程式末端的使用者。

<a name="log-storage"></a>
### Log Storage
### 日誌儲存

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`:
Laravel 提供立即可用的日誌模式,支援寫入日誌資訊到 `single` 檔案、`daily` 檔案、`syslog``errorlog`。要設定 Laravel 使用的儲存系統,你應該在 `config/app.php` 設定檔修改 `log` 選項。例如,如果你希望使用 `daily` 記錄檔而不是單個檔案,你應該將 `app` 設定檔的 `log` 值設定為 `daily`

'log' => 'daily'

#### Maximum Daily Log Files
#### 最大天數日誌檔案

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:
使用 `daily` 日誌模式,Laravel 預設只會保留五天的日誌檔案。如果你想要調整保留檔案的天數,你可以新增 `log_max_files` 設定值到 `app` 設定檔:

'log_max_files' => 30

<a name="log-severity-levels"></a>
### Log Severity Levels
### 日誌的嚴重性級別

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.
使用 Monolog 時,日誌訊息可以有不同級別的嚴重性。預設的 Laravel 會儲存所有寫入的日誌級別。然而,在你正式環境中,你可能希望通過增加 `log_level` 選項到你的 `app.php` 設定檔來將設定應該記錄的最低嚴重性。

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:
這個選項一旦被設定,Laravel 會記錄所有級別大於或等於指定的嚴重性。例如,預設的 `error` `日誌級別` 會記錄 **錯誤****嚴重****警告** **緊急** 訊息:

'log_level' => env('APP_LOG_LEVEL', 'error'),

> {tip} Monolog recognizes the following severity levels - from least severe to most severe: `debug`, `info`, `notice`, `warning`, `error`, `critical`, `alert`, `emergency`.
> {tip} Monolog 可以識別以下嚴重程度,從最低到最嚴重: `debug`, `info`, `notice`, `warning`, `error`, `critical`, `alert`, `emergency`.
<a name="custom-monolog-configuration"></a>
### Custom Monolog Configuration
### 自訂 Monolog 設定

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:
如果你想要完全掌控 Monolog 為應用程式的設定,可以使用 `configureMonologUsing` 方法。在檔案回傳 `$app` 變數之前,你應該在你的 `bootstrap/app.php` 檔案終回呼這個方法:

$app->configureMonologUsing(function ($monolog) {
$monolog->pushHandler(...);
});

return $app;

#### Customizing The Channel Name
#### 自訂頻道名稱

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:
預設的 Monolog 實例化名稱會與當前環境匹配,像是 `production` `local`。要變更這個值,請新增 `log_channel` 選項到 `app.php` 設定檔中:

'log_channel' => env('APP_LOG_CHANNEL', 'my-app-name'),

<a name="the-exception-handler"></a>
## The Exception Handler
## 異常處理

<a name="report-method"></a>
### The Report Method
### Report 方法

All exceptions are handled by the `App\Exceptions\Handler` class. This class contains two methods: `report` and `render`. We'll examine each of these methods in detail. The `report` method is used to log exceptions or send them to an external service like [Bugsnag](https://bugsnag.com) or [Sentry](https://github.com/getsentry/sentry-laravel). By default, the `report` method simply passes the exception to the base class where the exception is logged. However, you are free to log exceptions however you wish.
`App\Exceptions\Handler` 類別會幫你處理所有例外。這個類別會有兩個方法:`report` `render`。我們將詳細的討論這些方法。`report` 方法用於記錄例外或發送它們到外部服務,像是 [Bugsnag](https://bugsnag.com) [Sentry](https://github.com/getsentry/sentry-laravel)。預設的 `report` 方法只是將例外傳給記錄例外的基礎類別。不過,你可以自由的記錄例外。

For example, if you need to report different types of exceptions in different ways, you may use the PHP `instanceof` comparison operator:
例如,如果你需要以不同的方式回報不同類型的例外,你可以使用 `instanceof` 類型運算子:

/**
* Report or log an exception.
* 回報或記錄一個例外。
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
* 這是向 SentryBugsnag 等發送例外的好地方。
*
* @param \Exception $exception
* @return void
Expand All @@ -99,9 +99,9 @@ For example, if you need to report different types of exceptions in different wa
return parent::report($exception);
}

#### The `report` Helper
#### `report` 輔助函式

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:
有時你一方面要回報例外,另一方面要處理當前的請求。`report` 輔助函式可以讓你使用異常處理器的 `report` 方法快速的回報例外,而不會顯示錯誤頁面:

public function isValid($value)
{
Expand All @@ -114,12 +114,12 @@ Sometimes you may need to report an exception but continue handling the current
}
}

#### Ignoring Exceptions By Type
#### 依類型忽略例外

The `$dontReport` property of the exception handler contains an array of exception types that will not be logged. For example, exceptions resulting from 404 errors, as well as several other types of errors, are not written to your log files. You may add other exception types to this array as needed:
異常處理器的 `$dontReport` 屬性會包含一組例外類型的陣列不會被記錄。例如,404 錯誤導致的例外以及其它類型的錯誤將不會寫入你的日誌中。可以根據實際的需要來新增其他例外類型到這組陣列:

/**
* A list of the exception types that should not be reported.
* 不該被回報的例外類型清單。
*
* @var array
*/
Expand All @@ -132,12 +132,12 @@ The `$dontReport` property of the exception handler contains an array of excepti
];

<a name="render-method"></a>
### The Render Method
### Render 方法

The `render` method is responsible for converting a given exception into an HTTP response that should be sent back to the browser. By default, the exception is passed to the base class which generates a response for you. However, you are free to check the exception type or return your own custom response:
`render` 方法負責將給定的例外轉換成應該回傳給瀏覽器的 HTTP 回應。預設的例外會被傳入產生回應的基礎類別。然而,你可以自由的檢查例外類型或回傳你自訂的回應:

/**
* Render an exception into an HTTP response.
* 渲染例外到 HTTP 回應。
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
Expand All @@ -153,9 +153,9 @@ The `render` method is responsible for converting a given exception into an HTTP
}

<a name="renderable-exceptions"></a>
### Reportable & Renderable Exceptions
### 可報告與可見的例外

Instead of type-checking exceptions in the exception handler's `report` and `render` methods, you may define `report` and `render` methods directly on your custom exception. When these methods exist, they will be called automatically by the framework:
想要取代異常處理器用來檢查類型例外的 `report` `render` 方法,你可以直接在你自訂的例外中定義 `report` `render` 方法。當這些方法存在時,它們會被框架自動呼叫:

<?php

Expand All @@ -166,7 +166,7 @@ Instead of type-checking exceptions in the exception handler's `report` and `ren
class RenderException extends Exception
{
/**
* Report the exception.
* 回報例外。
*
* @return void
*/
Expand All @@ -176,7 +176,7 @@ Instead of type-checking exceptions in the exception handler's `report` and `ren
}

/**
* Render the exception into an HTTP response.
* 渲染例外到 HTTP 回應。
*
* @param \Illuminate\Http\Request
* @return \Illuminate\Http\Response
Expand All @@ -188,27 +188,27 @@ Instead of type-checking exceptions in the exception handler's `report` and `ren
}

<a name="http-exceptions"></a>
## HTTP Exceptions
## HTTP 例外

Some exceptions describe HTTP error codes from the server. For example, this may be a "page not found" error (404), an "unauthorized error" (401) or even a developer generated 500 error. In order to generate such a response from anywhere in your application, you may use the `abort` helper:
有些例外會描述 HTTP 錯誤代碼來自伺服器。例如,這可能是「找不到頁面」的 404 錯誤代碼,「未授權的錯誤」的 401 錯誤代碼或者是開發者產生的 500 錯誤代碼。為了在你應用程式的任何地方產生這樣的回應,你可以使用 `abort` 輔助函式:

abort(404);

The `abort` helper will immediately raise an exception which will be rendered by the exception handler. Optionally, you may provide the response text:
`abort` 輔助函式會馬上發出異常處理器即將渲染的例外。或者,你可以提供回應的文字內容:

abort(403, 'Unauthorized action.');

<a name="custom-http-error-pages"></a>
### Custom HTTP Error Pages
### 自訂 HTTP 錯誤頁面

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:
Laravel 很容易為各種的 HTTP 狀態碼設計所要顯示的自訂錯誤頁面。例如,如果你希望自訂 HTTP 404 錯誤代碼頁面,而去建立 `resources/views/errors/404.blade.php`。這個檔案會為應用程式產生所有的 404 錯誤代碼而服務。在這個目錄中的視圖應該命名與 HTTP 狀態碼一致。透過 `abort` 函式發出的 `HttpException` 實例會作為 `$exception` 變數傳入視圖:

<h2>{{ $exception->getMessage() }}</h2>

<a name="logging"></a>
## Logging
## 日誌

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):
Laravel 在強大的 [Monolog](https://github.com/seldaek/monolog) 函式庫上提供了一個簡單的抽象層。預設的 Laravel 已設定好在 `storage/logs` 目錄中建立日誌檔案。你可以使用 `Log` [facade](/docs/{{version}}/facades) 撰寫資訊到日誌中:

<?php

Expand All @@ -221,7 +221,7 @@ Laravel provides a simple abstraction layer on top of the powerful [Monolog](htt
class UserController extends Controller
{
/**
* Show the profile for the given user.
* 顯示給定使用者的個人資料。
*
* @param int $id
* @return Response
Expand All @@ -234,7 +234,7 @@ Laravel provides a simple abstraction layer on top of the powerful [Monolog](htt
}
}

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**.
日誌提供了符合 [RFC 5424](https://tools.ietf.org/html/rfc5424) 定義的八種日誌級別:**emergency****alert****critical****error****warning****notice****info** **debug**

Log::emergency($message);
Log::alert($message);
Expand All @@ -245,14 +245,14 @@ The logger provides the eight logging levels defined in [RFC 5424](https://tools
Log::info($message);
Log::debug($message);

#### Contextual Information
#### Context 資訊

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:
Context 資料的陣列也可以被傳入 `Log` 方法。這個 Context 資料會被格式化並顯示日誌訊息:

Log::info('User failed to login.', ['id' => $user->id]);

#### Accessing The Underlying Monolog Instance
#### 存取底層的 Monolog 實例

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:
Monolog 有多種額外的處理器可被用於記錄上。如果有需要,你可以存取被 Laravel 使用的底層 Monolog 實例:

$monolog = Log::getMonolog();

0 comments on commit 6db540a

Please sign in to comment.