Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active December 19, 2017 14:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save coderofsalvation/11325307 to your computer and use it in GitHub Desktop.
Save coderofsalvation/11325307 to your computer and use it in GitHub Desktop.
static php class for syslog-style logging to local syslogdaemon (native php), remote syslog server, or papertrailapp
/*
* PLEASE DO NOT USE THIS CODE, BUT GO TO: https://github.com/coderofsalvation/syslog-flexible
*/
// local, remote and papertrail compatible syslogclass
class Syslog{
public static $hostname = false;
public static $port = 514;
public static $program = "[]";
public static $embedLevel = true;
public static function level2String($level){
// taken from syslog + http:// nl3.php.net/syslog for log levels
switch( $level ){
case LOG_EMERG: return "EMERGENCY"; break; // system is unusable
case LOG_ALERT: return "ALERT"; break; // action must be taken immediately
case LOG_CRIT: return "CRITICAL"; break; // critical conditions
case LOG_ERR: return "ERROR"; break; // error conditions
case LOG_WARNING: return "WARNING"; break; // warning conditions
case LOG_NOTICE: return "NOTICE"; break; // normal, but significant, condition
case LOG_INFO: return "INFO"; break; // informational message
case LOG_DEBUG: return "DEBUG"; break; // debug-level message
}
}
public static function send( $message, $level = LOG_NOTICE, $component = "web" ){
if( self::$embedLevel ) $message = "[".self::level2String($level)."] ".$message;
if( self::$hostname == false ) return syslog( $level, $message );
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$facility = 1; // user level
$pri = ($facility*8)+$level; // multiplying the Facility number by 8 + adding the nume
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
foreach(explode("\n", $message) as $line) {
$syslog_message = "<{$pri}>" . date('M d H:i:s ') . self::$program . ' ' . $component . ': ' . $message;
socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, self::$hostname, self::$port );
}
socket_close($sock);
}
}
@fmbiete
Copy link

fmbiete commented Apr 4, 2015

Hi,
I want to use this in a AGPL project? How is this licensed?
Thanks!!

@richrawlings
Copy link

Lines 31 and 32 are truncated?

@shairozan
Copy link

Lines 31, 32, and 29 are truncated. You may find the rest of what you're looking for in here since it was the source for the class.

@coderofsalvation
Copy link
Author

Hi People, please go here to find the most recent version: https://github.com/coderofsalvation/syslog-flexible

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