base.php
94 lines
| 1 | <?php |
| 2 | namespace Elementor\Core\Logger\Loggers; |
| 3 | |
| 4 | use Elementor\Core\Logger\Items\Base as Log_Item; |
| 5 | use Elementor\Core\Logger\Items\Log_Item_Interface as Log_Item_Interface; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly |
| 9 | } |
| 10 | |
| 11 | abstract class Base implements Logger_Interface { |
| 12 | |
| 13 | abstract protected function save_log( Log_Item_Interface $item ); |
| 14 | |
| 15 | /** |
| 16 | * @return Log_Item_Interface[] |
| 17 | */ |
| 18 | abstract public function get_log(); |
| 19 | |
| 20 | public function log( $item, $type = self::LEVEL_INFO, $args = [] ) { |
| 21 | if ( ! $item instanceof Log_Item ) { |
| 22 | $item = $this->create_item( $item, $type, $args ); |
| 23 | } |
| 24 | $this->save_log( $item ); |
| 25 | } |
| 26 | |
| 27 | public function info( $message, $args = [] ) { |
| 28 | $this->log( $message, self::LEVEL_INFO, $args ); |
| 29 | } |
| 30 | |
| 31 | public function notice( $message, $args = [] ) { |
| 32 | $this->log( $message, self::LEVEL_NOTICE, $args ); |
| 33 | } |
| 34 | |
| 35 | public function warning( $message, $args = [] ) { |
| 36 | $this->log( $message, self::LEVEL_WARNING, $args ); |
| 37 | } |
| 38 | |
| 39 | public function error( $message, $args = [] ) { |
| 40 | $this->log( $message, self::LEVEL_ERROR, $args ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param string $message |
| 45 | * @param string $type |
| 46 | * @param array $args |
| 47 | * |
| 48 | * @return Log_Item_Interface |
| 49 | */ |
| 50 | private function create_item( $message, $type, $args = [] ) { |
| 51 | $args['message'] = $message; |
| 52 | $args['type'] = $type; |
| 53 | |
| 54 | $item = new Log_Item( $args ); |
| 55 | |
| 56 | return $item; |
| 57 | } |
| 58 | |
| 59 | public function get_formatted_log_entries( $max_entries, $table = true ) { |
| 60 | $entries = $this->get_log(); |
| 61 | |
| 62 | if ( empty( $entries ) ) { |
| 63 | return [ |
| 64 | 'All' => [ |
| 65 | 'total_count' => 0, |
| 66 | 'count' => 0, |
| 67 | 'entries' => '', |
| 68 | ], |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | $sorted_entries = []; |
| 73 | $open_tag = $table ? '<tr><td>' : ''; |
| 74 | $close_tab = $table ? '</td></tr>' : PHP_EOL; |
| 75 | |
| 76 | $format = $table ? 'html' : 'raw'; |
| 77 | |
| 78 | foreach ( $entries as $entry ) { |
| 79 | /** @var Log_Item $entry */ |
| 80 | $sorted_entries[ $entry->get_name() ][] = $open_tag . $entry->format( $format ) . $close_tab; |
| 81 | } |
| 82 | |
| 83 | $formatted_entries = []; |
| 84 | foreach ( $sorted_entries as $key => $sorted_entry ) { |
| 85 | $formatted_entries[ $key ]['total_count'] = count( $sorted_entry ); |
| 86 | $formatted_entries[ $key ]['count'] = count( $sorted_entry ); |
| 87 | $sorted_entry = array_slice( $sorted_entry, -$max_entries ); |
| 88 | $formatted_entries[ $key ]['count'] = count( $sorted_entry ); |
| 89 | $formatted_entries[ $key ]['entries'] = implode( $sorted_entry ); |
| 90 | } |
| 91 | return $formatted_entries; |
| 92 | } |
| 93 | } |
| 94 |