Created
February 5, 2016 12:40
-
-
Save bonny/a4604442e4ea9260068b to your computer and use it in GitHub Desktop.
Example how to post all logged events in WordPress recent events plugin Simple History to Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Prio 100 so we run late and give other filters chance to run | |
add_filter( "simple_history/log_insert_context", "on_log_insert_context_slackit", 100, 2 ); | |
function on_log_insert_context_slackit( $context, $data ) { | |
$slack_webhook_url = '<your incoming webhook url>'; | |
$remote_addr = empty( $context["_server_remote_addr"] ) ? "" : $context["_server_remote_addr"]; | |
$level = empty( $data["level"] ) ? "" : $data["level"]; | |
$user_login = empty( $context["_user_login"] ) ? "" : $context["_user_login"]; | |
$message = SimpleLogger::interpolate( $data["message"], $context ); | |
$initiator = empty( $data["initiator"] ) ? "" : $data["initiator"]; | |
$server_http_user_agent = empty( $context["server_http_user_agent"] ) ? "" : $context["server_http_user_agent"]; | |
$log_message = sprintf( | |
'%3$s %4$s', | |
$remote_addr, // 1 | |
$level, // 2 | |
$user_login, // 3 | |
$message // 4 | |
); | |
$log_message = trim( $log_message ); | |
$color = ""; | |
switch ( $level ) { | |
case "debug": | |
$color = "#CEF6D8"; | |
break; | |
case "info": | |
$color = "white"; | |
break; | |
case "notice": | |
$color = "#FFFFE0"; | |
break; | |
case "warning": | |
$color = "#F7D358"; | |
break; | |
case "error": | |
$color = "#F79F81"; | |
break; | |
case "critical": | |
$color = "#FA5858"; | |
break; | |
case "alert": | |
$color = "#c74545"; | |
break; | |
case "emergency": | |
$color = "#610B0B"; | |
break; | |
} | |
$fields = array( | |
array( | |
"title" => "Message", | |
"value" => $log_message | |
), | |
array( | |
"title" => "Site name", | |
"value" => get_bloginfo( 'name' ) . " - " . get_bloginfo("description"), | |
"short" => true, | |
), | |
array( | |
"title" => "Site url", | |
"value" => home_url(), | |
"short" => true, | |
), | |
/*array( | |
"title" => "User", | |
"value" => $user_login, | |
"short" => true | |
),*/ | |
array( | |
"title" => "Level", | |
"value" => $level, | |
"short" => true | |
), | |
array( | |
"title" => "Initiator", | |
"value" => $initiator, | |
"short" => true | |
), | |
array( | |
"title" => "Remote addr", | |
"value" => $remote_addr, | |
"short" => true | |
), | |
); | |
if ( $server_http_user_agent ) { | |
$fields[] = array( | |
"title" => "User agent", | |
"value" => $server_http_user_agent, | |
"short" => false | |
); | |
} | |
$author_icon = ""; | |
if ( function_exists("get_site_icon_url") ) { | |
$author_icon = get_site_icon_url(512); | |
} | |
$arr_slack_data = array( | |
"username" => "WordPress Simple History", | |
"icon_url" => "https://simple-history.com/assets/uploads/2014/12/icon-5121-548c6e42_site_icon-32x32.png", | |
"text" => 'An event was logged', | |
"attachments" => array( | |
array( | |
"thumb_url" => $author_icon, | |
// "author_name" => get_bloginfo( 'name' ) . " " . home_url(), | |
// "author_link" => home_url(), | |
// "author_icon" => $author_icon, | |
// An optional value that can either be one of good, warning, danger, or any hex color code (eg. #439FE0). This value is used to color the border along the left side of the message attachment. | |
"color" => $color, | |
//"title" => $log_message, | |
//"text" => $log_message, | |
"fields" => $fields, | |
) // attachments | |
) | |
); | |
$post_args = array( | |
'blocking' => false, | |
'timeout' => 0.01, | |
'body' => json_encode( $arr_slack_data ) | |
); | |
wp_remote_post( $slack_webhook_url, $post_args ); | |
return $context; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment