Skip to content

Instantly share code, notes, and snippets.

@djeraseit
Forked from troy/send_remote_syslog.php
Created January 28, 2021 08:04
Show Gist options
  • Save djeraseit/eae5b4580cbc43a008567f49e5a409fe to your computer and use it in GitHub Desktop.
Save djeraseit/eae5b4580cbc43a008567f49e5a409fe 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.com/ 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");
?>
@djeraseit
Copy link
Author

logs6.papertrailapp.com:17646
Accepts TLS, TCP (plain text), and UDP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment