diff --git a/lib/kolab_sync_logger.php b/lib/kolab_sync_logger.php index f52b1a2..c6864a5 100644 --- a/lib/kolab_sync_logger.php +++ b/lib/kolab_sync_logger.php @@ -1,189 +1,210 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ /** * Class for logging messages into log file(s) */ class kolab_sync_logger extends Zend_Log { public $mode; protected $log_driver; protected $logfile; protected $format; protected $log_dir; protected $username; /** * Constructor */ public function __construct($mode = null) { $rcube = rcube::get_instance(); $this->mode = intval($mode); $this->logfile = $rcube->config->get('activesync_log_file'); $this->log_driver = $rcube->config->get('log_driver'); $this->format = $rcube->config->get('log_date_format', 'd-M-Y H:i:s O'); $this->log_dir = $rcube->config->get('log_dir'); $r = new ReflectionClass($this); $this->_priorities = $r->getConstants(); } public function __call($method, $params) { $method = strtoupper($method); if ($this->_priorities[$method] <= $this->mode) { $this->log(array_shift($params), $method); } } /** * Check whether debug logging is enabled * * @return bool */ public function hasDebug() { // This is what we check in self::log() below return !empty($this->log_dir) && $this->mode >= self::NOTICE; } /** * Message logger * * @param string $message Log message * @param int|string $method Message severity */ public function log($message, $method, $extras = null) { if (is_numeric($method)) { $mode = $method; $method = array_search($method, $this->_priorities); } else { $mode = $this->_priorities[$method]; } // Don't log messages with lower prio than the configured one if ($mode > $this->mode) { return; } // Don't log debug messages if it's disabled e.g. by per_user_logging if (empty($this->log_dir) && $mode >= self::NOTICE) { return; } $rcube = rcube::get_instance(); $log_dir = $this->log_dir ?: $rcube->config->get('log_dir'); $logfile = $this->logfile; // if log_file is configured all logs will go to it // otherwise use separate file for info/debug and warning/error if (!$logfile) { switch ($mode) { case self::DEBUG: case self::INFO: case self::NOTICE: $file = 'console'; break; default: $file = 'errors'; break; } $logfile = $log_dir . DIRECTORY_SEPARATOR . $file; if (version_compare(version_parse(RCUBE_VERSION), '1.4.0') >= 0) { $logfile .= $rcube->config->get('log_file_ext', '.log'); } } elseif ($logfile[0] != '/') { $logfile = $log_dir . DIRECTORY_SEPARATOR . $logfile; } if (!is_string($message)) { $message = var_export($message, true); } + // write message in logfmt format with extra info when configured to log to STDOUT + if ($this->log_driver == 'logfmt') { + $user_name = $this->username; + + $output = "name=$file component=syncroton"; + $params = ['cmd' => 'Cmd', 'device' => 'DeviceId', 'type' => 'DeviceType']; + + foreach ($params as $key => $val) { + if ($val = $_GET[$val]) { + $output .= " $key=$val"; + } + } + + if (!empty($user_name)) { + $output .= " user=$user_name"; + } + $line = json_encode($message); + $output .= " log=$line\n"; + return file_put_contents("php://stdout", $output, FILE_APPEND) !== false; + } + // add user/request information to the log if ($mode <= self::WARN) { $device = []; $params = ['cmd' => 'Cmd', 'device' => 'DeviceId', 'type' => 'DeviceType']; if (!empty($this->username)) { $device['user'] = $this->username; } foreach ($params as $key => $val) { if ($val = $_GET[$val]) { $device[$key] = $val; } } if (!empty($device)) { $message = @json_encode($device) . ' ' . $message; } } $date = rcube_utils::date_format($this->format); $logline = sprintf("[%s]: [%s] %s\n", $date, $method, $message); // write message with file name when configured to log to STDOUT if ($this->log_driver == 'stdout') { $stdout = "php://stdout"; file_put_contents($stdout, $logline, FILE_APPEND); return; } if ($fp = @fopen($logfile, 'a')) { fwrite($fp, $logline); fflush($fp); fclose($fp); return; } if ($mode <= self::WARN) { // send error to PHPs error handler if write to file didn't succeed trigger_error($message, E_USER_WARNING); } } /** * Set current user name to add into error log */ public function set_username($username) { $this->username = $username; } /** * Set log directory */ public function set_log_dir($dir) { $this->log_dir = $dir; } }