Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

fmbiete commented Apr 4, 2015

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

@richrawlings

This comment has been minimized.

Copy link

richrawlings commented Aug 5, 2015

Lines 31 and 32 are truncated?

@shairozan

This comment has been minimized.

Copy link

shairozan commented Aug 6, 2015

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

This comment has been minimized.

Copy link
Owner Author

coderofsalvation commented Aug 21, 2015

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
You can’t perform that action at this time.