Skip to content

Commit

Permalink
Replace usages of add_log() method with Log facade (#2658)
Browse files Browse the repository at this point in the history
CDash currently uses the custom `add_log()` method as a thin wrapper
around Laravel's Log facade. This PR removes the `add_log()` method as
part of our ongoing effort to use Laravel functionality directly
wherever possible. As part of this work, project and build context
information was added to all log messages created during the submission
process and under routes where the project or build information is
known.
  • Loading branch information
williamjallen authored Jan 9, 2025
1 parent 94c4492 commit c5f3221
Show file tree
Hide file tree
Showing 42 changed files with 401 additions and 652 deletions.
5 changes: 5 additions & 0 deletions app/Http/Controllers/AbstractBuildController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Utils\TestingDay;
use CDash\Model\Build;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;

abstract class AbstractBuildController extends AbstractProjectController
Expand All @@ -27,6 +28,10 @@ protected function setBuild(Build $build): void
abort(404, 'Build does not exist. Maybe it has been deleted.');
}

Log::shareContext([
'buildid' => $build->Id,
]);

$this->setProject($build->GetProject());
$this->build = $build;
$this->date = TestingDay::get($this->project, $this->build->StartTime);
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/AbstractProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Utils\TestingDay;
use CDash\Model\Project;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;

abstract class AbstractProjectController extends AbstractController
Expand Down Expand Up @@ -32,6 +33,10 @@ protected function setProject(Project $project): void
{
Gate::authorize('view-project', $project);

Log::shareContext([
'projectid' => $project->Id,
]);

$this->project = $project;
$this->project->Fill();
$this->date = TestingDay::get($this->project, date(FMT_DATETIME));
Expand Down
9 changes: 9 additions & 0 deletions app/Jobs/ProcessSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public function __construct($filename, $projectid, $buildid, $expected_md5)
{
$this->timeout = config('cdash.queue_timeout');

$context = [];
if (isset($projectid)) {
$context['projectid'] = $projectid;
}
if (isset($buildid)) {
$context['buildid'] = $buildid;
}
Log::shareContext($context);

$this->filename = $filename;
$this->projectid = $projectid;
$this->buildid = $buildid;
Expand Down
11 changes: 5 additions & 6 deletions app/Listeners/EmailSentListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ public function handle(MessageSent $event): void
}
$addresses = implode(', ', $addresses);

// TODO: Clean up this logging code by using Log facade with multiple message levels
$context = [];
if (config('app.debug')) {
add_log($addresses, 'TESTING: EMAIL', LOG_DEBUG);
add_log($event->message->getSubject(), 'TESTING: EMAILTITLE', LOG_DEBUG);
add_log($event->message->getTextBody(), 'TESTING: EMAILBODY', LOG_DEBUG);
} else {
Log::info("Sent email titled '{$event->message->getSubject()}' to {$addresses}");
$context['subject'] = $event->message->getSubject();
$context['body'] = $event->message->getTextBody();
}

Log::info("Sent email titled '{$event->message->getSubject()}' to {$addresses}", $context);
}
}
16 changes: 4 additions & 12 deletions app/Utils/RepositoryUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,7 @@ public static function post_pull_request_comment($projectid, $pull_request, $com
if (method_exists(self::class, $PR_func)) {
self::$PR_func($project, $pull_request, $comment, $cdash_url);
} else {
add_log("PR commenting not implemented for '$project->CvsViewerType'",
'post_pull_request_comment()', LOG_WARNING);
Log::warning("PR commenting not implemented for '$project->CvsViewerType'");
}
}

Expand Down Expand Up @@ -785,8 +784,7 @@ public static function post_github_pull_request_comment(Project $project, $pull_

if (is_null($repo) || !isset($repo['username'])
|| !isset($repo['password'])) {
add_log("Missing repository info for project #$project->Id",
'post_github_pull_request_comment()', LOG_WARNING);
Log::warning("Missing repository info for project #$project->Id");
return;
}

Expand Down Expand Up @@ -817,17 +815,11 @@ public static function post_github_pull_request_comment(Project $project, $pull_

$retval = curl_exec($ch);
if ($retval === false) {
add_log(
'cURL error: ' . curl_error($ch),
'post_github_pull_request_comment',
LOG_ERR, $project->Id);
Log::error('cURL error: ' . curl_error($ch), ['projectid' => $project->Id]);
} elseif (config('app.debug')) {
$matches = [];
preg_match("#/comments/(\d+)#", $retval, $matches);
add_log(
'Just posted comment #' . $matches[1],
'post_github_pull_request_comment',
LOG_DEBUG, $project->Id);
Log::debug('Just posted comment #' . $matches[1], ['projectid' => $project->Id]);
}

curl_close($ch);
Expand Down
114 changes: 78 additions & 36 deletions app/cdash/app/Model/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ public function SetSubProject($subproject): int|bool
}

if (empty($this->ProjectId)) {
add_log('ProjectId not set' . $subproject, 'Build::SetSubProject', LOG_ERR,
$this->ProjectId, $this->Id,
ModelType::BUILD, $this->Id);
Log::error('ProjectId not set', [
'projectid' => $this->ProjectId,
'buildid' => $this->Id,
]);
return false;
}

Expand Down Expand Up @@ -210,8 +211,10 @@ public function SetSubProject($subproject): int|bool
$Label->Text = $subProject->GetName();
$Label->Insert();

add_log('New subproject detected: ' . $subproject, 'Build::SetSubProject',
LOG_INFO, $this->ProjectId, $this->Id, ModelType::BUILD, $this->Id);
Log::info('New subproject detected: ' . $subproject, [
'projectid' => $this->ProjectId,
'buildid' => $this->Id,
]);
return true;
}

Expand Down Expand Up @@ -575,7 +578,11 @@ public function GetErrors(array $propertyFilters = [], int $fetchStyle = PDO::FE
// This needs to take into account that this build may be a parent build
if ($this->Errors === []) {
if (!$this->Id) {
add_log('BuildId not set', 'Build::GetErrors', LOG_WARNING);
Log::warning('BuildId not set', [
'projectid' => $this->ProjectId,
'buildid' => $this->Id,
'function' => 'Build::GetErrors',
]);
return false;
}

Expand All @@ -602,7 +609,11 @@ public function GetFailures(array $propertyFilters = [], int $fetchStyle = PDO::
// This needs to take into account that this build may be a parent build
if ($this->Failures === []) {
if (!$this->Id) {
add_log('BuildId not set', 'Build::GetFailures', LOG_WARNING);
Log::warning('BuildId not set', [
'projectid' => $this->ProjectId,
'buildid' => $this->Id,
'function' => 'Build::GetFailures',
]);
return false;
}

Expand Down Expand Up @@ -632,7 +643,9 @@ protected function PropertyFilter(array $rows, array $filters): array
foreach ($filters as $prop => $value) {
if (is_object($row)) {
if (!property_exists($row, $prop)) {
add_log("Cannot filter on {$prop}: property does not exist", 'Build::PropertyFilter', LOG_WARNING);
Log::warning("Cannot filter on {$prop}: property does not exist", [
'function' => 'Build::PropertyFilter',
]);
continue;
}

Expand All @@ -641,7 +654,9 @@ protected function PropertyFilter(array $rows, array $filters): array
}
} elseif (is_array($row)) {
if (!array_key_exists($prop, $row)) {
add_log("Cannot filter on {$prop}: property does not exist", 'Build::PropertyFilter', LOG_WARNING);
Log::warning("Cannot filter on {$prop}: property does not exist", [
'function' => 'Build::PropertyFilter',
]);
continue;
}

Expand Down Expand Up @@ -767,9 +782,10 @@ public function GetIdFromName(?string $subproject): int
public function InsertLabelAssociations(): bool
{
if (!$this->Id) {
add_log('No Build::Id - cannot call $label->Insert...', 'Build::InsertLabelAssociations', LOG_ERR,
$this->ProjectId, $this->Id,
ModelType::BUILD, $this->Id);
Log::error('No Build::Id - cannot call $label->Insert...', [
'function' => 'Build::InsertLabelAssociations',
'projectid' => $this->ProjectId,
]);
return false;
}

Expand Down Expand Up @@ -942,8 +958,10 @@ public function GetMissingTests(): array
$this->MissingTests = [];

if (!$this->Id) {
add_log('BuildId is not set', 'Build::GetMissingTests', LOG_ERR,
$this->ProjectId, $this->Id, ModelType::BUILD, $this->Id);
Log::error('BuildId is not set', [
'function' => 'Build::GetMissingTests',
'projectid' => $this->ProjectId,
]);
return [];
}

Expand Down Expand Up @@ -990,8 +1008,10 @@ public function GetNumberOfMissingTests(): int
private function GetTests(string $criteria, int $maxitems = 0): array|false
{
if (!$this->Id) {
add_log('BuildId is not set', 'Build::GetTests', LOG_ERR,
$this->ProjectId, $this->Id, ModelType::BUILD, $this->Id);
Log::error('BuildId is not set', [
'function' => 'Build::GetTests',
'projectid' => $this->ProjectId,
]);
return false;
}

Expand Down Expand Up @@ -1075,8 +1095,10 @@ public function GetNotRunTests(int $maxitems = 0): array|false
public function GetErrorDifferences(): array|false
{
if (!$this->Id) {
add_log('BuildId is not set', 'Build::GetErrorDifferences', LOG_ERR,
$this->ProjectId, $this->Id, ModelType::BUILD, $this->Id);
Log::error('BuildId is not set', [
'function' => 'Build::GetErrorDifferences',
'projectid' => $this->ProjectId,
]);
return false;
}

Expand Down Expand Up @@ -1155,9 +1177,10 @@ public function GetErrorDifferences(): array|false
public function ComputeDifferences(): bool
{
if (!$this->Id) {
add_log('BuildId is not set', 'Build::ComputeDifferences', LOG_ERR,
$this->ProjectId, $this->Id,
ModelType::BUILD, $this->Id);
Log::error('BuildId is not set', [
'function' => 'Build::ComputeDifferences',
'projectid' => $this->ProjectId,
]);
return false;
}

Expand Down Expand Up @@ -1228,14 +1251,19 @@ public function ComputeConfigureDifferences(): bool
public function ComputeTestTiming(): bool
{
if (!$this->Id) {
add_log('BuildId is not set', 'Build::ComputeTestTiming', LOG_ERR,
$this->ProjectId, $this->Id, ModelType::BUILD, $this->Id);
Log::error('BuildId is not set', [
'function' => 'Build::ComputeTestTiming',
'projectid' => $this->ProjectId,
]);
return false;
}

if (!$this->ProjectId) {
add_log('ProjectId is not set', 'Build::ComputeTestTiming', LOG_ERR,
$this->ProjectId, $this->Id, ModelType::BUILD, $this->Id);
Log::error('ProjectId is not set', [
'function' => 'Build::ComputeTestTiming',
'projectid' => $this->ProjectId,
'buildid' => $this->Id,
]);
return false;
}

Expand Down Expand Up @@ -1386,13 +1414,18 @@ public function ComputeTestTiming(): bool
public function ComputeUpdateStatistics(): bool
{
if (!$this->Id) {
add_log('Id is not set', 'Build::ComputeUpdateStatistics', LOG_ERR,
$this->ProjectId, $this->Id, ModelType::BUILD, $this->Id);
Log::error('Id is not set', [
'function' => 'Build::ComputeUpdateStatistics',
'projectid' => $this->ProjectId,
]);
return false;
}

if (!$this->ProjectId) {
add_log('ProjectId is not set', 'Build::ComputeUpdateStatistics', LOG_ERR, 0, $this->Id);
Log::error('ProjectId is not set', [
'function' => 'Build::ComputeUpdateStatistics',
'buildid' => $this->Id,
]);
return false;
}

Expand Down Expand Up @@ -1708,7 +1741,9 @@ public function GetLabels($labelarray = []): array|false
public function GetGroup(): int|false
{
if (!$this->Id) {
add_log('Id not set', 'Build GetGroup()', LOG_ERR);
Log::error('Id is not set', [
'function' => 'Build::GetGroup',
]);
return false;
}
$stmt = $this->PDO->prepare(
Expand Down Expand Up @@ -1751,7 +1786,9 @@ public function GetNumberOfWarnings(): int|false
public function GetUploadedFilesOrUrls(): array|false
{
if (!$this->Id) {
add_log('Id not set', 'Build GetUploadedFilesOrUrls()', LOG_ERR);
Log::error('Id not set', [
'function' => 'Build GetUploadedFilesOrUrls()',
]);
return false;
}

Expand Down Expand Up @@ -2226,10 +2263,11 @@ public function GetParentId(): int
public function SetParentId($parentid): void
{
if ($parentid > 0 && (int) $parentid === (int) $this->Id) {
add_log("Attempt to mark build $this->Id as its own parent",
'Build::SetParentId', LOG_ERR,
$this->ProjectId, $this->Id,
ModelType::BUILD, $this->Id);
Log::error("Attempt to mark build $this->Id as its own parent", [
'function' => 'Build::SetParentId',
'projectid' => $this->ProjectId,
'buildid' => $this->Id,
]);
return;
}
$this->ParentId = (int) $parentid;
Expand Down Expand Up @@ -2260,7 +2298,9 @@ public function ComputeTestingDayBounds(): bool
private function GetErrorsForChildren(int $fetchStyle = PDO::FETCH_ASSOC): array|false
{
if (!$this->Id) {
add_log('Id not set', 'Build::GetErrorsForChildren', LOG_WARNING);
Log::warning('Id not set', [
'function' => 'Build::GetErrorsForChildren',
]);
return false;
}

Expand Down Expand Up @@ -2288,7 +2328,9 @@ private function GetErrorsForChildren(int $fetchStyle = PDO::FETCH_ASSOC): array
private function GetFailuresForChildren(int $fetchStyle = PDO::FETCH_ASSOC): array|false
{
if (!$this->Id) {
add_log('Id not set', 'Build::GetFailuresForChildren', LOG_WARNING);
Log::warning('Id not set', [
'function' => 'Build::GetFailuresForChildren',
]);
return false;
}

Expand Down
Loading

0 comments on commit c5f3221

Please sign in to comment.