1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace GV; |
3
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
6
|
|
|
die(); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The \GV\WP_Action_Logger implementation. |
11
|
|
|
* |
12
|
|
|
* Uses the old logging stuff for now. |
13
|
|
|
*/ |
14
|
|
|
class WP_Action_Logger extends Logger { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Logs with an arbitrary level using `do_action` and our |
18
|
|
|
* old action handlers. |
19
|
|
|
* |
20
|
|
|
* $context['data'] will be passed to the action. |
21
|
|
|
* |
22
|
|
|
* @param mixed $level The log level. |
23
|
|
|
* @param string $message The message to log. |
24
|
|
|
* @param array $context The context. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
1 |
|
protected function log( $level, $message, $context ) { |
29
|
|
|
|
30
|
1 |
|
$backtrace = debug_backtrace(); |
31
|
1 |
|
$location = $this->interpolate( "{class}{type}{function}", $backtrace[2] ); |
|
|
|
|
32
|
|
|
|
33
|
1 |
|
$message = $this->interpolate( "[$level, $location] $message", $context ); |
34
|
|
|
|
35
|
|
|
switch ( $level ): |
36
|
1 |
|
case LogLevel::EMERGENCY: |
37
|
1 |
|
case LogLevel::ALERT: |
38
|
1 |
|
case LogLevel::CRITICAL: |
39
|
1 |
|
case LogLevel::ERROR: |
40
|
1 |
|
$action = 'error'; |
41
|
1 |
|
break; |
42
|
1 |
|
case LogLevel::WARNING: |
43
|
1 |
|
case LogLevel::NOTICE: |
44
|
1 |
|
case LogLevel::INFO: |
45
|
|
|
case LogLevel::DEBUG: |
46
|
1 |
|
$action = 'debug'; |
47
|
1 |
|
break; |
48
|
|
|
endswitch; |
49
|
|
|
|
50
|
1 |
|
if ( defined( 'DOING_GRAVITYVIEW_TESTS' ) ) { |
51
|
|
|
/** Let's make this testable! */ |
52
|
1 |
|
do_action( |
53
|
1 |
|
sprintf( 'gravityview_log_%s_test', $action ), |
|
|
|
|
54
|
1 |
|
$this->interpolate( $message, $context ), |
55
|
1 |
|
empty( $context['data'] ) ? array() : $context['data'] |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
do_action( |
60
|
1 |
|
sprintf( 'gravityview_log_%s', $action ), |
61
|
1 |
|
$this->interpolate( $message, $context ), |
62
|
1 |
|
empty( $context['data'] ) ? array() : $context['data'] |
63
|
|
|
); |
64
|
1 |
|
} |
65
|
|
|
} |
66
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.