<?php
use Automattic\Jetpack\Constants;
defined('ABSPATH') || exit;
class WC_Logger implements WC_Logger_Interface
{
protected $handlers;
protected $threshold;
public function __construct($handlers = null, $threshold = null)
{
if (null === $handlers) {
$handlers = apply_filters('woocommerce_register_log_handlers', array());
}
$register_handlers = array();
if (!empty($handlers) && is_array($handlers)) {
foreach ($handlers as $handler) {
$implements = class_implements($handler);
if (is_object($handler) && is_array($implements) && in_array('WC_Log_Handler_Interface', $implements, true)) {
$register_handlers[] = $handler;
} else {
wc_doing_it_wrong(__METHOD__, sprintf(__('The provided handler %1$s does not implement %2$s.', 'woocommerce'), '<code>' . esc_html(is_object($handler) ? get_class($handler) : $handler) . '</code>', '<code>WC_Log_Handler_Interface</code>'), '3.0');
}
}
}
if (null === $threshold) {
$threshold = Constants::get_constant('WC_LOG_THRESHOLD');
if (null !== $threshold && !WC_Log_Levels::is_valid_level($threshold)) {
$threshold = null;
}
}
if (null !== $threshold) {
$threshold = WC_Log_Levels::get_level_severity($threshold);
}
$this->handlers = $register_handlers;
$this->threshold = $threshold;
}
protected function should_handle($level)
{
if (null === $this->threshold) {
return true;
}
return $this->threshold <= WC_Log_Levels::get_level_severity($level);
}
public function add($handle, $message, $level = WC_Log_Levels::NOTICE)
{
$message = apply_filters('woocommerce_logger_add_message', $message, $handle);
$this->log($level, $message, array('source' => $handle, '_legacy' => true));
wc_do_deprecated_action('woocommerce_log_add', array($handle, $message), '3.0', 'This action has been deprecated with no alternative.');
return true;
}
public function log($level, $message, $context = array())
{
if (!WC_Log_Levels::is_valid_level($level)) {
wc_doing_it_wrong(__METHOD__, sprintf(__('%1$s was called with an invalid level "%2$s".', 'woocommerce'), '<code>WC_Logger::log</code>', $level), '3.0');
}
if ($this->should_handle($level)) {
$timestamp = time();
foreach ($this->handlers as $handler) {
$message = apply_filters('woocommerce_logger_log_message', $message, $level, $context, $handler);
if (null !== $message) {
$handler->handle($timestamp, $level, $message, $context);
}
}
}
}
public function emergency($message, $context = array())
{
$this->log(WC_Log_Levels::EMERGENCY, $message, $context);
}
public function alert($message, $context = array())
{
$this->log(WC_Log_Levels::ALERT, $message, $context);
}
public function critical($message, $context = array())
{
$this->log(WC_Log_Levels::CRITICAL, $message, $context);
}
public function error($message, $context = array())
{
$this->log(WC_Log_Levels::ERROR, $message, $context);
}
public function warning($message, $context = array())
{
$this->log(WC_Log_Levels::WARNING, $message, $context);
}
public function notice($message, $context = array())
{
$this->log(WC_Log_Levels::NOTICE, $message, $context);
}
public function info($message, $context = array())
{
$this->log(WC_Log_Levels::INFO, $message, $context);
}
public function debug($message, $context = array())
{
$this->log(WC_Log_Levels::DEBUG, $message, $context);
}
public function clear($source = '')
{
if (!$source) {
return false;
}
foreach ($this->handlers as $handler) {
if (is_callable(array($handler, 'clear'))) {
$handler->clear($source);
}
}
return true;
}
public function clear_expired_logs()
{
$days = absint(apply_filters('woocommerce_logger_days_to_retain_logs', 30));
$timestamp = strtotime("-{$days} days");
foreach ($this->handlers as $handler) {
if (is_callable(array($handler, 'delete_logs_before_timestamp'))) {
$handler->delete_logs_before_timestamp($timestamp);
}
}
}
}