2424 */
2525namespace OC \Diagnostics ;
2626
27+ use OC \Log ;
28+ use OC \SystemConfig ;
29+ use OCP \Diagnostics \IEvent ;
2730use OCP \Diagnostics \IEventLogger ;
31+ use Psr \Log \LoggerInterface ;
2832
2933class EventLogger implements IEventLogger {
30- /**
31- * @var \OC\Diagnostics\Event[]
32- */
34+
35+ /** @var Event[] */
3336 private $ events = [];
34-
37+
38+ /** @var SystemConfig */
39+ private $ config ;
40+
41+ /** @var LoggerInterface */
42+ private $ logger ;
43+
44+ /** @var Log */
45+ private $ internalLogger ;
46+
3547 /**
3648 * @var bool - Module needs to be activated by some app
3749 */
3850 private $ activated = false ;
3951
52+ public function __construct (SystemConfig $ config , LoggerInterface $ logger , Log $ internalLogger ) {
53+ $ this ->config = $ config ;
54+ $ this ->logger = $ logger ;
55+ $ this ->internalLogger = $ internalLogger ;
56+
57+ if ($ this ->isLoggingActivated ()) {
58+ $ this ->activate ();
59+ }
60+ }
61+
62+ public function isLoggingActivated (): bool {
63+ if ($ this ->config ->getValue ('debug ' , false )) {
64+ return true ;
65+ }
66+
67+ $ isDebugLevel = $ this ->internalLogger ->getLogLevel ([]) === Log::DEBUG ;
68+ $ systemValue = (bool )$ this ->config ->getValue ('diagnostics.logging ' , false );
69+ return $ systemValue && $ isDebugLevel ;
70+ }
71+
4072 /**
4173 * @inheritdoc
4274 */
43- public function start ($ id , $ description ) {
75+ public function start ($ id , $ description = '' ) {
4476 if ($ this ->activated ) {
4577 $ this ->events [$ id ] = new Event ($ id , $ description , microtime (true ));
78+ $ this ->writeLog ($ this ->events [$ id ]);
4679 }
4780 }
4881
@@ -53,6 +86,7 @@ public function end($id) {
5386 if ($ this ->activated && isset ($ this ->events [$ id ])) {
5487 $ timing = $ this ->events [$ id ];
5588 $ timing ->end (microtime (true ));
89+ $ this ->writeLog ($ timing );
5690 }
5791 }
5892
@@ -63,6 +97,7 @@ public function log($id, $description, $start, $end) {
6397 if ($ this ->activated ) {
6498 $ this ->events [$ id ] = new Event ($ id , $ description , $ start );
6599 $ this ->events [$ id ]->end ($ end );
100+ $ this ->writeLog ($ this ->events [$ id ]);
66101 }
67102 }
68103
@@ -72,11 +107,29 @@ public function log($id, $description, $start, $end) {
72107 public function getEvents () {
73108 return $ this ->events ;
74109 }
75-
110+
76111 /**
77112 * @inheritdoc
78113 */
79114 public function activate () {
80115 $ this ->activated = true ;
81116 }
117+
118+ private function writeLog (IEvent $ event ) {
119+ if ($ this ->activated ) {
120+ if ($ event ->getEnd () === null ) {
121+ return ;
122+ }
123+ $ duration = $ event ->getDuration ();
124+ $ timeInMs = round ($ duration * 1000 , 4 );
125+
126+ $ loggingMinimum = (int )$ this ->config ->getValue ('diagnostics.logging.threshold ' , 0 );
127+ if ($ loggingMinimum > 0 && $ timeInMs < $ loggingMinimum ) {
128+ return ;
129+ }
130+
131+ $ message = microtime () . ' - ' . $ event ->getId () . ': ' . $ timeInMs . ' ( ' . $ event ->getDescription () . ') ' ;
132+ $ this ->logger ->debug ($ message , ['app ' => 'diagnostics ' ]);
133+ }
134+ }
82135}
0 commit comments