Skip to content

Commit

Permalink
replace logentries with stream logging
Browse files Browse the repository at this point in the history
  • Loading branch information
briskt committed Apr 7, 2020
1 parent 6b08fca commit e59c4b8
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 394 deletions.
50 changes: 31 additions & 19 deletions application/common/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use common\components\Emailer;
use Sil\JsonLog\target\EmailServiceTarget;
use Sil\JsonLog\target\JsonSyslogTarget;
use Sil\JsonLog\target\JsonStreamTarget;
use Sil\PhpEnv\Env;
use yii\helpers\ArrayHelper;

Expand Down Expand Up @@ -58,6 +58,24 @@
'enableHIBP' => $passwordRulesEnv['enableHIBP'] ?? true,
];

$logPrefix = function () {
$request = Yii::$app->request;
$prefixData = [
'env' => YII_ENV,
];
if ($request instanceof \yii\web\Request) {
// Assumes format: Bearer consumer-module-name-32randomcharacters
$prefixData['id'] = substr($request->headers['Authorization'], 7, 16) ?: 'unknown';
$prefixData['ip'] = $request->getUserIP();
$prefixData['method'] = $request->getMethod();
$prefixData['url'] = $request->getUrl();
} elseif ($request instanceof \yii\console\Request) {
$prefixData['id'] = '(console)';
}

return Json::encode($prefixData);
};

return [
'id' => 'app-common',
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
Expand All @@ -76,29 +94,23 @@
'traceLevel' => 0,
'targets' => [
[
'class' => JsonSyslogTarget::class,
'class' => JsonStreamTarget::class,
'url' => 'php://stdout',
'levels' => ['info'],
'logVars' => [],
'categories' => ['application'],
'prefix' => $logPrefix,
],
[
'class' => JsonStreamTarget::class,
'url' => 'php://stderr',
'levels' => ['error', 'warning'],
'except' => [
'yii\web\HttpException:401',
'yii\web\HttpException:404',
],
'logVars' => [], // Disable logging of _SERVER, _POST, etc.
'prefix' => function($message) use ($appEnv) {
$prefixData = [
'env' => $appEnv,
];

// There is no user when a console command is run
try {
$appUser = \Yii::$app->user;
} catch (\Exception $e) {
$appUser = null;
}
if ($appUser && ! \Yii::$app->user->isGuest) {
$prefixData['user'] = \Yii::$app->user->identity->email;
}
return \yii\helpers\Json::encode($prefixData);
},
'logVars' => [],
'prefix' => $logPrefix,
],
[
'class' => EmailServiceTarget::class,
Expand Down
5 changes: 1 addition & 4 deletions application/common/models/EventLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public static function log($topic, $details, $userId = null)
$eventLog->topic = $topic;
$eventLog->details = is_array($details) ? Json::encode($details) : $details;

/*
* Save event to LogEntries
*/
try {
$user = User::findOne(['id' => $userId]);
if ($user !== null) {
Expand All @@ -62,4 +59,4 @@ public static function log($topic, $details, $userId = null)
}
}

}
}
2 changes: 1 addition & 1 deletion application/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"yiisoft/yii2-gii": "*",
"silinternational/email-service-php-client": "^2.0.1",
"silinternational/php-env": "^2.1.1",
"silinternational/yii2-json-log-targets": "^1.0.0",
"silinternational/yii2-json-log-targets": "^1.1.0",
"silinternational/yii2-email-log-target": "^1.0.1",
"silinternational/idp-id-broker-php-client": "^3.1.0",
"silinternational/zxcvbn-api-client-php": "^2.0",
Expand Down
Loading

0 comments on commit e59c4b8

Please sign in to comment.