PHP
CMS
SNS
HumHub

HumHubでのLoggerの使い方

概要

HumHubでのLoggerの使い方を公式ドキュメントで調べようと思ったら、TBDになっていた。

Yiiのドキュメントで調べて、Yii::debug()を使おうとしたら、
Error: Call to undefined method Yii::debug() になった。

なので、HumHubのLoggerのソースを調べてみた。

関連

ソースを追ってみる

ソースを調べて見たところ、debugは使えないみたい。

protected/vendor/yiisoft/yii2/log/Logger.php

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\log;

use Yii;
use yii\base\Component;

/**
 * Logger records logged messages in memory and sends them to different targets if [[dispatcher]] is set.
 *
 * A Logger instance can be accessed via `Yii::getLogger()`. You can call the method [[log()]] to record a single log message.
 * For convenience, a set of shortcut methods are provided for logging messages of various severity levels
 * via the [[Yii]] class:
 *
 * - [[Yii::trace()]]
 * - [[Yii::error()]]
 * - [[Yii::warning()]]
 * - [[Yii::info()]]
 * - [[Yii::beginProfile()]]
 * - [[Yii::endProfile()]]
 *
 * For more details and usage information on Logger, see the [guide article on logging](guide:runtime-logging).
 *
 * When the application ends or [[flushInterval]] is reached, Logger will call [[flush()]]
 * to send logged messages to different log targets, such as [[FileTarget|file]], [[EmailTarget|email]],
 * or [[DbTarget|database]], with the help of the [[dispatcher]].
 *
 * @property array $dbProfiling The first element indicates the number of SQL statements executed, and the
 * second element the total time spent in SQL execution. This property is read-only.
 * @property float $elapsedTime The total elapsed time in seconds for current request. This property is
 * read-only.
 * @property array $profiling The profiling results. Each element is an array consisting of these elements:
 * `info`, `category`, `timestamp`, `trace`, `level`, `duration`, `memory`, `memoryDiff`. The `memory` and
 * `memoryDiff` values are available since version 2.0.11. This property is read-only.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Logger extends Component
{

/* snip */

Usage

ショートカットメソッドを使う場合

use Yii;

Yii::trace('This is trace');
Yii::error('This is error');
Yii::warning('This is warning');
Yii::info('This is info');
Yii::beginProfile('This is beginProfile');
Yii::endProfile('This is endProfile');

Logger instanceを使う場合

use Yii;

$logger = Yii::getLogger();
// error
$logger->log('This is error', 1);
// warning
$logger->log('This is warning', 2);
// info
$logger->log('This is info', 3);

ログの出力場所

以下のいずれかで、出力されたログを確認できる

  • DBのlogテーブル
  • https://サイトドメイン/admin/logging

sqlのdebug方法

参考:https://www.yiiframework.com/wiki/857/show-raw-sql-query

echo $query->createCommand()->sql;
echo $query->createCommand()->getRawSql();