Admin
5 years ago
Commands
6 years ago
Db
6 years ago
Ecommerce
6 years ago
Report
6 years ago
Site
5 years ago
TrackingCode
5 years ago
User
5 years ago
views
6 years ago
API.php
5 years ago
Access.php
6 years ago
AjaxTracker.php
5 years ago
Annotations.php
6 years ago
Bootstrap.php
6 years ago
Capabilities.php
6 years ago
Compatibility.php
6 years ago
Email.php
5 years ago
Installer.php
6 years ago
Logger.php
5 years ago
OptOut.php
5 years ago
Paths.php
6 years ago
PrivacyBadge.php
6 years ago
Referral.php
6 years ago
Roles.php
6 years ago
ScheduledTasks.php
6 years ago
Settings.php
5 years ago
Site.php
6 years ago
TrackingCode.php
5 years ago
Uninstaller.php
6 years ago
Updater.php
6 years ago
User.php
6 years ago
Logger.php
114 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo; |
| 11 | |
| 12 | use Exception; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // if accessed directly |
| 16 | } |
| 17 | |
| 18 | class Logger { |
| 19 | const LEVEL_NONE = 99; |
| 20 | const LEVEL_DEBUG = 1; |
| 21 | const LEVEL_INFO = 3; |
| 22 | |
| 23 | private function get_log_level() |
| 24 | { |
| 25 | if ( defined('MATOMO_DEBUG')) { |
| 26 | if (MATOMO_DEBUG) { |
| 27 | return self::LEVEL_DEBUG; |
| 28 | } |
| 29 | return self::LEVEL_NONE; |
| 30 | } |
| 31 | |
| 32 | return self::LEVEL_INFO; |
| 33 | } |
| 34 | |
| 35 | public function log( $message , $mode = 3) { |
| 36 | $log_level = $this->get_log_level(); |
| 37 | |
| 38 | if ($log_level > $mode) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) { |
| 43 | if ( is_array( $message ) || is_object( $message ) ) { |
| 44 | error_log( 'Matomo: ' . print_r( $message, true ) ); |
| 45 | } else { |
| 46 | error_log( 'Matomo: ' . $message ); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function clear_logged_exceptions() { |
| 52 | delete_option( $this->make_id() ); |
| 53 | } |
| 54 | |
| 55 | private function persist( $key, $message ) { |
| 56 | $id = $this->make_id(); |
| 57 | $logs = $this->get_last_logged_entries(); |
| 58 | $logs[] = array( |
| 59 | 'name' => $key, |
| 60 | 'value' => time(), |
| 61 | 'comment' => $message, |
| 62 | ); |
| 63 | $logs = array_slice( $logs, -6 ); |
| 64 | update_option( $id, $logs ); |
| 65 | } |
| 66 | |
| 67 | private function make_id() { |
| 68 | return Settings::OPTION_PREFIX . 'errorlogs'; |
| 69 | } |
| 70 | |
| 71 | public function get_last_logged_entries() { |
| 72 | $id = $this->make_id(); |
| 73 | $logs = get_option( $id ); |
| 74 | if ( empty( $logs ) ) { |
| 75 | $logs = array(); |
| 76 | } |
| 77 | |
| 78 | // remove any entry older than 1 week |
| 79 | $logs = array_filter( |
| 80 | $logs, |
| 81 | function ( $log ) { |
| 82 | $one_week_seconds = 604800; |
| 83 | return ! empty( $log['value'] ) && is_numeric( $log['value'] ) && ( time() - $log['value'] ) <= $one_week_seconds; |
| 84 | } |
| 85 | ); |
| 86 | return $logs; |
| 87 | } |
| 88 | |
| 89 | public function get_readable_trace( Exception $e ) { |
| 90 | $trace = ''; |
| 91 | if ( $e->getFile() ) { |
| 92 | $trace = basename( $e->getFile() ) . ':' . $e->getLine() . '; '; |
| 93 | } |
| 94 | foreach ( $e->getTrace() as $index => $item ) { |
| 95 | if ( ! empty( $item['file'] ) ) { |
| 96 | $trace .= basename( $item['file'] ) . ':' . $item['line'] . '; '; |
| 97 | } |
| 98 | if ( $index > 8 ) { |
| 99 | continue; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return trim( $trace ); |
| 104 | } |
| 105 | |
| 106 | public function log_exception( $key, Exception $e ) { |
| 107 | $trace = $this->get_readable_trace($e); |
| 108 | $message = $e->getMessage() . ' => ' . $trace; |
| 109 | $this->log( 'Matomo error: ' . $message ); |
| 110 | $this->persist( $key, $message ); |
| 111 | } |
| 112 | |
| 113 | } |
| 114 |