/environment/classes/logger/backend/Logger_Backend_File.class.php

https://gitlab.com/x33n/ProjectPier-Core · PHP · 153 lines · 75 code · 19 blank · 59 comment · 13 complexity · e9aa37579ece8451b72cfe6b85184c41 MD5 · raw file

  1. <?php
  2. /**
  3. * Logger backend that is able to save session data into a text file
  4. *
  5. * @package Logger
  6. * @subpackage backends
  7. * @http://www.projectpier.org/
  8. */
  9. class Logger_Backend_File implements Logger_Backend {
  10. /** New line **/
  11. const NEW_LINE = "\r\n";
  12. const SET_SEPARATOR = '===============================================================================';
  13. const SESSION_SEPARATOR = '-------------------------------------------------------------------------------';
  14. /**
  15. * Path of the log file. If file exists it need to be writable, if not parent
  16. * folder need to exist and be writable
  17. *
  18. * @var string
  19. */
  20. private $log_file;
  21. /**
  22. * Constructor
  23. *
  24. * @param void
  25. * @return Logger_Backend_File
  26. */
  27. function __construct($log_file) {
  28. $this->setLogFile($log_file);
  29. } // __construct
  30. // ---------------------------------------------------
  31. // Backend interface implementation and utils
  32. // ---------------------------------------------------
  33. /**
  34. * Save array of sessions into a single session set
  35. *
  36. * @param array $sessions
  37. * @return boolean
  38. */
  39. public function saveSessionSet($sessions) {
  40. if (!is_array($sessions)) {
  41. return false;
  42. } // if
  43. $session_names = array();
  44. $session_outputs = array();
  45. foreach ($sessions as $session) {
  46. if ($session instanceof Logger_Session) {
  47. $session_names[] = $session->getName();
  48. $session_outputs[] = $this->renderSessionContent($session);
  49. } // if
  50. } // if
  51. if (!count($session_names) || !count($session_outputs)) {
  52. return false;
  53. } // if
  54. $output = self::SET_SEPARATOR . self::NEW_LINE . 'Session set: ' . implode(', ', $session_names) . self::NEW_LINE . self::SET_SEPARATOR;
  55. foreach ($session_outputs as $session_output) {
  56. $output .= self::NEW_LINE . $session_output;
  57. } // foreach
  58. $output .= self::NEW_LINE . self::SET_SEPARATOR;
  59. return file_put_contents($this->getLogFile(), self::NEW_LINE . $output . self::NEW_LINE, FILE_APPEND);
  60. } // saveSessionSet
  61. /**
  62. * Save session object into the file
  63. *
  64. * @param Logger_Session $session
  65. * @return boolean
  66. */
  67. public function saveSession(Logger_Session $session) {
  68. $output = $this->renderSessionContent($session);
  69. return file_put_contents($this->getLogFile(), self::NEW_LINE . $output . self::NEW_LINE, FILE_APPEND);
  70. } // saveSession
  71. /**
  72. * Prepare session output as string
  73. *
  74. * @param Logger_Session $session
  75. * @return string
  76. */
  77. private function renderSessionContent(Logger_Session $session) {
  78. $session_executed_in = microtime(true) - $session->getSessionStart();
  79. $counter = 0;
  80. $output = 'Session "' . $session->getName() . '" started at ' . gmdate(DATE_ISO8601, floor($session->getSessionStart())) . "\n";
  81. if ($session->isEmpty()) {
  82. $output .= 'Empty session';
  83. } else {
  84. foreach ($session->getEntries() as $entry) {
  85. $counter++;
  86. $output .= "#$counter " . Logger::severityToString($entry->getSeverity()) . ': ' . $entry->getFormattedMessage(' ') . "\n";
  87. } // foreach
  88. } // if
  89. $output .= "Time since start: " . $session_executed_in . " seconds\n" . self::SESSION_SEPARATOR;
  90. return str_replace("\n", self::NEW_LINE, $output);
  91. } // renderSessionContent
  92. // ---------------------------------------------------
  93. // Getters and setters
  94. // ---------------------------------------------------
  95. /**
  96. * Get log_file
  97. *
  98. * @param null
  99. * @return string
  100. */
  101. function getLogFile() {
  102. if (trim($this->log_file) && !is_file($this->log_file)) {
  103. file_put_contents($this->log_file, '<?php die(); ?>');
  104. } // if
  105. return $this->log_file;
  106. } // getLogFile
  107. /**
  108. * Set log_file value
  109. *
  110. * @param string $value
  111. * @return null
  112. * @throws FileNotWritableError If file exists and is not writable
  113. * @throws DirDnxError If file does not exists and parent directory does not exists
  114. * @throws DirNotWritableError If file does not exists, but parent exists and is not writable
  115. */
  116. function setLogFile($value) {
  117. $file_path = $value;
  118. if (is_file($file_path)) {
  119. if (!file_is_writable($file_path)) {
  120. throw new FileNotWritableError($file_path);
  121. } // if
  122. } else {
  123. $folder_path = dirname($file_path);
  124. if (!is_dir($folder_path)) {
  125. throw new DirDnxError($folder_path);
  126. } // if
  127. if (!folder_is_writable($folder_path)) {
  128. throw new DirNotWritableError($folder_path);
  129. } // if
  130. } // if
  131. $this->log_file = $value;
  132. } // setLogFile
  133. } // Logger_Backend_File
  134. ?>