Skip to content

Instantly share code, notes, and snippets.

@shakahl
Forked from troy/send_remote_syslog.php
Created December 29, 2015 22:21
Show Gist options
  • Save shakahl/3d2ab343eb6281c828f0 to your computer and use it in GitHub Desktop.
Save shakahl/3d2ab343eb6281c828f0 to your computer and use it in GitHub Desktop.
Send UDP remote syslog message from PHP (RFC 3164)
# replace PAPERTRAIL_HOSTNAME and PAPERTRAIL_PORT
# see http://help.papertrailapp/ for additional PHP syslog options
function send_remote_syslog($message, $component = "web", $program = "next_big_thing") {
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
foreach(explode("\n", $message) as $line) {
$syslog_message = "<22>" . date('M d H:i:s ') . $program . ' ' . $component . ': ' . $line;
socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, PAPERTRAIL_HOSTNAME, PAPERTRAIL_PORT);
}
socket_close($sock);
}
send_remote_syslog("Test");
# send_remote_syslog("Any log message");
# send_remote_syslog("Something just happened", "other-component");
# send_remote_syslog("Something just happened", "a-background-job-name", "whatever-app-name");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment