-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbsp.php
92 lines (68 loc) · 1.89 KB
/
bsp.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
<?php
/**
* Plugin Name: BetterStack for WP and for WooCommerce
* Description: Simple integration for logger WP and BetterStack Logs. const BETTERSTACK_LOGS_SOURCE_TOKEN get here https://logs.betterstack.com/
* Version: 0.3
*/
namespace BetterStackPress;
if ( defined( 'BETTERSTACK_LOGS_SOURCE_TOKEN' ) ) {
add_action( 'shutdown', function () {
$error = error_get_last();
if ( is_null( $error ) ) {
return;
}
if ( $error['type'] != E_ERROR ) {
return;
}
send_to_betterstack( $error );
}, 1 );
add_filter( 'wp_php_error_message', function ($message, $error) {
send_to_betterstack( $error );
return $message;
}, 11, 2 );
}
function send_to_betterstack( $error ) {
$message = explode( 'Stack trace:', $error['message'] );
$data = [
'message' => trim( $message[0] ),
'nested' => [],
];
if ( isset( $message[1] ) ) {
$data['nested']['stack_trace'] = explode( "\n", trim( $message[1] ) );
}
if ( $user_id = get_current_user_id() ) {
$data['nested']['user_id'] = $user_id;
}
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$data['nested']['request'] = $_SERVER['REQUEST_URI'];
}
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
$data['nested']['referer'] = $_SERVER['HTTP_REFERER'];
} else {
$data['nested']['referer'] = 'unknown';
}
if ( isset( $error['type'] ) ) {
$data['nested']['type'] = $error['type'];
}
$json = json_encode( $data );
$result = wp_remote_post( 'https://in.logs.betterstack.com', [
'headers' => [
'Authorization' => 'Bearer ' . BETTERSTACK_LOGS_SOURCE_TOKEN,
'Content-Type' => 'application/json'
],
'body' => $json,
] );
return $result;
}
/**
* simple test for check BetterStack
*
* 1. just run {{siteUrl}}/?test_BetterStackLogsIntegration
* 2. check log https://logs.betterstack.com/
*/
add_action( 'init', function () {
if ( ! isset( $_GET['test_BetterStackLogsIntegration'] ) ) {
return;
}
test_wrong_function();
} );