pippinsplugins / wp-logging
一个提供通用日志系统的 WordPress 类。此类旨在用于需要持久跟踪事件、错误、操作等的大系统。
Requires
- php: >=5.2.0
This package is not auto-updated.
Last update: 2024-09-15 05:54:59 UTC
README
一个提供通用日志系统的 WordPress 类。此类旨在用于需要持久跟踪事件、错误、操作等的大系统。
此类最初是为 Easy Digital Downloads 开发的,用于跟踪付款创建、文件下载、购买错误等。
日志类型
日志条目被设计为分离到“类型”。默认情况下,类有“错误”和“事件”日志类型。您可以通过修改 log_types() 方法中的数组来将这些类型更改为任何您想要的。
private function log_types() { $terms = array( 'error', 'event' ); return apply_filters( 'wp_log_types', $terms ); }
您还可以使用过滤器注册额外的日志类型。
function pw_add_log_types( $types ) { $types[] = 'registration'; return $types; } add_filter( 'wp_log_types', 'pw_add_log_types' );
记录日志条目
记录日志条目有两种方式,一种快捷简单,另一种更复杂,但提供了更多控制。
使用 add()
$log_entry = WP_Logging::add( $title = '', $message = '', $parent = 0, $type = null );
- $title (字符串) - 日志条目标题
- $message - 日志条目消息 (字符串)
- $parent (整数) - 您想将此日志条目连接到的帖子对象 ID,如果有的话
- $type (字符串) - 给此日志条目赋予的类型分类。必须是 log_types() 中注册的类型之一。这是可选的。
一个示例日志条目创建可能看起来像这样
$title = 'Payment Error'; $message = 'There was an error processing the payment. Here are details of the transaction: (details shown here)'; $parent = 46; // This might be the ID of a payment post type item we want this log item connected to $type = 'error'; WP_Logging::add( $title, $message, $parent, $type );
或者,没有类型
$title = 'Payment Error'; $message = 'There was an error processing the payment. Here are details of the transaction: (details shown here)'; $parent = 46; // This might be the ID of a payment post type item we want this log item connected to WP_Logging::add( $title, $message, $parent );
使用 insert_log()
$log_entry = WP_logging::insert_log( $log_data = array(), $log_meta = array() );
此方法要求通过数组传递所有日志数据。一个数组用于主要帖子对象数据,另一个数组用于与日志条目一起记录的附加日志元数据。
$log_data
数组接受可以传递给 wp_insert_post() 的所有参数,还有一个额外的参数为 log_type
。
$log_data
数组期望键/值对,用于记录与日志条目一起记录的任何元数据。元数据存储在正常的帖子元数据中,尽管所有元数据都以 _wp_log_
为前缀。
使用 insert_log()
创建日志条目可能看起来像这样
$log_data = array( 'post_title' => 'Payment Error', 'post_content' => 'There was an error processing the payment. Here are details of the transaction: (details shown here)', 'post_parent' => 46, // This might be the ID of a payment post type item we want this log item connected to 'log_type' => 'error' ); $log_meta = array( 'customer_ip' => 'xxx.xx.xx.xx' // the customer's IP address 'user_id' => get_current_user_id() // the ID number of the currently logged-in user ); $log_entry = WP_Logging::insert_log( $log_data, $log_meta );
获取日志条目
有两种方法可以检索使用此日志类存储的条目
WP_Logging::get_logs( $object_id = 0, $type = null, $paged = null )
WP_Logging::get_connected_logs( $args = array() )
get_logs()
是一个简单的方法,允许您快速检索与特定帖子对象连接的日志。例如,要检索与帖子 ID 57 相关的错误日志,您可以这样做
$logs = WP_Logging::get_logs( 57, 'error' );
这将检索与 ID 57 相关的前 10 个条目。注意,第三个参数是 $paged
。这允许您传递一个页面号(如果您正在构建用于显示日志的管理界面),然后 WP_Logging 将调整检索的日志以匹配页面号。
如果您需要更细粒度的控制,您将想要使用 get_connected_logs()
。此方法接受一个包含单个键/值对的数组作为唯一参数。$args 数组接受可以传递给 wp_insert_post() 的所有参数,还有一个额外的参数为 log_type
。
这里是使用 get_connected_logs()
的一个示例
$args = array( 'post_parent' => 57, 'posts_per_page'=> 10, 'paged' => get_query_var( 'paged' ), 'log_type' => 'error' ); $logs = WP_Logging::get_connected_logs( $args );
如果您想检索所有日志条目并忽略分页,您可以这样做
$args = array( 'post_parent' => 57, 'posts_per_page'=> -1, 'log_type' => 'error' ); $logs = WP_Logging::get_connected_logs( $args );
get_logs()
和 get_connected_logs()
都将返回一个典型的帖子对象数组,就像 get_posts() 一样
获取日志条目数量
《get_log_count()》方法允许您检索数据库中存储的日志条目总数。它可以检索特定帖子对象ID的特定类型的日志,还可以通过传递可选的元选项来仅计算具有存储的元值的日志条目数量。
该方法如下所示
WP_Logging:: get_log_count( $object_id = 0, $type = null, $meta_query = null )
要检索附加到帖子57的《error》日志总数,您可以这样做
$count = WP_Logging::get_log_count( 57, 'error' );
要检索附加到帖子对象ID 57的日志总数(无论类型如何),您可以这样做
$count = WP_Logging::get_log_count( 57 );
第三个参数是传递元查询数组。该数组应与传递给WP_Query的元查询相同。例如,要检索具有匹配特定IP地址的用户IP的error日志条目计数,您可以这样做
$meta_query = array( array( 'key' => '_wp_log_customer_ip', // the meta key 'value' => 'xxx.xx.xx.xx' // the IP address to retrieve logs for ) ); $count = WP_Logging::get_log_count( 57, 'error', $meta_query );
修剪日志
要修剪旧日志,您首先需要将修剪条件设置为true,然后设置一个cron作业来执行修剪。建议您将cron作业设置为每小时一次,这样您就可以随时跟踪修剪日志。每天运行一次并删除100条日志(默认数量)意味着许多网站实际上永远不会跟上日志修剪。
function activate_pruning( $should_we_prune ){ return true; } // rapid_activate_pruning add_filter( 'wp_logging_should_we_prune', 'activate_pruning', 10 ); $scheduled = wp_next_scheduled( 'wp_logging_prune_routine' ); if ( $scheduled == false ){ wp_schedule_event( time(), 'hourly', 'wp_logging_prune_routine' ); }
默认的时间周期是修剪超过2周的日志。要更改它,请使用wp_logging_prune_when
。如果我们想修剪超过1个月的旧日志。
function change_prune_time( $time ){ return '1 month ago'; } add_filter( 'wp_logging_prune_when', 'change_prune_time' );
修剪查询通过get_posts
运行,并且您可以通过wp_logging_prune_query_args
过滤器过滤数组中的任何参数。默认情况下,每次运行修剪例程时,我们修剪100条日志。如果您服务器可以处理负载,您当然可以增加这个数字。
日志被设置为绕过WordPress回收站系统。如果您想让日志进入WordPress回收站,那么您需要过滤wp_logging_force_delete_log
并返回false。
function hit_trash(){ return false; } add_filter( 'wp_logging_force_delete_log', 'hit_trash' );
如果您让日志进入WordPress回收站,那么您需要编写自己的例程来清除回收站,这样日志就不会为您累积。