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 { ...@@ -152,8 +152,8 @@ class ZLog {
self::$logger->SetUser($user); self::$logger->SetUser($user);
self::$logger->SetAuthUser(Request::GetAuthUser()); self::$logger->SetAuthUser(Request::GetAuthUser());
self::$logger->SetSpecialLogUsers($specialLogUsers); self::$logger->SetSpecialLogUsers($specialLogUsers);
self::$logger->SetDevid('['. Request::GetDeviceID() .']'); self::$logger->SetDevid(Request::GetDeviceID());
self::$logger->SetPidstr('[' . str_pad(@getmypid(),5," ",STR_PAD_LEFT) . ']'); self::$logger->SetPid(@getmypid());
self::$logger->AfterInitialize(); self::$logger->AfterInitialize();
} }
return self::$logger; return self::$logger;
......
...@@ -44,8 +44,17 @@ class FileLog extends Log { ...@@ -44,8 +44,17 @@ class FileLog extends Log {
*/ */
private function getLogToUserFile() { private function getLogToUserFile() {
if ($this->log_to_user_file === false) { if ($this->log_to_user_file === false) {
if (in_array(strtolower($this->GetDevid()), ['','webservice','validate'])) {
$this->setLogToUserFile(preg_replace('/[^a-z0-9]/', '_', strtolower($this->GetAuthUser())) . '.log'); $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; return $this->log_to_user_file;
} }
...@@ -66,14 +75,19 @@ class FileLog extends Log { ...@@ -66,14 +75,19 @@ class FileLog extends Log {
* *
* @param int $loglevel * @param int $loglevel
* @param string $message * @param string $message
* @param boolean $includeUserDevice puts username and device in the string
* *
* @access public * @access public
* @return string * @return string
*/ */
public function BuildLogString($loglevel, $message) { public function BuildLogString($loglevel, $message, $includeUserDevice) {
$log = Utils::GetFormattedTime() . ' ' . $this->GetPidstr() . ' ' . $this->GetLogLevelString($loglevel, $loglevel >= LOGLEVEL_INFO) . ' ' . $this->GetUser(); $log = Utils::GetFormattedTime() .' ['. str_pad($this->GetPid(),5," ",STR_PAD_LEFT) .'] '. $this->GetLogLevelString($loglevel, $loglevel >= LOGLEVEL_INFO);
if (LOGLEVEL >= LOGLEVEL_DEVICEID || (LOGUSERLEVEL >= LOGLEVEL_DEVICEID && $this->IsAuthUserInSpecialLogUsers())) {
$log .= ' ' . $this->GetDevid(); if ($includeUserDevice) {
$log .= ' '. $this->GetUser();
}
if ($includeUserDevice && (LOGLEVEL >= LOGLEVEL_DEVICEID || (LOGUSERLEVEL >= LOGLEVEL_DEVICEID && $this->IsAuthUserInSpecialLogUsers()))) {
$log .= ' ['. $this->GetDevid() .']';
} }
$log .= ' ' . $message; $log .= ' ' . $message;
return $log; return $log;
...@@ -93,7 +107,7 @@ class FileLog extends Log { ...@@ -93,7 +107,7 @@ class FileLog extends Log {
* @return void * @return void
*/ */
protected function Write($loglevel, $message) { 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); @file_put_contents(LOGFILE, $data, FILE_APPEND);
} }
...@@ -106,7 +120,7 @@ class FileLog extends Log { ...@@ -106,7 +120,7 @@ class FileLog extends Log {
* @return void * @return void
*/ */
public function WriteForUser($loglevel, $message) { 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); @file_put_contents(LOGFILEDIR . $this->getLogToUserFile(), $data, FILE_APPEND);
} }
......
...@@ -43,7 +43,7 @@ abstract class Log { ...@@ -43,7 +43,7 @@ abstract class Log {
/** /**
* @var string * @var string
*/ */
protected $pidstr = ''; protected $pid = '';
/** /**
* @var array * @var array
...@@ -165,8 +165,8 @@ abstract class Log { ...@@ -165,8 +165,8 @@ abstract class Log {
* @access public * @access public
* @return string * @return string
*/ */
public function GetPidstr() { public function GetPid() {
return $this->pidstr; return $this->pid;
} }
/** /**
...@@ -177,8 +177,8 @@ abstract class Log { ...@@ -177,8 +177,8 @@ abstract class Log {
* @access public * @access public
* @return void * @return void
*/ */
public function SetPidstr($value) { public function SetPid($value) {
$this->pidstr = $value; $this->pid = $value;
} }
/** /**
......
...@@ -167,7 +167,7 @@ class Syslog extends Log { ...@@ -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->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;
return $log; 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