設定Configuration
アプリケーションのログ機能は、Illuminate\Foundation\Bootstrap\ConfigureLogging
初期化クラスの中で設定しています。このクラスはconfig/app.php
設定ファイルのlog
オプションを参照します。The logging facilities for your
application are configured in the
Illuminate\Foundation\Bootstrap\ConfigureLogging
bootstrapper class. This class utilizes the
log
configuration option from your
config/app.php
configuration
file.
デフォルトのログ機能は、日別のログファイルを使用するように設定されています。これは必要に応じてカスタマイズできます。LaravelはポピュラーなMonologログライブラリーを使用しています。Monologが提供する様々なハンドラーを利用することができます。By default, the logger is configured to use daily log files; 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.
例えば、日別のファイルの代わりに同じログファイルを続けて使用したい場合、config/app.php
設定ファイルを以下のように変更してください。For example, if you wish to use a
single log file instead of daily files, you can make
the following change to your
config/app.php
configuration
file:
'log' => 'single'
Laravelは初めから、単一ファイル(single
)、日別ファイル(daily
)、
システムログ(syslog
)、エラーログ(errorlog
)ログモードをサポートしています。。しかし、アプリケーションに合わせるため、ログをカスタマイズしたいのでしたら、ConfigureLogging初期設定クラスを自由にオーバーライドできます。Out of the box, Laravel supported
single
, daily
,
syslog
and errorlog
logging modes. However, you are free to customize
the logging for your application as you wish by
overriding the ConfigureLogging
bootstrapper class.
エラー詳細Error Detail
config/app.php
設定ファイルのapp.debug
設定オプションにより、ブラウザに出力されるアプリケーションのエラー詳細をコントロールできます。デフォルトでは、この設定オプションは、.env
ファイルで設定されているAPP_DEBUG
環境変数を反映するように設定されています。The amount of error detail your
application displays through the browser is
controlled by the app.debug
configuration option in your
config/app.php
configuration file. By
default, this configuration option is set to respect
the APP_DEBUG
environment variable,
which is stored in your .env
file.
ローカル環境では、APP_DEBUG
環境変数をture
に設定すべきでしょう。実働環境では、この値は常にfalse
にしてください。For local development, you should
set the APP_DEBUG
environment variable
to true
. In your production
environment, this value should always be
false
.
エラーの処理Handling Errors
全例外はApp\Exceptions\Handler
クラスで処理されます。このクラスはreport
とrender
メソッドで構成されています。All exceptions are handled by the
App\Exceptions\Handler
class. This
class contains two methods: report
and
render
.
report
メソッドは例外のログや、BugSnagのような外部サービスにメールを送るために使用します。デフォルトでは、例外をログしている親クラスの基本的な実装へ渡すだけです。しかし、例外をお好きなようにログしてかまいません。それぞれの例外タイプを異なった方法でログする必要があれば、PHPのinstanceof
比較演算子が使えます。The report
method is
used to log exceptions or send them to an external
service like BugSnag[https://bugsnag.com]. By
default, the report
method simply
passes the exception to the base implementation on
the parent class where the exception is logged.
However, you are free to log exceptions however you
wish. If you need to report different types of
exceptions in different ways, you may use the PHP
instanceof
comparison
operator:
/**
* 例外をレポートもしくはログする
*
* SentryやBugsnagなどにレポートするために最適な場所
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
if ($e instanceof CustomException)
{
//
}
return parent::report($e);
}
render
メソッドは例外をHTTPレスポンスに変換し、ブラウザに送り返す役割を持っています。デフォルトでは、例外はレスポンスを生成するベースクラスへ渡されます。しかし、例外のタイプを調べ、カスタムレスポンスを返すのも自由です。The render
method is
responsible for converting the 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.
例外ハンドラーのdontReport
プロパティは、ログしない例外タイプの配列で構成されています。デフォルトでは、404エラーを示す例外はログファイルに書き込まれません。必要に応じ、他の例外タイプを配列に追加してください。The dontReport
property of the exception handler contains an array
of exception types that will not be logged. By
default, exceptions resulting from 404 errors are
not written to your log files. You may add other
exception types to this array as needed.
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:
abort(404);
オプションとして、理由を説明することもできます。Optionally, you may provide a response:
abort(403, '許可されていないアクションです。');
このメソッドはリクエストのライフサイクル中であれば、いつでも使用できます。This method may be used at any time during the request's lifecycle.
404エラーページのカスタマイズCustom 404 Error Page
全ての404エラーでカスタムビューを返すためには、resources/views/errors/404.blade.php
ファイルを作成してください。アプリケーションで起きる全404エラーに対し、このビューが表示されるようになります。To return a custom view for all
404 errors, create a
resources/views/errors/404.blade.php
file. This view will be served on all 404 errors
generated by your application.
ログLogging
Laravelのログ機能は、強力なMonologライブラリーのシンプルな上位レイヤーを提供しています。Laravelはデフォルトで、アプリケーションの日別ログファイルをstorage/logs
ディレクトリーへ作成するように設定されています。ログへ情報を書き込みたい場合は、以下のように行います。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
daily log files for your application which are
stored in the storage/logs
directory.
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', ['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 Event Listener
Log::listen(function($level, $message, $context)
{
//
});