Skip to content

Commit 3622d3e

Browse files
committed
log Flow activity
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent 1bf44e1 commit 3622d3e

13 files changed

Lines changed: 468 additions & 3 deletions

File tree

apps/workflowengine/appinfo/info.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
<nextcloud min-version="18" max-version="18"/>
2626
</dependencies>
2727

28+
<background-jobs>
29+
<job>OCA\WorkflowEngine\BackgroundJobs\Rotate</job>
30+
</background-jobs>
31+
2832
<repair-steps>
2933
<post-migration>
3034
<step>OCA\WorkflowEngine\Migration\PopulateNewlyIntroducedDatabaseFields</step>

apps/workflowengine/composer/composer/autoload_classmap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
return array(
99
'OCA\\WorkflowEngine\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
10+
'OCA\\WorkflowEngine\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php',
1011
'OCA\\WorkflowEngine\\Check\\AbstractStringCheck' => $baseDir . '/../lib/Check/AbstractStringCheck.php',
1112
'OCA\\WorkflowEngine\\Check\\FileMimeType' => $baseDir . '/../lib/Check/FileMimeType.php',
1213
'OCA\\WorkflowEngine\\Check\\FileName' => $baseDir . '/../lib/Check/FileName.php',
@@ -24,10 +25,12 @@
2425
'OCA\\WorkflowEngine\\Controller\\RequestTime' => $baseDir . '/../lib/Controller/RequestTime.php',
2526
'OCA\\WorkflowEngine\\Controller\\UserWorkflowsController' => $baseDir . '/../lib/Controller/UserWorkflowsController.php',
2627
'OCA\\WorkflowEngine\\Entity\\File' => $baseDir . '/../lib/Entity/File.php',
28+
'OCA\\WorkflowEngine\\Helper\\LogContext' => $baseDir . '/../lib/Helper/LogContext.php',
2729
'OCA\\WorkflowEngine\\Helper\\ScopeContext' => $baseDir . '/../lib/Helper/ScopeContext.php',
2830
'OCA\\WorkflowEngine\\Manager' => $baseDir . '/../lib/Manager.php',
2931
'OCA\\WorkflowEngine\\Migration\\PopulateNewlyIntroducedDatabaseFields' => $baseDir . '/../lib/Migration/PopulateNewlyIntroducedDatabaseFields.php',
3032
'OCA\\WorkflowEngine\\Migration\\Version2000Date20190808074233' => $baseDir . '/../lib/Migration/Version2000Date20190808074233.php',
33+
'OCA\\WorkflowEngine\\Service\\Logger' => $baseDir . '/../lib/Service/Logger.php',
3134
'OCA\\WorkflowEngine\\Service\\RuleMatcher' => $baseDir . '/../lib/Service/RuleMatcher.php',
3235
'OCA\\WorkflowEngine\\Settings\\ASettings' => $baseDir . '/../lib/Settings/ASettings.php',
3336
'OCA\\WorkflowEngine\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',

apps/workflowengine/composer/composer/autoload_static.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ComposerStaticInitWorkflowEngine
2222

2323
public static $classMap = array (
2424
'OCA\\WorkflowEngine\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
25+
'OCA\\WorkflowEngine\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php',
2526
'OCA\\WorkflowEngine\\Check\\AbstractStringCheck' => __DIR__ . '/..' . '/../lib/Check/AbstractStringCheck.php',
2627
'OCA\\WorkflowEngine\\Check\\FileMimeType' => __DIR__ . '/..' . '/../lib/Check/FileMimeType.php',
2728
'OCA\\WorkflowEngine\\Check\\FileName' => __DIR__ . '/..' . '/../lib/Check/FileName.php',
@@ -39,10 +40,12 @@ class ComposerStaticInitWorkflowEngine
3940
'OCA\\WorkflowEngine\\Controller\\RequestTime' => __DIR__ . '/..' . '/../lib/Controller/RequestTime.php',
4041
'OCA\\WorkflowEngine\\Controller\\UserWorkflowsController' => __DIR__ . '/..' . '/../lib/Controller/UserWorkflowsController.php',
4142
'OCA\\WorkflowEngine\\Entity\\File' => __DIR__ . '/..' . '/../lib/Entity/File.php',
43+
'OCA\\WorkflowEngine\\Helper\\LogContext' => __DIR__ . '/..' . '/../lib/Helper/LogContext.php',
4244
'OCA\\WorkflowEngine\\Helper\\ScopeContext' => __DIR__ . '/..' . '/../lib/Helper/ScopeContext.php',
4345
'OCA\\WorkflowEngine\\Manager' => __DIR__ . '/..' . '/../lib/Manager.php',
4446
'OCA\\WorkflowEngine\\Migration\\PopulateNewlyIntroducedDatabaseFields' => __DIR__ . '/..' . '/../lib/Migration/PopulateNewlyIntroducedDatabaseFields.php',
4547
'OCA\\WorkflowEngine\\Migration\\Version2000Date20190808074233' => __DIR__ . '/..' . '/../lib/Migration/Version2000Date20190808074233.php',
48+
'OCA\\WorkflowEngine\\Service\\Logger' => __DIR__ . '/..' . '/../lib/Service/Logger.php',
4649
'OCA\\WorkflowEngine\\Service\\RuleMatcher' => __DIR__ . '/..' . '/../lib/Service/RuleMatcher.php',
4750
'OCA\\WorkflowEngine\\Settings\\ASettings' => __DIR__ . '/..' . '/../lib/Settings/ASettings.php',
4851
'OCA\\WorkflowEngine\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',

apps/workflowengine/lib/AppInfo/Application.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
namespace OCA\WorkflowEngine\AppInfo;
2323

2424
use OCA\WorkflowEngine\Controller\RequestTime;
25+
use OCA\WorkflowEngine\Helper\LogContext;
2526
use OCA\WorkflowEngine\Manager;
27+
use OCA\WorkflowEngine\Service\Logger;
2628
use OCP\AppFramework\QueryException;
2729
use OCP\EventDispatcher\Event;
2830
use OCP\Template;
@@ -97,6 +99,16 @@ function ($event) use ($eventName, $operationClass, $entityClass) {
9799
$ruleMatcher->setEntity($entity);
98100
$ruleMatcher->setOperation($operation);
99101

102+
$ctx = new LogContext();
103+
$ctx
104+
->setOperation($operation)
105+
->setEntity($entity)
106+
->setEventName($eventName);
107+
108+
/** @var Logger $flowLogger */
109+
$flowLogger = $this->getContainer()->query(Logger::class);
110+
$flowLogger->logEventInit($ctx);
111+
100112
if ($event instanceof Event) {
101113
$entity->prepareRuleMatcher($ruleMatcher, $eventName, $event);
102114
$operation->onEvent($eventName, $event, $ruleMatcher);
@@ -117,6 +129,7 @@ function ($event) use ($eventName, $operationClass, $entityClass) {
117129
]
118130
);
119131
}
132+
$flowLogger->logEventDone($ctx);
120133

121134
} catch (QueryException $e) {
122135
// Ignore query exceptions since they might occur when an entity/operation were setup before by an app that is disabled now
@@ -126,5 +139,7 @@ function ($event) use ($eventName, $operationClass, $entityClass) {
126139
}, $eventNames ?? []);
127140
}
128141
}
142+
143+
129144
}
130145
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
4+
*
5+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\WorkflowEngine\BackgroundJobs;
25+
26+
use OC\BackgroundJob\TimedJob;
27+
use OCA\WorkflowEngine\AppInfo\Application;
28+
use OCP\Log\RotationTrait;
29+
30+
class Rotate extends TimedJob {
31+
use RotationTrait;
32+
33+
public function __construct() {
34+
$this->setInterval(60*60*3);
35+
}
36+
37+
protected function run($argument) {
38+
$config = \OC::$server->getConfig();
39+
$default = $config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/flow.log';
40+
$this->filePath = trim((string)$config->getAppValue(Application::APP_ID, 'logfile', $default));
41+
42+
if($this->filePath === '') {
43+
// disabled, nothing to do
44+
return;
45+
}
46+
47+
$this->maxSize = $config->getSystemValue('log_rotate_size', 100 * 1024 * 1024);
48+
49+
if($this->shouldRotateBySize()) {
50+
$this->rotate();
51+
}
52+
}
53+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
5+
*
6+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCA\WorkflowEngine\Helper;
26+
27+
use OCP\WorkflowEngine\IEntity;
28+
use OCP\WorkflowEngine\IManager;
29+
use OCP\WorkflowEngine\IOperation;
30+
31+
class LogContext {
32+
/** @var array */
33+
protected $details;
34+
35+
public function setDescription(string $description): LogContext {
36+
$this->details['description'] = $description;
37+
return $this;
38+
}
39+
40+
public function setScopes(array $scopes): LogContext {
41+
$this->details['scopes'] = [];
42+
foreach ($scopes as $scope) {
43+
if($scope instanceof ScopeContext) {
44+
switch($scope->getScope()) {
45+
case IManager::SCOPE_ADMIN:
46+
$this->details['scopes'][] = ['scope' => 'admin'];
47+
break;
48+
case IManager::SCOPE_USER:
49+
$this->details['scopes'][] = [
50+
'scope' => 'user',
51+
'uid' => $scope->getScopeId(),
52+
];
53+
break;
54+
default:
55+
continue;
56+
}
57+
}
58+
}
59+
return $this;
60+
}
61+
62+
public function setOperation(?IOperation $operation): LogContext {
63+
if($operation instanceof IOperation) {
64+
$this->details['operation'] = [
65+
'class' => get_class($operation),
66+
'name' => $operation->getDisplayName(),
67+
];
68+
}
69+
return $this;
70+
}
71+
72+
public function setEntity(?IEntity $entity): LogContext {
73+
if($entity instanceof IEntity) {
74+
$this->details['entity'] = [
75+
'class' => get_class($entity),
76+
'name' => $entity->getName(),
77+
];
78+
}
79+
return $this;
80+
}
81+
82+
public function setConfiguration(array $configuration): LogContext {
83+
$this->details['configuration'] = $configuration;
84+
return $this;
85+
}
86+
87+
public function setEventName(string $eventName): LogContext {
88+
$this->details['eventName'] = $eventName;
89+
return $this;
90+
}
91+
92+
public function getDetails(): array {
93+
return $this->details;
94+
}
95+
}

apps/workflowengine/lib/Manager.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCA\WorkflowEngine\Check\UserGroupMembership;
3535
use OCA\WorkflowEngine\Entity\File;
3636
use OCA\WorkflowEngine\Helper\ScopeContext;
37+
use OCA\WorkflowEngine\Service\Logger;
3738
use OCA\WorkflowEngine\Service\RuleMatcher;
3839
use OCP\AppFramework\QueryException;
3940
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -127,7 +128,13 @@ public function __construct(
127128
}
128129

129130
public function getRuleMatcher(): IRuleMatcher {
130-
return new RuleMatcher($this->session, $this->container, $this->l, $this);
131+
return new RuleMatcher(
132+
$this->session,
133+
$this->container,
134+
$this->l,
135+
$this,
136+
$this->container->query(Logger::class)
137+
);
131138
}
132139

133140
public function getAllConfiguredEvents() {

0 commit comments

Comments
 (0)