I am using yii2-debug. I am getting this error:
Unable to send log via yii\debug\LogTarget: Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\console\Request::getAbsoluteUrl()'
I tracked this down to: /yiisoft/yii2-debug/src/LogTarget.php
For now, I’ve updated the $summary code so the error will stop (see my diff at the end), but I’m not clear what is really causing the error?
I configure LogTarget in both common/config/main.php and console/config/main.php
common/config/main.php:
$config['components']['log']['targets'][] = [
'class' => 'yii\log\FileTarget',
'enabled' => true,
//'categories' => [ 'application' ],
'levels' => [ 'error', 'warning' ],
'enableRotation' => true,
'logFile' => '@runtime/logs/errors.log',
'logVars' => [],
];
$config['components']['log']['targets'][] = [
'class' => 'yii\log\FileTarget',
'enabled' => true,
//'categories' => [ 'application' ],
'levels' => [ 'error' ],
'enableRotation' => true,
'logFile' => '@runtime/logs/fatal.log',
'logVars' => [],
];
$config['components']['log']['targets'][] = [
'class' => 'yii\log\EmailTarget',
'mailer' => 'mailer',
'levels' => ['error', 'warning'],
'message' => [
'from' => ['xxx@mydomain'],
'to' => ['yyyy@mydomain'],
'subject' => sprintf("%s - %s", gethostname(), "ERROR"),
],
];
$config['components']['log']['targets'][] = [
'class' => 'yii\log\FileTarget',
'enabled' => true,
'levels' => [ 'error', 'warning', 'trace' ],
'enableRotation' => true,
'logFile' => '@runtime/logs/debug.log',
'logVars' => [],
];
console/config/main.php
$config['components']['log']['targets'][] = [
// https://github.com/pahanini/yii2-consolelog
'class' => 'pahanini\log\ConsoleTarget',
'enabled' => true,
'levels' => ['error', 'warning', 'trace'],
'exportInterval' => 1,
'logVars' => [],
];
diff:
diff --git a/vendor/yiisoft/yii2-debug/src/LogTarget.php b/vendor/yiisoft/yii2-debug/src/LogTarget.php
index e0867ad9..680e594c 100644
--- a/vendor/yiisoft/yii2-debug/src/LogTarget.php
+++ b/vendor/yiisoft/yii2-debug/src/LogTarget.php
@@ -197,16 +197,27 @@ class LogTarget extends Target
$request = Yii::$app->getRequest();
$response = Yii::$app->getResponse();
- $summary = [
- 'tag' => $this->tag,
- 'url' => $request->getAbsoluteUrl(),
- 'ajax' => (int) $request->getIsAjax(),
- 'method' => $request->getMethod(),
- 'ip' => $request->getUserIP(),
- 'time' => $_SERVER['REQUEST_TIME_FLOAT'],
- 'statusCode' => $response->statusCode,
- 'sqlCount' => $this->getSqlTotalCount(),
- ];
+
+ // are we working from the console?
+ if (! method_exists($request, "getAbsoluteUrl")) {
+ $summary = [
+ 'tag' => $this->tag,
+ 'time' => $_SERVER['REQUEST_TIME_FLOAT'],
+ 'sqlCount' => $this->getSqlTotalCount(),
+ ];
+ }
+ else {
+ $summary = [
+ 'tag' => $this->tag,
+ 'url' => $request->getAbsoluteUrl(),
+ 'ajax' => (int) $request->getIsAjax(),
+ 'method' => $request->getMethod(),
+ 'ip' => $request->getUserIP(),
+ 'time' => $_SERVER['REQUEST_TIME_FLOAT'],
+ 'statusCode' => $response->statusCode,
+ 'sqlCount' => $this->getSqlTotalCount(),
+ ];
+ }
if (isset($this->module->panels['mail'])) {
$mailFiles = $this->module->panels['mail']->getMessagesFileName();