Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.2KB

  1. <?php
  2. /**
  3. * @file src/Core/Logger.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\BaseObject;
  7. use Friendica\Core\Config;
  8. use Friendica\Util\DateTimeFormat;
  9. use ReflectionClass;
  10. /**
  11. * @brief Logger functions
  12. */
  13. class Logger extends BaseObject
  14. {
  15. // Log levels:
  16. const WARNING = 0;
  17. const INFO = 1;
  18. const TRACE = 2;
  19. const DEBUG = 3;
  20. const DATA = 4;
  21. const ALL = 5;
  22. public static $levels = [
  23. self::WARNING => 'Warning',
  24. self::INFO => 'Info',
  25. self::TRACE => 'Trace',
  26. self::DEBUG => 'Debug',
  27. self::DATA => 'Data',
  28. self::ALL => 'All',
  29. ];
  30. /**
  31. * @brief Logs the given message at the given log level
  32. *
  33. * @param string $msg
  34. * @param int $level
  35. */
  36. public static function log($msg, $level = self::INFO)
  37. {
  38. $a = self::getApp();
  39. $debugging = Config::get('system', 'debugging');
  40. $logfile = Config::get('system', 'logfile');
  41. $loglevel = intval(Config::get('system', 'loglevel'));
  42. if (
  43. !$debugging
  44. || !$logfile
  45. || $level > $loglevel
  46. ) {
  47. return;
  48. }
  49. $processId = session_id();
  50. if ($processId == '')
  51. {
  52. $processId = $a->process_id;
  53. }
  54. $callers = debug_backtrace();
  55. if (count($callers) > 1) {
  56. $function = $callers[1]['function'];
  57. } else {
  58. $function = '';
  59. }
  60. $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
  61. DateTimeFormat::utcNow(DateTimeFormat::ATOM),
  62. $processId,
  63. self::$levels[$level],
  64. basename($callers[0]['file']),
  65. $callers[0]['line'],
  66. $function,
  67. $msg
  68. );
  69. $stamp1 = microtime(true);
  70. @file_put_contents($logfile, $logline, FILE_APPEND);
  71. $a->saveTimestamp($stamp1, "file");
  72. }
  73. /**
  74. * @brief An alternative logger for development.
  75. * Works largely as log() but allows developers
  76. * to isolate particular elements they are targetting
  77. * personally without background noise
  78. *
  79. * @param string $msg
  80. */
  81. public static function devLog($msg)
  82. {
  83. $a = self::getApp();
  84. $logfile = Config::get('system', 'dlogfile');
  85. if (!$logfile) {
  86. return;
  87. }
  88. $dlogip = Config::get('system', 'dlogip');
  89. if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip)
  90. {
  91. return;
  92. }
  93. $processId = session_id();
  94. if ($processId == '')
  95. {
  96. $processId = $a->process_id;
  97. }
  98. if (!is_string($msg)) {
  99. $msg = var_export($msg, true);
  100. }
  101. $callers = debug_backtrace();
  102. $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
  103. DateTimeFormat::utcNow(),
  104. $processId,
  105. basename($callers[0]['file']),
  106. $callers[0]['line'],
  107. $callers[1]['function'],
  108. $msg
  109. );
  110. $stamp1 = microtime(true);
  111. @file_put_contents($logfile, $logline, FILE_APPEND);
  112. $a->saveTimestamp($stamp1, "file");
  113. }
  114. }