設定Configuration
アプリケーションのログハンドラーは、app/start/global.php
スタートファイルで登録されています。デフォルトでは、このロガーはログファイルを一つだけ使用するように設定されています。しかしながら、必要であれば、この動作を変更できます。Laravelは人気のあるMonologログライブラリーを使用していますので、このライブラリーが提供する様々なハンドラーを利用できる長所があります。The logging handler for your
application is registered in the
app/start/global.php
start
file[/docs/4.2/lifecycle#start-files]. By
default, the logger is configured to use a single
log file; however, you may customize this behavior
as needed. Since Laravel uses the popular
Monolog[https://github.com/Seldaek/monolog]
logging library, you can take advantage of the
variety of handlers that Monolog offers.
例えば、大きな1ファイルにログを残す代わりに、日毎のファイルへログしたい場合は、スタートファイルを以下のように修正してください。For example, if you wish to use daily log files instead of a single, large file, you can make the following change to your start file:
$logFile = 'laravel.log';
Log::useDailyFiles(storage_path().'/logs/'.$logFile);
エラー詳細Error Detail
デフォルトではアプリケーションのエラー詳細は出力される設定になっています。これはエラー発生時にエラーページでスタックトレースとエラーメッセージを表示するためです。エラーの詳細を表示しないようにするためには、app/config/app.php
ファイル中のdebug
オプションをfalse
に設定してください。By default, error detail is
enabled for your application. This means that when
an error occurs you will be shown an error page with
a detailed stack trace and error message. You may
turn off error details by setting the
debug
option in your
app/config/app.php
file to
false
.
Note: 特に実機での運用環境では、エラー詳細表示をオフにしておくことを強く推奨します。Note: It is strongly recommended that you turn off error detail in a production environment.
エラーの処理Handling Errors
デフォルトでは、app/start/global.php
ファイルで全ての例外の処理を行なっています。By default, the
app/start/global.php
file contains an
error handler for all exceptions:
App::error(function(Exception $exception)
{
Log::error($exception);
});
これはもっとも基本的なエラー処理です。必要に応じ、もっと多くの処理を指定してください。ハンドラーは処理する例外のタイプヒントを元に呼び出されます。例えば、RuntimeException
のみを処理するハンドラーを作成したいのならば:This is the most basic error
handler. However, you may specify more handlers if
needed. Handlers are called based on the type-hint
of the Exception they handle. For example, you may
create a handler that only handles
RuntimeException
instances:
App::error(function(RuntimeException $exception)
{
// この例外を処理する...
});
もし例外ハンドラーが、レスポンスをリターンしたら、それはブラウザーに送信され、他のハンドラーは呼び出されません。If an exception handler returns a response, that response will be sent to the browser and no other error handlers will be called:
App::error(function(InvalidUserException $exception)
{
Log::error($exception);
return '恐れ入ります。このアカウントで不具合が発生したようです!';
});
PHPのfatalエラーをリッスンするにはApp::fatal
メソッドを使用します。To listen for PHP fatal errors,
you may use the App::fatal
method:
App::fatal(function($exception)
{
//
});
複数の例外ハンドラーを使用する場合、一般的なものから限定的なものへの順番で定義してください。例えば、全部の例外を処理するタイプException
の例外のハンドラーは、カスタム例外、例えばIlluminate\Encryption\DecryptException
のハンドラーより前で定義する必要があります。If you have several exception
handlers, they should be defined from most generic
to most specific. So, for example, a handler that
handles all exceptions of type
Exception
should be defined before a
custom exception type such as
Illuminate\Encryption\DecryptException
.
エラーハンドラーの設置場所Where To Place Error Handlers
エラーハンドラーを登録しておく、決まったデフォルトの設置場所はありません。Laravelでは、皆さんに自由に決めてもらっています。start/global.php
ファイルに設置するのはひとつのオプションです。一般的に、このファイルは「初期設定」コードを設置するために便利な場所と言えるでしょう。このファイルが乱雑になってきたら、app/errors.php
ファイルを作成し、start/global.php
スクリプトの中からrequire
することもできます。3つ目のオプションはサービスプロバイダーでハンドラーを登録する方法です。いずれにせよ、「正しい」一つの答えは存在しません。皆さんが快適に思える場所で設定してください。There is no default
"home" for error handler registrations.
Laravel offers you freedom in this area. One option
is to define the handlers in your
start/global.php
file. In general, this
is a convenient location to place any
"bootstrapping" code. If that file is
getting crowded, you could create an
app/errors.php
file, and
require
that file from your
start/global.php
script. A third option
is to create a service
provider[/docs/4.2/ioc#service-providers]
that registers the handlers. Again, there is no
single "correct" answer. Choose a location
that you are comfortable with.
HTTP例外HTTP Exceptions
いくつかの例外は、サーバーからのHTTPエラーコードを表しています。例えば、「ページが見つかりません(404)」であったり、「認証されていないエラー(401)」であったり、開発者が発生させた500エラーであることもあるでしょう。このようなレスポンスをリターンしたい場合には、以下のように行なってください。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 return such a response, use the following:
App::abort(404);
オプションとして、理由を説明することもできます。Optionally, you may provide a response:
App::abort(403, 'Unauthorized action.');
このメソッドはリクエストのライフサイクル中であれば、いつでも使用できます。This method may be used at any time during the request's lifecycle.
404エラーの処理Handling 404 Errors
カスタム404エラーページを表示するのは簡単で、アプリケーション中の全「404 見つかりません」エラーを処理するためのエラーハンドラーを登録し、エラービューをリターンしてください。You may register an error handler that handles all "404 Not Found" errors in your application, allowing you to easily return custom 404 error pages:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
ログLogging
Laravelのログ機能はパワフルなMonologをラップした、シンプルなレイヤーです。Laravelはアプリケーションにログファイルを一つ作成し、app/storage/logs/laravel.log
に保存するデフォルト設定になっています。以下のように、情報をログできます。The Laravel logging facilities
provide a simple layer on top of the powerful
Monolog[http://github.com/seldaek/monolog]
library. By default, Laravel is configured to create
a single log file for your application, and this
file is stored in
app/storage/logs/laravel.log
. You may
write information to the log like so:
Log::info('なにか役に立つ情報');
Log::warning('悪いことが起きている可能性がある情報');
Log::error('本当に悪いことが起きた情報');
ログはRFC 5424で定義されているdebug、 info、 notice、 warning、 error、 critical、alertの7レベルをサポートしています。The logger provides the seven logging levels defined in RFC 5424[http://tools.ietf.org/html/rfc5424]: debug, info, notice, warning, error, critical, and alert.
ログメソッドにはコンテキストデーターを配列で渡すこともできます。An array of contextual data may also be passed to the log methods:
Log::info('Log message', array('context' => 'Other helpful information'));
Monologにはログに使えるその他の多彩なハンドラーが用意されています。必要であれば、Laravelが内部で使用している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 = Log::getMonolog();
ログに渡される全てのメッセージを捉えるため、イベントを登録することもできます。You may also register an event to catch all messages passed to the log:
ログリスナーを登録するRegistering A Log Listener
Log::listen(function($level, $message, $context)
{
//
});