Repository navigation
Expand file tree
/
Copy pathbetterstack-logger.php
More file actions
96 lines (85 loc) · 3.34 KB
/
Copy pathbetterstack-logger.php
File metadata and controls
96 lines (85 loc) · 3.34 KB
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
<?php
/*
Plugin Name: BetterStack Logger
Plugin URI: https://github.com/ikigau/betterstack-logger
Description: Seamlessly integrate with BetterStack to log messages directly from your WordPress site. Enhance your logging capabilities with ease and precision.
Short Description: Seamlessly integrate with BetterStack to log messages directly from your WordPress site.
Tags: betterstack, logger, logging, error, errors, debug, debugging, log, logs, monitoring, monitoring, betterstack logger, betterstack logging, betterstack error logging, betterstack error logger, betterstack debug logging, betterstack debug logger
Version: 1.2.0
Requires at least: 6.0
Requires PHP: 8.0
Tested up to: 6.9.4
Author: Pedro Gaudêncio
Author URI: https://ikigau.com
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
Text Domain: betterstack-logger
*/
if (!defined('WPINC')) {
die;
}
require_once plugin_dir_path(__FILE__) . 'includes/BetterStackLogger.php';
// Load the WooCommerce bridge handler after WooCommerce has initialized.
// BetterStackLogger still lazy-loads this file as a fallback when registering handlers.
add_action('woocommerce_loaded', function () {
if (class_exists('WC_Log_Handler') && interface_exists('WC_Log_Handler_Interface')) {
require_once plugin_dir_path(__FILE__) . 'includes/WCLogHandlerBetterStack.php';
}
});
// Create a global instance of the BetterStackLogger class
global $betterstack_logger;
$betterstack_logger = new BetterStackLogger();
/**
* Backward-compatible helper for custom application logs.
*
* Existing calls like better_error_log('message') continue to work.
*
* @param string $message Log message.
* @param array<string, mixed> $context Optional structured context.
* @param string $level Log level: debug|info|warning|error|critical.
* @return void
*/
if (!function_exists('better_error_log')) {
function better_error_log($message, $context = [], $level = 'error') {
global $betterstack_logger;
if ($betterstack_logger instanceof BetterStackLogger) {
$safe_context = is_array($context) ? $context : [];
$betterstack_logger->log_error($message, $safe_context, (string) $level);
}
}
}
/**
* Shorthand backward-compatible helper for custom application logs.
*
* @param string $message Log message.
* @param array<string, mixed> $context Optional structured context.
* @param string $level Log level: debug|info|warning|error|critical.
* @return void
*/
if (!function_exists('b_log')) {
function b_log($message, $context = [], $level = 'error') {
global $betterstack_logger;
if ($betterstack_logger instanceof BetterStackLogger) {
$safe_context = is_array($context) ? $context : [];
$betterstack_logger->log_error($message, $safe_context, (string) $level);
}
}
}
/**
* Log a WooCommerce-specific structured event.
*
* @param string $event
* @param string $message
* @param array<string, mixed> $context
* @param string $level
* @return void
*/
if (!function_exists('better_wc_log')) {
function better_wc_log($event, $message, $context = [], $level = 'error') {
global $betterstack_logger;
if ($betterstack_logger instanceof BetterStackLogger) {
$safe_context = is_array($context) ? $context : [];
$betterstack_logger->log_woocommerce_event((string) $event, (string) $message, $safe_context, (string) $level);
}
}
}