| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Log; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Log |
| 9 |
* |
| 10 |
* The static facade intended to be the primary way of logging within GiveWP to make life easier. |
| 11 |
* |
| 12 |
* @package Give\Log |
| 13 |
* @since 2.10.0 |
| 14 |
* |
| 15 |
* @note There are two special keywords used in the context that are representing category and source. |
| 16 |
* The default value for the Category is "Core" and for the source is "Give Core" |
| 17 |
* If you want to change the category and/or source, you should provide them as context attributes. |
| 18 |
* Source and category attributes should be written lowercase. |
| 19 |
* |
| 20 |
* @example |
| 21 |
* |
| 22 |
* Log::error( 'Error message', [ |
| 23 |
* 'category' => 'Payment', |
| 24 |
* 'source' => 'Stripe add-on' |
| 25 |
* ] ); |
| 26 |
* |
| 27 |
* @note Use as many contexts attributes as you need. The more the better. |
| 28 |
* |
| 29 |
* @example |
| 30 |
* |
| 31 |
* Log::error( 'Error message', [ |
| 32 |
* 'category' => 'Payment', |
| 33 |
* 'source' => 'Stripe add-on', |
| 34 |
* 'donation_id' => $donationId, |
| 35 |
* 'donor_id' => $donorId |
| 36 |
* ] ); |
| 37 |
* |
| 38 |
* @note You can use an array or object as a context attribute value. |
| 39 |
* |
| 40 |
* @example |
| 41 |
* |
| 42 |
* try { |
| 43 |
* something(); |
| 44 |
* } catch ( Exception $exception ) { |
| 45 |
* Log::error( 'Something went wrong', [ |
| 46 |
* 'exception' => $exception, |
| 47 |
* 'additional_info' => [ |
| 48 |
* 'donation_id' => $donationId |
| 49 |
* ] |
| 50 |
* ] ); |
| 51 |
* } |
| 52 |
* |
| 53 |
* |
| 54 |
* @method static error( string $message, array $context = [] ) |
| 55 |
* @method static warning( string $message, array $context = [] ) |
| 56 |
* @method static notice( string $message, array $context = [] ) |
| 57 |
* @method static success( string $message, array $context = [] ) |
| 58 |
* @method static info( string $message, array $context = [] ) |
| 59 |
* @method static http( string $message, array $context = [] ) |
| 60 |
* @method static spam( string $message, array $context = [] ) |
| 61 |
*/ |
| 62 |
class Log { |
| 63 |
public function __call( $name, $arguments ) { |
| 64 |
list ( $message, $context ) = array_pad( $arguments, 2, null ); |
| 65 |
|
| 66 |
if ( is_array( $context ) ) { |
| 67 |
// Convert context values to string |
| 68 |
$context = array_map( |
| 69 |
function ( $item ) { |
| 70 |
if ( is_array( $item ) || is_object( $item ) ) { |
| 71 |
$item = print_r( $item, true ); |
| 72 |
} |
| 73 |
|
| 74 |
return $item; |
| 75 |
}, |
| 76 |
$context |
| 77 |
); |
| 78 |
|
| 79 |
// Default fields |
| 80 |
$data = array_filter( |
| 81 |
$context, |
| 82 |
function ( $key ) { |
| 83 |
return array_key_exists( $key, LogFactory::getDefaults() ); |
| 84 |
}, |
| 85 |
ARRAY_FILTER_USE_KEY |
| 86 |
); |
| 87 |
|
| 88 |
// Additional context |
| 89 |
$data['context'] = array_diff( |
| 90 |
$context, |
| 91 |
$data |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
// Set message |
| 96 |
if ( ! is_null( $message ) ) { |
| 97 |
$data['message'] = $message; |
| 98 |
} |
| 99 |
|
| 100 |
// Set type |
| 101 |
$data['type'] = $name; |
| 102 |
|
| 103 |
try { |
| 104 |
$log = LogFactory::makeFromArray( $data ); |
| 105 |
$log->save(); |
| 106 |
|
| 107 |
return $log; |
| 108 |
} catch ( Exception $exception ) { |
| 109 |
error_log( $exception->getMessage() ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Static helper for calling the logger methods |
| 115 |
* |
| 116 |
* @since 2.11.1 |
| 117 |
* |
| 118 |
* @param string $name |
| 119 |
* @param array $arguments |
| 120 |
*/ |
| 121 |
public static function __callStatic( $name, $arguments ) { |
| 122 |
/** @var Log $logger */ |
| 123 |
$logger = give( __CLASS__ ); |
| 124 |
|
| 125 |
call_user_func_array( [ $logger, $name ], $arguments ); |
| 126 |
} |
| 127 |
} |
| 128 |
|