Admin
5 years ago
Commands
5 years ago
Helpers
5 years ago
Migrations
5 years ago
ValueObjects
5 years ago
Assets.php
5 years ago
Log.php
5 years ago
LogFactory.php
5 years ago
LogModel.php
5 years ago
LogRepository.php
5 years ago
LogServiceProvider.php
5 years ago
Log.php
75 lines
| 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 | * @method static error( string $message, array $context = [] ) |
| 16 | * @method static warning( string $message, array $context = [] ) |
| 17 | * @method static notice( string $message, array $context = [] ) |
| 18 | * @method static success( string $message, array $context = [] ) |
| 19 | * @method static info( string $message, array $context = [] ) |
| 20 | * @method static http( string $message, array $context = [] ) |
| 21 | * @method static spam( string $message, array $context = [] ) |
| 22 | */ |
| 23 | class Log { |
| 24 | /** |
| 25 | * @param string $type |
| 26 | * @param array $args |
| 27 | */ |
| 28 | public static function __callStatic( $type, $args ) { |
| 29 | list ( $message, $context ) = array_pad( $args, 2, null ); |
| 30 | |
| 31 | if ( is_array( $context ) ) { |
| 32 | // Convert context values to string |
| 33 | $context = array_map( |
| 34 | function ( $item ) { |
| 35 | if ( is_array( $item ) || is_object( $item ) ) { |
| 36 | $item = print_r( $item, true ); |
| 37 | } |
| 38 | |
| 39 | return $item; |
| 40 | }, |
| 41 | $context |
| 42 | ); |
| 43 | |
| 44 | // Default fields |
| 45 | $data = array_filter( |
| 46 | $context, |
| 47 | function ( $key ) { |
| 48 | return array_key_exists( $key, LogFactory::getDefaults() ); |
| 49 | }, |
| 50 | ARRAY_FILTER_USE_KEY |
| 51 | ); |
| 52 | |
| 53 | // Additional context |
| 54 | $data['context'] = array_diff( |
| 55 | $context, |
| 56 | $data |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | // Set message |
| 61 | if ( ! is_null( $message ) ) { |
| 62 | $data['message'] = $message; |
| 63 | } |
| 64 | |
| 65 | // Set type |
| 66 | $data['type'] = $type; |
| 67 | |
| 68 | try { |
| 69 | LogFactory::makeFromArray( $data )->save(); |
| 70 | } catch ( Exception $exception ) { |
| 71 | error_log( $exception->getMessage() ); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 |