Admin
6 years ago
Commands
6 years ago
Db
6 years ago
Ecommerce
6 years ago
Report
6 years ago
Site
6 years ago
TrackingCode
6 years ago
User
6 years ago
views
6 years ago
API.php
6 years ago
Access.php
6 years ago
AjaxTracker.php
6 years ago
Annotations.php
6 years ago
Bootstrap.php
6 years ago
Capabilities.php
6 years ago
Compatibility.php
6 years ago
Email.php
6 years ago
Installer.php
6 years ago
Logger.php
6 years ago
OptOut.php
6 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
6 years ago
Site.php
6 years ago
TrackingCode.php
6 years ago
Uninstaller.php
6 years ago
Updater.php
6 years ago
User.php
6 years ago
Logger.php
93 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 | |
| 20 | public function log( $message ) { |
| 21 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) { |
| 22 | if ( is_array( $message ) || is_object( $message ) ) { |
| 23 | error_log( 'Matomo: ' . print_r( $message, true ) ); |
| 24 | } else { |
| 25 | error_log( 'Matomo: ' . $message ); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public function clear_logged_exceptions() { |
| 31 | delete_option( $this->make_id() ); |
| 32 | } |
| 33 | |
| 34 | private function persist( $key, $message ) { |
| 35 | $id = $this->make_id(); |
| 36 | $logs = $this->get_last_logged_entries(); |
| 37 | $logs[] = array( |
| 38 | 'name' => $key, |
| 39 | 'value' => time(), |
| 40 | 'comment' => $message, |
| 41 | ); |
| 42 | $logs = array_slice( $logs, -6 ); |
| 43 | update_option( $id, $logs ); |
| 44 | } |
| 45 | |
| 46 | private function make_id() { |
| 47 | return Settings::OPTION_PREFIX . 'errorlogs'; |
| 48 | } |
| 49 | |
| 50 | public function get_last_logged_entries() { |
| 51 | $id = $this->make_id(); |
| 52 | $logs = get_option( $id ); |
| 53 | if ( empty( $logs ) ) { |
| 54 | $logs = array(); |
| 55 | } |
| 56 | |
| 57 | // remove any entry older than 1 week |
| 58 | $logs = array_filter( |
| 59 | $logs, |
| 60 | function ( $log ) { |
| 61 | $one_week_seconds = 604800; |
| 62 | return ! empty( $log['value'] ) && is_numeric( $log['value'] ) && ( time() - $log['value'] ) <= $one_week_seconds; |
| 63 | } |
| 64 | ); |
| 65 | return $logs; |
| 66 | } |
| 67 | |
| 68 | public function get_readable_trace( Exception $e ) { |
| 69 | $trace = ''; |
| 70 | if ( $e->getFile() ) { |
| 71 | $trace = basename( $e->getFile() ) . ':' . $e->getLine() . '; '; |
| 72 | } |
| 73 | foreach ( $e->getTrace() as $index => $item ) { |
| 74 | if ( ! empty( $item['file'] ) ) { |
| 75 | $trace .= basename( $item['file'] ) . ':' . $item['line'] . '; '; |
| 76 | } |
| 77 | if ( $index > 8 ) { |
| 78 | continue; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return trim( $trace ); |
| 83 | } |
| 84 | |
| 85 | public function log_exception( $key, Exception $e ) { |
| 86 | $trace = $this->get_readable_trace($e); |
| 87 | $message = $e->getMessage() . ' => ' . $trace; |
| 88 | $this->log( 'Matomo error: ' . $message ); |
| 89 | $this->persist( $key, $message ); |
| 90 | } |
| 91 | |
| 92 | } |
| 93 |