LoginSignup
7
6

More than 5 years have passed since last update.

CakePHP3 のログをためす

Posted at

この記事は、CakePHP3 Advent Calendar 2016の4日目の記事です。
CakePHP3.3のロギングを簡単に説明します。
CakePHP3.xのLogTraitを用いたものです。

環境

Welcome to CakePHP v3.3.7 Console
---------------------------------------------------------------
App : src
Path: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
PHP : 5.6.25

ロギングさせる方法

DB

config/app.phpDatasources 内の使用している設定(default)の、logtrueにする。

PHP

自分でPHPのコードに埋めたログを吐き出す。

app.php

    /**
     * Configures logging options
     */
    'Log' => [
        'debug' => [
            'className' => 'Cake\Log\Engine\FileLog',
            'path' => LOGS,
            'file' => 'debug',
            'levels' => ['notice', 'info', 'debug'],
            'url' => env('LOG_DEBUG_URL', null),
        ],
        'error' => [
            'className' => 'Cake\Log\Engine\FileLog',
            'path' => LOGS,
            'file' => 'error',
            'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
            'url' => env('LOG_ERROR_URL', null),
        ],
    ],

Controller / View(.ctp)

Componentも同じ

AppController.php
        $this->log("ʕ·ᴥ·ʔ I",'info');
        $this->log("ʕ·ᴥ·ʔ N",'notice');
        $this->log("ʕ·ᴥ·ʔ D",'debug');

        $this->log("( ゚ᆸ゚ )E",'error');
        $this->log("( ゚ᆸ゚ )W",'warning');
        $this->log("( ゚ᆸ゚ )C",'critical');
        $this->log("( ゚ᆸ゚ )A",'alert');
        $this->log("( ゚ᆸ゚ )E",'emergency');
$ tail -f *.log


==> debug.log <==
2016-12-03 07:06:29 Info: ʕ·ᴥ·ʔ I
2016-12-03 07:06:29 Notice: ʕ·ᴥ·ʔ N
2016-12-03 07:06:29 Debug: ʕ·ᴥ·ʔ D

==> error.log <==
2016-12-03 07:06:29 Error: ( ゚ᆸ゚ )E
2016-12-03 07:06:29 Warning: ( ゚ᆸ゚ )W
2016-12-03 07:06:29 Critical: ( ゚ᆸ゚ )C
2016-12-03 07:06:29 Alert: ( ゚ᆸ゚ )A
2016-12-03 07:06:29 Emergency: ( ゚ᆸ゚ )E

ログレベル使い分け

公式ドキュメントより。
上からヤバイ順。

  • Emergency: システム異常
  • Alert: 速攻対応必要あり
  • Critical: 致命的な状況
  • Error: 不具合
  • Warning: 警告
  • Notice: 通常、しかしメッセージを出したいとき
  • Info: なんらかの情報
  • Debug: デバッグメッセージ

以上。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6