Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Send UDP remote syslog message from PHP (RFC 3164)
# see 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);
# 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");

kbond commented Feb 1, 2013

change date('M j H:i:s ') with date('M d H:i:s ') to ensure works correctly on days 01-09


troy commented Feb 1, 2013

@kbond: Changed. Thanks!


troy commented Feb 25, 2013

Updated to handle newlines by transmitting them as multiple messages.

thnx, turned into a static class + support for local php syslog() function:

jmcbee commented Dec 9, 2014

what the meaning of <22>? Is that the facility level?

Is it possible to max the number of socket connections? Any PHP config to lookout for?


troy commented Sep 5, 2015

@fbm-static: Yes, that's a numeric code for the facility and severity. This example uses a single static value for both, and thus a static number, but it could also be calculated according to

@tenaciousRas: Not that I've heard of from users. This is emitting a UDP packet, which is typically 1ms or less, so you'd need tens of thousands of concurrent requests happening at exactly the same time (and thus probably hundreds of thousands or millions of typical concurrent sessions) in order to consume the ~60k ephemeral ports. Nothing special required that I know of.

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