Commit a715c67b authored by Sebastian Kummer's avatar Sebastian Kummer

Merge pull request #629 in ZP/z-push from...

Merge pull request #629 in ZP/z-push from feature/ZP-1104-have-a-device-specific-log-file to develop

* commit '042cf7cf':
  ZP-1104 Log each device to an own file when user in specialLogUsers.
  ZP-1104 Use SetDevid() & SetPid() without crappy strings around them.
parents 58149d60 042cf7cf
......@@ -152,8 +152,8 @@ class ZLog {
self::$logger->SetUser($user);
self::$logger->SetAuthUser(Request::GetAuthUser());
self::$logger->SetSpecialLogUsers($specialLogUsers);
self::$logger->SetDevid('['. Request::GetDeviceID() .']');
self::$logger->SetPidstr('[' . str_pad(@getmypid(),5," ",STR_PAD_LEFT) . ']');
self::$logger->SetDevid(Request::GetDeviceID());
self::$logger->SetPid(@getmypid());
self::$logger->AfterInitialize();
}
return self::$logger;
......
......@@ -44,7 +44,16 @@ class FileLog extends Log {
*/
private function getLogToUserFile() {
if ($this->log_to_user_file === false) {
$this->setLogToUserFile(preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '.log');
if (in_array(strtolower($this->GetDevid()), ['','webservice','validate'])) {
$this->setLogToUserFile(preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '.log');
}
else {
$this->setLogToUserFile(
preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) .'-'.
preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetDevid())) .
'.log'
);
}
}
return $this->log_to_user_file;
}
......@@ -64,16 +73,21 @@ class FileLog extends Log {
/**
* Returns the string to be logged.
*
* @param int $loglevel
* @param string $message
* @param int $loglevel
* @param string $message
* @param boolean $includeUserDevice puts username and device in the string
*
* @access public
* @return string
*/
public function BuildLogString($loglevel, $message) {
$log = Utils::GetFormattedTime() . ' ' . $this->GetPidstr() . ' ' . $this->GetLogLevelString($loglevel, $loglevel >= LOGLEVEL_INFO) . ' ' . $this->GetUser();
if (LOGLEVEL >= LOGLEVEL_DEVICEID || (LOGUSERLEVEL >= LOGLEVEL_DEVICEID && $this->IsAuthUserInSpecialLogUsers())) {
$log .= ' ' . $this->GetDevid();
public function BuildLogString($loglevel, $message, $includeUserDevice) {
$log = Utils::GetFormattedTime() .' ['. str_pad($this->GetPid(),5," ",STR_PAD_LEFT) .'] '. $this->GetLogLevelString($loglevel, $loglevel >= LOGLEVEL_INFO);
if ($includeUserDevice) {
$log .= ' '. $this->GetUser();
}
if ($includeUserDevice && (LOGLEVEL >= LOGLEVEL_DEVICEID || (LOGUSERLEVEL >= LOGLEVEL_DEVICEID && $this->IsAuthUserInSpecialLogUsers()))) {
$log .= ' ['. $this->GetDevid() .']';
}
$log .= ' ' . $message;
return $log;
......@@ -93,7 +107,7 @@ class FileLog extends Log {
* @return void
*/
protected function Write($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL;
$data = $this->buildLogString($loglevel, $message, true) . PHP_EOL;
@file_put_contents(LOGFILE, $data, FILE_APPEND);
}
......@@ -106,7 +120,7 @@ class FileLog extends Log {
* @return void
*/
public function WriteForUser($loglevel, $message) {
$data = $this->buildLogString($loglevel, $message) . PHP_EOL;
$data = $this->buildLogString($loglevel, $message, false) . PHP_EOL;
@file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND);
}
......
......@@ -43,7 +43,7 @@ abstract class Log {
/**
* @var string
*/
protected $pidstr = '';
protected $pid = '';
/**
* @var array
......@@ -165,8 +165,8 @@ abstract class Log {
* @access public
* @return string
*/
public function GetPidstr() {
return $this->pidstr;
public function GetPid() {
return $this->pid;
}
/**
......@@ -177,8 +177,8 @@ abstract class Log {
* @access public
* @return void
*/
public function SetPidstr($value) {
$this->pidstr = $value;
public function SetPid($value) {
$this->pid = $value;
}
/**
......
......@@ -167,7 +167,7 @@ class Syslog extends Log {
$log = $this->GetLogLevelString($loglevel); // Never pad syslog log because syslog log are usually read with a software.
$log .= $this->GetUser();
if ($loglevel >= LOGLEVEL_DEVICEID) {
$log .= $this->GetDevid();
$log .= '['. $this->GetDevid() .']';
}
$log .= ' ' . $message;
return $log;
......
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