Skip to content

Commit

Permalink
making file logging more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Jan 8, 2020
1 parent 9d73235 commit 46b53dc
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions src/helpers/LoggerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
use Craft;
use craft\log\FileTarget;
use yii\log\Logger;
use yii\web\Request;

/**
* @author Flipbox Factory <[email protected]>
* @since 2.0.0
*/
class LoggerHelper
{
public static $requireSession = true;

/**
* Takes an array of log categories and creates log target configs
*
Expand Down Expand Up @@ -50,11 +53,7 @@ public static function targetConfig(string $category, array $targetConfig = []):
return static::fileTargetConfig($category, $targetConfig);
}

return call_user_func(
self::config(),
$category,
$targetConfig
);
return static::bootstrapConfig($category, $targetConfig);
}

/**
Expand Down Expand Up @@ -84,33 +83,47 @@ public static function fileTargetConfig(string $category, array $targetConfig =
}

/**
* @return callable
* @param string $category
* @param array $config
* @return array
* @since 2.6.2
*/
public static function config(): callable
public static function bootstrapConfig(string $category, array $config = []): array
{
return function (string $category, array $config = []) {
// Only log console requests and web requests that aren't getAuthTimeout requests
$isConsoleRequest = Craft::$app->getRequest()->getIsConsoleRequest();
if (!$isConsoleRequest && !Craft::$app->getUser()->enableSession) {
return [];
}
$request = Craft::$app->getRequest();
// Only log console requests and web requests that aren't getAuthTimeout requests
$isConsoleRequest = $request instanceof Request && $request->getIsConsoleRequest();
if (!$isConsoleRequest && (static::$requireSession && !Craft::$app->getUser()->enableSession)) {
return [];
}

$target = [
'logVars' => [],
'categories' => [$category, $category . ':*']
];

$target = [
'logVars' => [],
'categories' => [$category, $category . ':*']
];

if (!$isConsoleRequest) {
// Only log errors and warnings, unless Craft is running in Dev Mode or it's being installed/updated
if (!YII_DEBUG
&& Craft::$app->getIsInstalled()
&& !Craft::$app->getUpdates()->getIsCraftDbMigrationNeeded()
) {
$target['levels'] = Logger::LEVEL_ERROR | Logger::LEVEL_WARNING;
}
if (!$isConsoleRequest) {
// Only log errors and warnings, unless Craft is running in Dev Mode or it's being installed/updated
if (!YII_DEBUG
&& Craft::$app->getIsInstalled()
&& !Craft::$app->getUpdates()->getIsCraftDbMigrationNeeded()
) {
$target['levels'] = Logger::LEVEL_ERROR | Logger::LEVEL_WARNING;
}
}

return array_merge($target, $config);
}

return array_merge($target, $config);
/**
* @return callable
*
* @deprecated
*/
public static function config(): callable
{
return function (string $category, array $config = []) {
return static::bootstrapConfig($category, $config);
};
}
}

0 comments on commit 46b53dc

Please sign in to comment.