ZP-765 Code formatting. Released under the Affero GNU General Public License (AGPL) version 3.

parent 139f1c2a
/etc/kronos/z-push/config.php
\ No newline at end of file
...@@ -93,22 +93,27 @@ ...@@ -93,22 +93,27 @@
* ones, e.g. setting to LOGLEVEL_DEBUG will also output LOGLEVEL_FATAL, LOGLEVEL_ERROR, * ones, e.g. setting to LOGLEVEL_DEBUG will also output LOGLEVEL_FATAL, LOGLEVEL_ERROR,
* LOGLEVEL_WARN and LOGLEVEL_INFO level entries. * LOGLEVEL_WARN and LOGLEVEL_INFO level entries.
*/ */
define('LOGFILEDIR', '/var/log/z-push/');
define('LOGFILE', LOGFILEDIR . 'z-push.log');
define('LOGERRORFILE', LOGFILEDIR . 'z-push-error.log');
define('LOGLEVEL', LOGLEVEL_WBXML); define('LOGLEVEL', LOGLEVEL_WBXML);
define('LOGAUTHFAIL', false); define('LOGAUTHFAIL', false);
// Either filelog or syslog or a custom log class in core/log/logclass
define('LOGBACKEND', 'filelog');
// To save e.g. WBXML data only for selected users, add the usernames to the array // To save e.g. WBXML data only for selected users, add the usernames to the array
// The data will be saved into a dedicated file per user in the LOGFILEDIR // The data will be saved into a dedicated file per user in the LOGFILEDIR
// Users have to be encapusulated in quotes, several users are comma separated, like: // Users have to be encapusulated in quotes, several users are comma separated, like:
// $specialLogUsers = array('info@domain.com', 'myusername'); // $specialLogUsers = array('info@domain.com', 'myusername');
define('LOGUSERLEVEL', LOGLEVEL_DEVICEID); define('LOGUSERLEVEL', LOGLEVEL_DEVICEID);
$specialLogUsers = array(); $specialLogUsers = array();
// Either filelog or syslog or a custom log class in core/log/logclass
define('LOGBACKEND', 'filelog');
/**
* Filelog settings
*/
define('LOGFILEDIR', '/var/log/z-push/');
define('LOGFILE', LOGFILEDIR . 'z-push.log');
define('LOGERRORFILE', LOGFILEDIR . 'z-push-error.log');
/**
* Syslog settings
*/
// false will log to local syslog, otherwise put the remote syslog IP here // false will log to local syslog, otherwise put the remote syslog IP here
define('LOG_SYSLOG_HOST', false); define('LOG_SYSLOG_HOST', false);
// Syslog port // Syslog port
......
...@@ -101,10 +101,10 @@ class ZLog { ...@@ -101,10 +101,10 @@ class ZLog {
self::$lastLogs[$loglevel] = $message; self::$lastLogs[$loglevel] = $message;
try{ try {
self::getLogger()->Log($loglevel, $message); self::getLogger()->Log($loglevel, $message);
} }
catch(\Exception $e) { catch (\Exception $e) {
//@TODO How should we handle logging error ? //@TODO How should we handle logging error ?
// Ignore any error. // Ignore any error.
} }
......
...@@ -220,31 +220,38 @@ class ZPush { ...@@ -220,31 +220,38 @@ class ZPush {
else else
define('REAL_BASE_PATH', BASE_PATH); define('REAL_BASE_PATH', BASE_PATH);
if(!defined('LOGBACKEND')){ if (!defined('LOGBACKEND')) {
define('LOGBACKEND', 'filelog'); define('LOGBACKEND', 'filelog');
} }
if(LOGBACKEND == 'syslog') { if (LOGBACKEND == 'syslog') {
if(!defined('LOG_SYSLOG_FACILITY')) {
if (!defined('LOG_SYSLOG_FACILITY')) {
define('LOG_SYSLOG_FACILITY', LOG_LOCAL0); define('LOG_SYSLOG_FACILITY', LOG_LOCAL0);
} }
if(!defined('LOG_SYSLOG_HOST')){
if (!defined('LOG_SYSLOG_HOST')) {
define('LOG_SYSLOG_HOST', false); define('LOG_SYSLOG_HOST', false);
} }
if(!defined('LOG_SYSLOG_PORT')){
if (!defined('LOG_SYSLOG_PORT')) {
define('LOG_SYSLOG_PORT', 514); define('LOG_SYSLOG_PORT', 514);
} }
if(!defined('LOG_SYSLOG_PROGRAM')){
if (!defined('LOG_SYSLOG_PROGRAM')) {
define('LOG_SYSLOG_PROGRAM', 'z-push'); define('LOG_SYSLOG_PROGRAM', 'z-push');
} }
if(!is_numeric(LOG_SYSLOG_PORT)){
if (!is_numeric(LOG_SYSLOG_PORT)) {
throw new FatalMisconfigurationException("The LOG_SYSLOG_PORT must a be a number."); throw new FatalMisconfigurationException("The LOG_SYSLOG_PORT must a be a number.");
} }
if(LOG_SYSLOG_HOST && LOG_SYSLOG_PORT <= 0){
if (LOG_SYSLOG_HOST && LOG_SYSLOG_PORT <= 0) {
throw new FatalMisconfigurationException("LOG_SYSLOG_HOST is defined but the LOG_SYSLOG_PORT does not seem to be valid."); throw new FatalMisconfigurationException("LOG_SYSLOG_HOST is defined but the LOG_SYSLOG_PORT does not seem to be valid.");
} }
} }
elseif(LOGBACKEND == 'filelog'){ elseif (LOGBACKEND == 'filelog') {
if (!defined('LOGFILEDIR')) if (!defined('LOGFILEDIR'))
throw new FatalMisconfigurationException("The LOGFILEDIR is not configured. Check if the config.php file is in place."); throw new FatalMisconfigurationException("The LOGFILEDIR is not configured. Check if the config.php file is in place.");
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* Project : Z-Push * Project : Z-Push
* Descr : Logging functionalities * Descr : Logging functionalities
* *
* Created : 11.13.2015 * Created : 13.11.2015
* *
* Copyright 2007 - 2015 Zarafa Deutschland GmbH * Copyright 2007 - 2015 Zarafa Deutschland GmbH
* *
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
* *
* Consult LICENSE file for details * Consult LICENSE file for details
************************************************/ ************************************************/
class FileLog extends Log{ class FileLog extends Log {
/** /**
* @var string|bool * @var string|bool
...@@ -67,7 +67,8 @@ class FileLog extends Log{ ...@@ -67,7 +67,8 @@ class FileLog extends Log{
$this->log_to_user_file = $value; $this->log_to_user_file = $value;
} }
public function __construct(){} public function __construct() {
}
/** /**
* Returns the string to be logged * Returns the string to be logged
...@@ -79,7 +80,7 @@ class FileLog extends Log{ ...@@ -79,7 +80,7 @@ class FileLog extends Log{
* @return string * @return string
*/ */
public function BuildLogString($loglevel, $message) { public function BuildLogString($loglevel, $message) {
$log = Utils::GetFormattedTime() . $this->GetPidstr() . $this->GetLogLevelString($loglevel, $loglevel >= LOGLEVEL_INFO) .' '. $this->GetUser(); $log = Utils::GetFormattedTime() . $this->GetPidstr() . $this->GetLogLevelString($loglevel, $loglevel >= LOGLEVEL_INFO) . ' ' . $this->GetUser();
if ($loglevel >= LOGLEVEL_DEVICEID) { if ($loglevel >= LOGLEVEL_DEVICEID) {
$log .= $this->GetDevid(); $log .= $this->GetDevid();
} }
...@@ -91,7 +92,7 @@ class FileLog extends Log{ ...@@ -91,7 +92,7 @@ class FileLog extends Log{
// Implementation of Log // Implementation of Log
// //
protected function Write($loglevel, $message){ protected function Write($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . "\n"; $data = $this->buildLogString($loglevel, $message) . "\n";
@file_put_contents(LOGFILE, $data, FILE_APPEND); @file_put_contents(LOGFILE, $data, FILE_APPEND);
...@@ -100,15 +101,15 @@ class FileLog extends Log{ ...@@ -100,15 +101,15 @@ class FileLog extends Log{
} }
} }
public function WriteForUser($loglevel, $message){ public function WriteForUser($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . "\n"; $data = $this->buildLogString($loglevel, $message) . "\n";
@file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND); @file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND);
} }
protected function afterLog($loglevel, $message){ protected function afterLog($loglevel, $message) {
if (($loglevel & LOGLEVEL_FATAL) || ($loglevel & LOGLEVEL_ERROR)) { if (($loglevel & LOGLEVEL_FATAL) || ($loglevel & LOGLEVEL_ERROR)) {
$data = $this->buildLogString($loglevel, $message) . "\n"; $data = $this->buildLogString($loglevel, $message) . "\n";
@file_put_contents(LOGERRORFILE, $data, FILE_APPEND); @file_put_contents(LOGERRORFILE, $data, FILE_APPEND);
} }
} }
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* Project : Z-Push * Project : Z-Push
* Descr : Logging functionalities * Descr : Logging functionalities
* *
* Created : 11.13.2015 * Created : 13.11.2015
* *
* Copyright 2007 - 2015 Zarafa Deutschland GmbH * Copyright 2007 - 2015 Zarafa Deutschland GmbH
* *
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
* *
* Consult LICENSE file for details * Consult LICENSE file for details
************************************************/ ************************************************/
abstract class Log{ abstract class Log {
/** /**
* @var string * @var string
...@@ -73,8 +73,9 @@ abstract class Log{ ...@@ -73,8 +73,9 @@ abstract class Log{
public function GetUser() { public function GetUser() {
return $this->user; return $this->user;
} }
/** /**
* @param string$value * @param string $value
* *
* @access public * @access public
*/ */
...@@ -89,6 +90,7 @@ abstract class Log{ ...@@ -89,6 +90,7 @@ abstract class Log{
public function GetDevid() { public function GetDevid() {
return $this->devid; return $this->devid;
} }
/** /**
* @param string $value * @param string $value
* *
...@@ -105,6 +107,7 @@ abstract class Log{ ...@@ -105,6 +107,7 @@ abstract class Log{
public function GetPidstr() { public function GetPidstr() {
return $this->pidstr; return $this->pidstr;
} }
/** /**
* @param string $value * @param string $value
* *
...@@ -134,6 +137,7 @@ abstract class Log{ ...@@ -134,6 +137,7 @@ abstract class Log{
} }
return false; return false;
} }
/** /**
* @access public * @access public
* @return array * @return array
...@@ -141,6 +145,7 @@ abstract class Log{ ...@@ -141,6 +145,7 @@ abstract class Log{
public function GetSpecialLogUsers() { public function GetSpecialLogUsers() {
return $this->specialLogUsers; return $this->specialLogUsers;
} }
/** /**
* @param array $value * @param array $value
* *
...@@ -150,7 +155,8 @@ abstract class Log{ ...@@ -150,7 +155,8 @@ abstract class Log{
$this->specialLogUsers = $value; $this->specialLogUsers = $value;
} }
public function __construct() {} public function __construct() {
}
/** /**
* @param int $loglevel * @param int $loglevel
...@@ -161,7 +167,7 @@ abstract class Log{ ...@@ -161,7 +167,7 @@ abstract class Log{
$this->Write($loglevel, $message); $this->Write($loglevel, $message);
} }
if ($loglevel <= LOGUSERLEVEL && $this->HasSpecialLogUsers()) { if ($loglevel <= LOGUSERLEVEL && $this->HasSpecialLogUsers()) {
if(RequestProcessor::isUserAuthenticated() && $this->IsUserInSpecialLogUsers(Request::GetAuthUser())) { if (RequestProcessor::isUserAuthenticated() && $this->IsUserInSpecialLogUsers(Request::GetAuthUser())) {
// something was logged before the user was authenticated, write this to the log // something was logged before the user was authenticated, write this to the log
if (!empty($this->unauthMessageCache)) { if (!empty($this->unauthMessageCache)) {
foreach ($this->unauthMessageCache as $authcache) { foreach ($this->unauthMessageCache as $authcache) {
...@@ -170,9 +176,8 @@ abstract class Log{ ...@@ -170,9 +176,8 @@ abstract class Log{
self::$unAuthCache = array(); self::$unAuthCache = array();
} }
$this->WriteForUser($loglevel, $message); $this->WriteForUser($loglevel, $message);
} } else {
else { $this->unauthMessageCache[] = array($loglevel, $message);
$this->unauthMessageCache[] = [$loglevel, $message];
} }
} }
...@@ -182,21 +187,24 @@ abstract class Log{ ...@@ -182,21 +187,24 @@ abstract class Log{
/** /**
* This function is used as an event for log implementer. * This function is used as an event for log implementer.
*/ */
protected function afterLog($loglevel, $message) {} protected function afterLog($loglevel, $message) {
}
/** /**
* Returns the string representation of the given $loglevel. * Returns the string representation of the given $loglevel.
* String can be padded * String can be padded
* *
* @param int $loglevel one of the LOGLEVELs * @param int $loglevel one of the LOGLEVELs
* @param boolean $pad * @param boolean $pad
* *
* @access protected * @access protected
* @return string * @return string
*/ */
protected function GetLogLevelString($loglevel, $pad = false) { protected function GetLogLevelString($loglevel, $pad = false) {
if ($pad) $s = " "; if ($pad)
else $s = ""; $s = " ";
else
$s = "";
switch($loglevel) { switch($loglevel) {
case LOGLEVEL_OFF: return ""; break; case LOGLEVEL_OFF: return ""; break;
case LOGLEVEL_FATAL: return "[FATAL]"; break; case LOGLEVEL_FATAL: return "[FATAL]"; break;
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* Project : Z-Push * Project : Z-Push
* Descr : Logging functionalities * Descr : Logging functionalities
* *
* Created : 11.13.2015 * Created : 13.11.2015
* *
* Copyright 2007 - 2015 Zarafa Deutschland GmbH * Copyright 2007 - 2015 Zarafa Deutschland GmbH
* *
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
* *
* Consult LICENSE file for details * Consult LICENSE file for details
************************************************/ ************************************************/
class Syslog extends Log{ class Syslog extends Log {
protected $program_name = ''; protected $program_name = '';
...@@ -55,6 +55,7 @@ class Syslog extends Log{ ...@@ -55,6 +55,7 @@ class Syslog extends Log{
public function GetProgramName() { public function GetProgramName() {
return $this->program_name; return $this->program_name;
} }
/** /**
* @param string $value * @param string $value
* *
...@@ -71,6 +72,7 @@ class Syslog extends Log{ ...@@ -71,6 +72,7 @@ class Syslog extends Log{
public function GetHost() { public function GetHost() {
return $this->host; return $this->host;
} }
/** /**
* @param string $value * @param string $value
* *
...@@ -87,6 +89,7 @@ class Syslog extends Log{ ...@@ -87,6 +89,7 @@ class Syslog extends Log{
public function GetPort() { public function GetPort() {
return $this->port; return $this->port;
} }
/** /**
* @param int $value * @param int $value
* *
...@@ -101,9 +104,9 @@ class Syslog extends Log{ ...@@ -101,9 +104,9 @@ class Syslog extends Log{
public function __construct($program_name = null, $host = null, $port = null) { public function __construct($program_name = null, $host = null, $port = null) {
parent::__construct(); parent::__construct();
if(is_null($program_name)) $program_name = LOG_SYSLOG_PROGRAM; if (is_null($program_name)) $program_name = LOG_SYSLOG_PROGRAM;
if(is_null($host)) $host = LOG_SYSLOG_HOST; if (is_null($host)) $host = LOG_SYSLOG_HOST;
if(is_null($port)) $port = LOG_SYSLOG_PORT; if (is_null($port)) $port = LOG_SYSLOG_PORT;
$this->SetProgramName($program_name); $this->SetProgramName($program_name);
$this->SetHost($host); $this->SetHost($host);
...@@ -123,22 +126,21 @@ class Syslog extends Log{ ...@@ -123,22 +126,21 @@ class Syslog extends Log{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// Shift the "syslog.php" entry. // Shift the "syslog.php" entry.
array_shift($backtrace); array_shift($backtrace);
foreach($backtrace as $trace) { foreach ($backtrace as $trace) {
if (!isset($trace['file'])) { if (!isset($trace['file'])) {
continue; continue;
} }
if (strpos($trace['file'], REAL_BASE_PATH . 'backend/') !== false) { if (strpos($trace['file'], REAL_BASE_PATH . 'backend/') !== false) {
preg_match('/\/backend\/([a-zA-Z]*)/', $trace['file'], $match); preg_match('/\/backend\/([a-zA-Z]*)/', $trace['file'], $match);
if (isset($match[1])) { if (isset($match[1])) {
return $this->GetProgramName().'/'.$match[1]; return $this->GetProgramName() . '/' . $match[1];
} }
} } elseif (basename($trace['file'], '.php') != 'zlog') {
elseif (basename($trace['file'], '.php') != 'zlog') { return $this->GetProgramName() . '/core';
return $this->GetProgramName().'/core';
} }
} }
return $this->GetProgramName().'/core'; return $this->GetProgramName() . '/core';
} }
/** /**
...@@ -149,15 +151,15 @@ class Syslog extends Log{ ...@@ -149,15 +151,15 @@ class Syslog extends Log{
* @return int One of many LOG_* syslog level. * @return int One of many LOG_* syslog level.
*/ */
protected function GetZpushLogLevelToSyslogLogLevel($loglevel) { protected function GetZpushLogLevelToSyslogLogLevel($loglevel) {
switch($loglevel) { switch ($loglevel) {
case LOGLEVEL_FATAL: return LOG_ALERT; break; case LOGLEVEL_FATAL: return LOG_ALERT; break;
case LOGLEVEL_ERROR: return LOG_ERR; break; case LOGLEVEL_ERROR: return LOG_ERR; break;
case LOGLEVEL_WARN: return LOG_WARNING; break; case LOGLEVEL_WARN: return LOG_WARNING; break;
case LOGLEVEL_INFO: return LOG_INFO; break; case LOGLEVEL_INFO: return LOG_INFO; break;
case LOGLEVEL_DEBUG: return LOG_DEBUG; break; case LOGLEVEL_DEBUG: return LOG_DEBUG; break;
case LOGLEVEL_WBXML: return LOG_DEBUG; break; case LOGLEVEL_WBXML: return LOG_DEBUG; break;
case LOGLEVEL_DEVICEID: return LOG_DEBUG; break; case LOGLEVEL_DEVICEID: return LOG_DEBUG; break;
case LOGLEVEL_WBXMLSTACK: return LOG_DEBUG; break; case LOGLEVEL_WBXMLSTACK: return LOG_DEBUG; break;
} }
return null; return null;
} }
...@@ -174,7 +176,7 @@ class Syslog extends Log{ ...@@ -174,7 +176,7 @@ class Syslog extends Log{
public function BuildLogString($loglevel, $message) { public function BuildLogString($loglevel, $message) {
$log = $this->GetLogLevelString($loglevel); // Never pad syslog log because syslog log are usually read with a software. $log = $this->GetLogLevelString($loglevel); // Never pad syslog log because syslog log are usually read with a software.
$log .= $this->GetUser(); $log .= $this->GetUser();
if($loglevel >= LOGLEVEL_DEVICEID) { if ($loglevel >= LOGLEVEL_DEVICEID) {
$log .= $this->GetDevid(); $log .= $this->GetDevid();
} }
$log .= ' ' . $message; $log .= ' ' . $message;
...@@ -190,15 +192,14 @@ class Syslog extends Log{ ...@@ -190,15 +192,14 @@ class Syslog extends Log{
if ($this->GetHost() && $this->GetPort()) { if ($this->GetHost() && $this->GetPort()) {
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$facility = 1; // user level $facility = 1; // user level
$pri = ($facility*8) + $loglevel; // multiplying the Facility number by 8 + adding the level $pri = ($facility * 8) + $loglevel; // multiplying the Facility number by 8 + adding the level
$data = $this->buildLogString($loglevel, $message); $data = $this->buildLogString($loglevel, $message);
if (strlen(trim($data)) > 0) { if (strlen(trim($data)) > 0) {
$syslog_message = "<{$pri}>" . date('M d H:i:s ') . '['.$this->GetProgramName() . ']: ' . $data; $syslog_message = "<{$pri}>" . date('M d H:i:s ') . '[' . $this->GetProgramName() . ']: ' . $data;
socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, $this->GetHost(), $this->GetPort()); socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, $this->GetHost(), $this->GetPort());
} }
socket_close($sock); socket_close($sock);
} } else {
else {
openlog($this->GenerateProgramName(), LOG_PID, LOG_SYSLOG_FACILITY); openlog($this->GenerateProgramName(), LOG_PID, LOG_SYSLOG_FACILITY);
syslog( syslog(
$this->GetZpushLogLevelToSyslogLogLevel($loglevel), $this->GetZpushLogLevelToSyslogLogLevel($loglevel),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment