-
Notifications
You must be signed in to change notification settings - Fork 0
/
ramphor-logger.php
130 lines (115 loc) · 3.98 KB
/
ramphor-logger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Plugin Name: Ramphor Logger
* Plugin URI: https://github.com/ramphor/logger
* Author: Ramphor Premium
* Author URI: https://puleeno.com
* Version: 0.2.0
* Description: The logger library for themes, plugins in WordPress and tracking all errors of PHP/WordPress
* Tags: logger, monitor, errors
*/
use Ramphor\Logger\Logger;
use Monolog\Handler\SlackHandler;
if (!class_exists(\WP_CLI::class)) {
$composer_file = sprintf( '%s/vendor/autoload.php', dirname( __FILE__ ) );
if ( file_exists( $composer_file ) ) {
require_once $composer_file;
}
if ( ! class_exists( Logger::class ) ) {
return error_log( sprintf( 'The class %s is not found to register custom logger', Logger::class ) );
}
function load_ramphor_logger_firstly() {
$path = substr(
__FILE__,
strlen( __FILE__ ) - 33
);
// Set me at first index
if ( $plugins = get_option( 'active_plugins' ) ) {
if ( $key = array_search( $path, $plugins ) ) {
array_splice( $plugins, $key, 1 );
array_unshift( $plugins, $path );
update_option( 'active_plugins', $plugins );
}
}
}
add_action( 'activated_plugin', 'load_ramphor_logger_firstly', 100 );
function ramphor_logger_exception_trigger( $e ) {
$message = sprintf(
"%s\n%s",
$e->getMessage(),
$e->getTraceAsString()
);
$logger = Logger::instance();
$logger->get()->error( $message );
_e( 'Your website has errors. Please contact to webadmin or your developer to get more informations', 'ramphor_logger' );
}
set_exception_handler( 'ramphor_logger_exception_trigger' );
if (!defined('RAMPHOR_LOGGER_DISABLE_ERROR_TRIGGER') || constant('RAMPHOR_LOGGER_DISABLE_ERROR_TRIGGER') === false) {
function ramphor_logger_error_trigger( $errno, $errstr, $errfile, $errline, $errcontext = array() ) {
$ignore_wp_core = apply_filters(
'ramphor_logger_ignore_wordpress_core_warning',
true
);
$errdir = preg_replace(
'/([^\/]+)(.+)?/',
'$1',
str_replace( ABSPATH, '', $errfile )
);
if ( $ignore_wp_core && in_array( $errdir, array( 'wp-includes', 'wp-admin' ) ) ) {
return;
}
ob_start();
debug_print_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
$backtrace = ob_get_clean();
$message = sprintf( "%s in %s line %s\n%s", $errstr, $errfile, $errline, $backtrace );
$logger = Logger::instance();
$user_error_log_level = get_option( 'user_error_log_level', 'warning' );
if ( is_callable( array( $logger->getLogger(), $user_error_log_level ) ) ) {
$logger->$user_error_log_level( $message );
} else {
$logger->warning( $message );
}
}
set_error_handler( 'ramphor_logger_error_trigger' );
}
function ramphor_logger_register_logger( $logger, $id ) {
if ( ! defined( 'RAMPHOR_LOGGER_SLACK_HANDLER_ENABLE' ) ) {
return;
}
$token = defined( 'RAMPHOR_LOGGER_SLACK_TOKEN' ) ? constant( 'RAMPHOR_LOGGER_SLACK_TOKEN' ) : '';
$channel = defined( 'RAMPHOR_LOGGER_SLACK_CHANNEL' ) ? constant( 'RAMPHOR_LOGGER_SLACK_CHANNEL' ) : '';
$botname = defined( 'RAMPHOR_LOGGER_SLACK_BOT_NAME' ) ? constant( 'RAMPHOR_LOGGER_SLACK_BOT_NAME' ) : '';
if ( isset( $token, $channel ) ) {
$slackHandler = new SlackHandler( $token, $channel, $botname );
$monitor_level = defined('RAMPHOR_LOGGER_SLACK_LOG_LEVEL')
? RAMPHOR_LOGGER_SLACK_LOG_LEVEL
: get_option( 'ramphor_logger_slack_at_level', $logger::NOTICE );
$log_level = $logger::NOTICE;
if ( in_array(
$monitor_level,
array(
$logger::DEBUG,
$logger::INFO,
$logger::NOTICE,
$logger::WARNING,
$logger::ERROR,
$logger::CRITICAL,
$logger::ALERT,
$logger::EMERGENCY,
)
) ) {
$log_level = $monitor_level;
}
$slackHandler->setLevel(
apply_filters(
'ramphor_logger_slack_handler_log_level',
$log_level
)
);
$logger->pushHandler( $slackHandler );
}
}
add_action( 'ramphor_logger_register_logger', 'ramphor_logger_register_logger', 10, 2 );
// Init the Ramphor Logger instance
add_action( 'loaded_plugins', array( Logger::class, 'instance' ) );
}