-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
}; | ||
} | ||
} |