PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Shield/Log.php

https://github.com/9ati3nt-zer0/shieldframework
PHP | 113 lines | 69 code | 12 blank | 32 comment | 17 complexity | caa3fe9ed1b996f6d9b45d3aabc9d79c MD5 | raw file
  1. <?php
  2. namespace Shield;
  3. class Log extends Base
  4. {
  5. /**
  6. * Path to the log file
  7. * @var string
  8. */
  9. private $logPath = null;
  10. public function setLogPath($path)
  11. {
  12. $path = realpath($path);
  13. if (!file_exists($path)) {
  14. $this->throwError('Cannot set log path - path does not exist!');
  15. } elseif (!is_writeable($path)) {
  16. $this->throwError('Cannot set log path - not writeable!');
  17. } else {
  18. $this->logPath = $path;
  19. }
  20. }
  21. /**
  22. * Get the current logging path
  23. *
  24. * @return string
  25. */
  26. public function getLogPath()
  27. {
  28. return $this->logPath;
  29. }
  30. /**
  31. * Make the default log path
  32. *
  33. * @return null
  34. */
  35. public function makeLogPath($logPath=null)
  36. {
  37. $logPath = ($logPath !== null) ? $logPath : $this->logPath;
  38. // check to see if we can write to it
  39. if (is_writable($logPath)) {
  40. mkdir($logPath);
  41. return true;
  42. } else {
  43. $this->throwError('Cannot create logs/ directory');
  44. return false;
  45. }
  46. }
  47. /**
  48. * Init the object and set up the config file path
  49. *
  50. * @param object $di DI container
  51. *
  52. * @return null
  53. */
  54. public function __construct(Di $di)
  55. {
  56. // check config for a path or set a default logging path
  57. $logPath = $di->get('Config')->get('log_path');
  58. if ($logPath !== null && is_dir(realpath($logPath)) && is_writable($logPath)) {
  59. $this->setLogPath(realpath($logPath));
  60. } else {
  61. $logPath = __DIR__.'/../app/../logs';
  62. $realpath = realpath($logPath);
  63. if ($realpath === false) {
  64. // directory may not exist, try to create
  65. if ($this->makeLogPath($logPath) === true) {
  66. $this->setLogPath($logPath);
  67. } else {
  68. // all else fails, write to /tmp
  69. $this->setLogPath('/tmp');
  70. }
  71. } else {
  72. $this->setLogPath($realpath);
  73. }
  74. }
  75. }
  76. /**
  77. * Log the message to the data source
  78. *
  79. * @param string $msg Message to write
  80. *
  81. * @return null
  82. */
  83. public function log($msg, $level='info')
  84. {
  85. $logFile = $this->getLogPath().'/log-'.date('Ymd').'.log';
  86. if (is_writeable($this->getLogPath())) {
  87. if (!$fp = fopen($logFile, 'a+')) {
  88. $this->throwError('Cannot open the log file.');
  89. }
  90. if ($fp) {
  91. $msg = '['.date('m.d.Y H:i:s').'] ['.strtoupper($level).'] '.$msg;
  92. if (fwrite($fp, $msg."\n") === false) {
  93. $this->throwError('Cannot write to the log file.');
  94. }
  95. fclose($fp);
  96. }
  97. } else {
  98. $this->throwError('Cannot write to the log directory.');
  99. }
  100. }
  101. }