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 @@
* ones, e.g. setting to LOGLEVEL_DEBUG will also output LOGLEVEL_FATAL, LOGLEVEL_ERROR,
* 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('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
// 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:
// $specialLogUsers = array('info@domain.com', 'myusername');
define('LOGUSERLEVEL', LOGLEVEL_DEVICEID);
$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
define('LOG_SYSLOG_HOST', false);
// Syslog port
......
......@@ -101,10 +101,10 @@ class ZLog {
self::$lastLogs[$loglevel] = $message;
try{
try {
self::getLogger()->Log($loglevel, $message);
}
catch(\Exception $e) {
catch (\Exception $e) {
//@TODO How should we handle logging error ?
// Ignore any error.
}
......
......@@ -220,31 +220,38 @@ class ZPush {
else
define('REAL_BASE_PATH', BASE_PATH);
if(!defined('LOGBACKEND')){
if (!defined('LOGBACKEND')) {
define('LOGBACKEND', 'filelog');
}
if(LOGBACKEND == 'syslog') {
if(!defined('LOG_SYSLOG_FACILITY')) {
if (LOGBACKEND == 'syslog') {
if (!defined('LOG_SYSLOG_FACILITY')) {
define('LOG_SYSLOG_FACILITY', LOG_LOCAL0);
}
if(!defined('LOG_SYSLOG_HOST')){
if (!defined('LOG_SYSLOG_HOST')) {
define('LOG_SYSLOG_HOST', false);
}
if(!defined('LOG_SYSLOG_PORT')){
if (!defined('LOG_SYSLOG_PORT')) {
define('LOG_SYSLOG_PORT', 514);
}
if(!defined('LOG_SYSLOG_PROGRAM')){
if (!defined('LOG_SYSLOG_PROGRAM')) {
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.");
}
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.");
}
}
elseif(LOGBACKEND == 'filelog'){
elseif (LOGBACKEND == 'filelog') {
if (!defined('LOGFILEDIR'))
throw new FatalMisconfigurationException("The LOGFILEDIR is not configured. Check if the config.php file is in place.");
......
......@@ -4,7 +4,7 @@
* Project : Z-Push
* Descr : Logging functionalities
*
* Created : 11.13.2015
* Created : 13.11.2015
*
* Copyright 2007 - 2015 Zarafa Deutschland GmbH
*
......@@ -40,7 +40,7 @@
*
* Consult LICENSE file for details
************************************************/
class FileLog extends Log{
class FileLog extends Log {
/**
* @var string|bool
......@@ -67,7 +67,8 @@ class FileLog extends Log{
$this->log_to_user_file = $value;
}
public function __construct(){}
public function __construct() {
}
/**
* Returns the string to be logged
......@@ -79,7 +80,7 @@ class FileLog extends Log{
* @return string
*/
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) {
$log .= $this->GetDevid();
}
......@@ -91,7 +92,7 @@ class FileLog extends Log{
// Implementation of Log
//
protected function Write($loglevel, $message){
protected function Write($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . "\n";
@file_put_contents(LOGFILE, $data, FILE_APPEND);
......@@ -100,12 +101,12 @@ class FileLog extends Log{
}
}
public function WriteForUser($loglevel, $message){
public function WriteForUser($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . "\n";
@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)) {
$data = $this->buildLogString($loglevel, $message) . "\n";
@file_put_contents(LOGERRORFILE, $data, FILE_APPEND);
......
......@@ -4,7 +4,7 @@
* Project : Z-Push
* Descr : Logging functionalities
*
* Created : 11.13.2015
* Created : 13.11.2015
*
* Copyright 2007 - 2015 Zarafa Deutschland GmbH
*
......@@ -40,7 +40,7 @@
*
* Consult LICENSE file for details
************************************************/
abstract class Log{
abstract class Log {
/**
* @var string
......@@ -73,8 +73,9 @@ abstract class Log{
public function GetUser() {
return $this->user;
}
/**
* @param string$value
* @param string $value
*
* @access public
*/
......@@ -89,6 +90,7 @@ abstract class Log{
public function GetDevid() {
return $this->devid;
}
/**
* @param string $value
*
......@@ -105,6 +107,7 @@ abstract class Log{
public function GetPidstr() {
return $this->pidstr;
}
/**
* @param string $value
*
......@@ -134,6 +137,7 @@ abstract class Log{
}
return false;
}
/**
* @access public
* @return array
......@@ -141,6 +145,7 @@ abstract class Log{
public function GetSpecialLogUsers() {
return $this->specialLogUsers;
}
/**
* @param array $value
*
......@@ -150,7 +155,8 @@ abstract class Log{
$this->specialLogUsers = $value;
}
public function __construct() {}
public function __construct() {
}
/**
* @param int $loglevel
......@@ -161,7 +167,7 @@ abstract class Log{
$this->Write($loglevel, $message);
}
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
if (!empty($this->unauthMessageCache)) {
foreach ($this->unauthMessageCache as $authcache) {
......@@ -170,9 +176,8 @@ abstract class Log{
self::$unAuthCache = array();
}
$this->WriteForUser($loglevel, $message);
}
else {
$this->unauthMessageCache[] = [$loglevel, $message];
} else {
$this->unauthMessageCache[] = array($loglevel, $message);
}
}
......@@ -182,7 +187,8 @@ abstract class Log{
/**
* 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.
......@@ -195,8 +201,10 @@ abstract class Log{
* @return string
*/
protected function GetLogLevelString($loglevel, $pad = false) {
if ($pad) $s = " ";
else $s = "";
if ($pad)
$s = " ";
else
$s = "";
switch($loglevel) {
case LOGLEVEL_OFF: return ""; break;
case LOGLEVEL_FATAL: return "[FATAL]"; break;
......
......@@ -4,7 +4,7 @@
* Project : Z-Push
* Descr : Logging functionalities
*
* Created : 11.13.2015
* Created : 13.11.2015
*
* Copyright 2007 - 2015 Zarafa Deutschland GmbH
*
......@@ -40,7 +40,7 @@
*
* Consult LICENSE file for details
************************************************/
class Syslog extends Log{
class Syslog extends Log {
protected $program_name = '';
......@@ -55,6 +55,7 @@ class Syslog extends Log{
public function GetProgramName() {
return $this->program_name;
}
/**
* @param string $value
*
......@@ -71,6 +72,7 @@ class Syslog extends Log{
public function GetHost() {
return $this->host;
}
/**
* @param string $value
*
......@@ -87,6 +89,7 @@ class Syslog extends Log{
public function GetPort() {
return $this->port;
}
/**
* @param int $value
*
......@@ -101,9 +104,9 @@ class Syslog extends Log{
public function __construct($program_name = null, $host = null, $port = null) {
parent::__construct();
if(is_null($program_name)) $program_name = LOG_SYSLOG_PROGRAM;
if(is_null($host)) $host = LOG_SYSLOG_HOST;
if(is_null($port)) $port = LOG_SYSLOG_PORT;
if (is_null($program_name)) $program_name = LOG_SYSLOG_PROGRAM;
if (is_null($host)) $host = LOG_SYSLOG_HOST;
if (is_null($port)) $port = LOG_SYSLOG_PORT;
$this->SetProgramName($program_name);
$this->SetHost($host);
......@@ -123,22 +126,21 @@ class Syslog extends Log{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// Shift the "syslog.php" entry.
array_shift($backtrace);
foreach($backtrace as $trace) {
foreach ($backtrace as $trace) {
if (!isset($trace['file'])) {
continue;
}
if (strpos($trace['file'], REAL_BASE_PATH . 'backend/') !== false) {
preg_match('/\/backend\/([a-zA-Z]*)/', $trace['file'], $match);
if (isset($match[1])) {
return $this->GetProgramName().'/'.$match[1];
return $this->GetProgramName() . '/' . $match[1];
}
}
elseif (basename($trace['file'], '.php') != 'zlog') {
return $this->GetProgramName().'/core';
} elseif (basename($trace['file'], '.php') != 'zlog') {
return $this->GetProgramName() . '/core';
}
}
return $this->GetProgramName().'/core';
return $this->GetProgramName() . '/core';
}
/**
......@@ -149,7 +151,7 @@ class Syslog extends Log{
* @return int One of many LOG_* syslog level.
*/
protected function GetZpushLogLevelToSyslogLogLevel($loglevel) {
switch($loglevel) {
switch ($loglevel) {
case LOGLEVEL_FATAL: return LOG_ALERT; break;
case LOGLEVEL_ERROR: return LOG_ERR; break;
case LOGLEVEL_WARN: return LOG_WARNING; break;
......@@ -174,7 +176,7 @@ class Syslog extends Log{
public function BuildLogString($loglevel, $message) {
$log = $this->GetLogLevelString($loglevel); // Never pad syslog log because syslog log are usually read with a software.
$log .= $this->GetUser();
if($loglevel >= LOGLEVEL_DEVICEID) {
if ($loglevel >= LOGLEVEL_DEVICEID) {
$log .= $this->GetDevid();
}
$log .= ' ' . $message;
......@@ -190,15 +192,14 @@ class Syslog extends Log{
if ($this->GetHost() && $this->GetPort()) {
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$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);
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_close($sock);
}
else {
} else {
openlog($this->GenerateProgramName(), LOG_PID, LOG_SYSLOG_FACILITY);
syslog(
$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