-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL-Log.php
596 lines (549 loc) · 19.8 KB
/
XBRL-Log.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
/**
* Implements the a logging class.
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2018 Lyquidity Solutions Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use XBRL\Formulas\Exceptions\FormulasException;
if ( ! class_exists( "\\Log", true ) )
{
/**
* Load the Log class
*/
require_once dirname( __FILE__ ) . "/../log/Log.php";
/**
* Load the event_log handler implementation
*/
require_once dirname( __FILE__ ) . "/../log/log/error-log.php";
}
/**
* This a singleton class used to provide a common logging facility for all XBRL class instances.
*/
class XBRL_Log
{
/**
* A reference to this singleton instance
* @var XBRL_Log
*/
private static $instance;
/**
* The log instance to use
* @var Log
*/
private $log;
/**
* Flag holding the instance validation warning state. Will be true if there has been at least one warning
* @var bool
*/
private $instanceValidationWarning = false;
/**
* Flag holding a conformance issue warning state. Will be true if there has been at least one warning
* @var bool
*/
private $conformanceIssueWarning = false;
/**
* Flag holding a business rules violation warning state. Will be true if there has been at least one warning
* @var bool
*/
private $businessRulesViolationeWarning = false;
/**
* Get an instance of the global singleton
* @return XBRL_Log
*/
public static function getInstance()
{
if ( is_null( self::$instance ) )
{
self::$instance = new self();
self::$instance->createLog( 'error_log', PEAR_LOG_TYPE_SYSTEM, 'xbrl_log',
array(
'lineFormat' => '[%{priority}] %{message}',
), PEAR_LOG_DEBUG
);
}
return self::$instance;
}
/**
* Attach an observer to the log
* @param \Log_observer $observer
* @return boolean
*/
public function attach( &$observer )
{
return $this->log->attach( $observer );
}
/**
* Detch the observer from the log
* @param \Log_observer $observer
* @return boolean
*/
public function detach( &$observer )
{
return $this->log->detach( $observer );
}
/**
* Set the log reporting priority
* @param int $priority
*/
public function setPriority( $priority = PEAR_LOG_ALL )
{
$this->log->setPriority( $priority );
}
/**
* Set a mask
* @param int $mask
*/
public function setMask( $mask = PEAR_LOG_ALL )
{
return $this->log->setMask( $mask );
}
/**
* This creates a specific type of log instance
* @param string $handler The type of Log handler to construct
* @param string $name The name of the log resource to which the events
* will be logged. Defaults to an empty string.
* @param string $ident An identification string that will be included in
* all log events logged by this handler. This value
* defaults to an empty string and can be changed at
* runtime using the ``setIdent()`` method.
* @param array $conf Associative array of key-value pairs that are
* used to specify any handler-specific settings.
* @param int $level Log messages up to and including this level.
* This value defaults to ``PEAR_LOG_DEBUG``.
* See `Log Levels`_ and `Log Level Masks`_.
* @return void
*/
public function createLog( $handler, $name, $ident, $conf = null, $level = null )
{
$this->log = Log::singleton( $handler, $name, $ident, $conf, $level );
}
/**
* If you know what you are doing and want to create a custom log,
* perhaps with custom handler, or perhaps you want a composite log
* that directs logs to multiple locations you set it here.
* @param Log $log The logging instance to use
* @return void
*/
public function setLog( $log )
{
$this->log = $log;
}
/**
* A convenience function for logging a emergency event. It will log a
* message at the PEAR_LOG_EMERG log level.
*
* PEAR_LOG_EMERG
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function emerg( $message )
{
if ( ! $this->log ) return;
return $this->log->emerg( $message );
}
/**
* A convenience function for logging an alert event. It will log a
* message at the PEAR_LOG_ALERT log level.
*
* PEAR_LOG_ALERT
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function alert( $message )
{
if ( ! $this->log ) return;
return $this->log->alert( $message );
}
/**
* A convenience function for logging a critical event. It will log a
* message at the PEAR_LOG_CRIT log level.
*
* PEAR_LOG_CRIT
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function crit( $message )
{
if ( ! $this->log ) return;
return $this->log->crit( $message );
}
/**
* A convenience function for logging a error event. It will log a
* message at the PEAR_LOG_ERR log level.
*
* PEAR_LOG_ERR
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function err( $message )
{
$ex = new Exception();
echo $ex->getTraceAsString();
if ( ! $this->log ) return;
return $this->log->err( $message );
}
/**
* A convenience function for logging a warning event. It will log a
* message at the PEAR_LOG_WARNING log level.
*
* PEAR_LOG_WARNING
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function warning( $message )
{
if ( ! $this->log ) return;
return $this->log->warning( $message );
}
/**
* A convenience function for logging a notice event. It will log a
* message at the PEAR_LOG_NOTICE log level.
*
* PEAR_LOG_NOTICE
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function notice( $message )
{
if ( ! $this->log ) return;
return $this->log->notice( $message );
}
/**
* A convenience function for logging a information event. It will log a
* message at the PEAR_LOG_INFO log level.
*
* PEAR_LOG_INFO
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function info( $message = "" )
{
if ( ! $this->log ) return;
return $this->log->info( $message );
}
/**
* A convenience function for logging a debug event. It will log a
* message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_DEBUG
*
* @param mixed $message String or object containing the message to log.
* @return boolean True if the message was successfully logged.
*/
public function debug( $message )
{
if ( ! $this->log ) return;
return $this->log->debug( $message );
}
/**
* A convenience function for logging a event about an XBRL dimensions specification conformance issue.
* It will log a message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function dimension_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->conformanceIssueWarning = true;
$msg = sprintf( "[dimension] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
return $this->log->warning( $msg );
}
/**
* A convenience function for logging a event about an XBRL taxonony specification conformance issue.
* It will log a message at the PEAR_LOG_WARNING log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function taxonomy_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->conformanceIssueWarning = true;
$msg = sprintf( "[taxonomy] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
return $this->log->warning( $msg );
}
/**
* A convenience function for logging a event about an XBRL business rules violation issue.
* It will log a message at the PEAR_LOG_WARNING log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The rules topic reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function business_rules_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->businessRulesViolationeWarning = true;
$msg = sprintf( "[business rules] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
return $this->log->warning( $msg );
}
/**
* A convenience function for logging a event about an XBRL formula specification conformance issue.
* It will log a message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function formula_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->conformanceIssueWarning = true;
$errorMessage = sprintf( "[formula] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
$this->log->warning( $errorMessage );
if ( isset( $source['error'] ) )
{
throw FormulasException::withType( $source['error'], "formula", $errorMessage );
}
return $errorMessage;
}
/**
* A convenience function for logging a event about an XBRL formula specification conformance issue.
* It will log a message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function consistencyassertion_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->conformanceIssueWarning = true;
$errorMessage = $this->log->warning( sprintf( "[consistency assertion] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
if ( isset( $source['error'] ) )
{
throw FormulasException::withType( $source['error'], "consistency-assertion", $errorMessage );
}
return $errorMessage;
}
/**
* A convenience function for logging a event about an XBRL formula specification conformance issue.
* It will log a message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function valueassertion_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->conformanceIssueWarning = true;
$errorMessage = $this->log->warning( sprintf( "[value assertion] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
if ( isset( $source['error'] ) )
{
throw FormulasException::withType( $source['error'], "value-assertion", $errorMessage );
}
return $errorMessage;
}
/**
* A convenience function for logging a event about an XBRL formula specification conformance issue.
* It will log a message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function existenceassertion_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->conformanceIssueWarning = true;
$errorMessage = sprintf( "[existence assertion] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
$this->log->warning( $errorMessage );
if ( isset( $source['error'] ) )
{
throw FormulasException::withType( $source['error'], "existence-assertion", $errorMessage );
}
return $errorMessage;
}
/**
* A convenience function for logging a formula evaluation result event.
* It will log a message at the PEAR_LOG_INFO log level.
*
* PEAR_LOG_INFO
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, link base, etc.
* @return boolean True if the message was successfully logged.
*/
public function formula_evaluation( $section, $message, $source )
{
if ( ! $this->log ) return;
$message = "$section $message";
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_INFO, 'message' => $message, 'source' => $source ) );
$this->log->info( $message );
return $message;
}
/**
* A convenience function for logging an event about an XBRL instance specification conformance issue.
* It will log a message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_WARNING
*
* @param string $section The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, segment, period, etc.
* @return boolean True if the message was successfully logged.
*/
public function instance_validation( $section, $message, $source )
{
if ( ! $this->log ) return;
$this->instanceValidationWarning = true;
// Concatenate the key and the value
$msg = sprintf( "[instance] $message (Section %s - %s)", $section, $this->arrayToDescription( $source ) );
$this->log->_announce( array( 'section' => $section, 'priority' => PEAR_LOG_WARNING, 'message' => $message, 'source' => $source ) );
return $this->log->warning( $msg );
}
/**
* Convert an array of context information about the source of an log instance to a string
*
* @param array $source
*/
public function arrayToDescription( $source )
{
array_walk( $source, function( &$value, $key ) { $value = "$key: $value"; } );
return implode( ', ', $source );
}
/**
* Returns the current instance validation warning state
* @return boolean
*/
public function hasInstanceValidationWarning()
{
return $this->instanceValidationWarning;
}
/**
* Resets the current instance validation warning state
* @return boolean
*/
public function resetInstanceValidationWarning()
{
$this->instanceValidationWarning = false;
}
/**
* Returns the current instance validation warning state
* @return boolean
*/
public function hasConformanceIssueWarning()
{
return $this->conformanceIssueWarning || $this->hasInstanceValidationWarning();
}
/**
* Returns the current instance validation warning state
* @return boolean
*/
public function hasBusinessRulesViolationWarning()
{
return $this->businessRulesViolationeWarning;
}
/**
* Resets the current instance validation warning state
* @return boolean
*/
public function resetConformanceIssueWarning()
{
$this->resetInstanceValidationWarning();
$this->conformanceIssueWarning = false;
}
/**
* Resets the current business rules violation warning state
* @return boolean
*/
public function resetBusinessRulesViolationWarning()
{
$this->businessRulesViolationeWarning = false;
}
/**
* A convenience function for logging an event about an XBRL conformance test.
* It will log a message at the PEAR_LOG_DEBUG log level.
*
* PEAR_LOG_WARNING
*
* @param string $testid The specification section reference
* @param string $message String or object containing the message to log.
* @param array $source An array containing details about the source such as the element id, segment, period, etc.
* @return boolean True if the message was successfully logged.
*/
public function conformance_issue( $testid, $message, $source )
{
if ( ! $this->log ) return;
$this->conformanceIssueWarning = true;
// Concatenate the key and the value
return $this->log->warning( sprintf( "[instance] $message (Section %s - %s)", $testid, $this->arrayToDescription( $source ) ) );
}
/**
* Set log output for console and error_log
* @return XBRL_Log
*/
public function debugLog()
{
$logConsole = Log::singleton( 'console', '', 'console',
array(
'lineFormat' => '%{timestamp} [%{priority}] %{message}',
'timeFormat' => '%Y-%m-%d %H:%M:%S',
)
);
$logError = Log::singleton( 'error_log', PEAR_LOG_TYPE_SYSTEM, 'error_log',
array(
'lineFormat' => '[%{priority}] %{message}',
)
);
$logComposite = Log::singleton( 'composite' );
$logComposite->addChild( $logConsole );
$logComposite->addChild( $logError );
$this->setLog( $logComposite );
}
}