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.

190 lines
4.7KB

  1. <?php
  2. /**
  3. * @file src/Core/Logger.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\App;
  7. use Friendica\BaseObject;
  8. use Monolog;
  9. use Friendica\Util\DateTimeFormat;
  10. /**
  11. * @brief Logger functions
  12. */
  13. class Logger extends BaseObject
  14. {
  15. /**
  16. * Creates a basic Monolog instance for logging.
  17. *
  18. * @param string $application the current application name (index, daemon, ...)
  19. *
  20. * @return Monolog\Logger The Logger instance
  21. */
  22. public static function create($application)
  23. {
  24. $logger = new Monolog\Logger($application);
  25. $logger->pushProcessor(new Monolog\Processor\IntrospectionProcessor());
  26. $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
  27. $logger->pushProcessor(new Monolog\Processor\WebProcessor());
  28. $logger->pushProcessor(new App\FriendicaLoggerProcessor());
  29. return $logger;
  30. }
  31. /**
  32. * Sets the default Logging handler for this instance.
  33. * Can be combined with other handlers too if necessary.
  34. *
  35. * @param Monolog\Logger $logger The Logger instance of this Application
  36. * @param App $app The Friendica Application
  37. */
  38. public static function loadDefaultHandler($logger, $app)
  39. {
  40. foreach ($logger->getProcessors() as $processor) {
  41. if ($processor instanceof App\FriendicaLoggerProcessor) {
  42. $processor->setProcessId($app->process_id);
  43. }
  44. }
  45. $debugging = Config::get('system', 'debugging');
  46. $logfile = Config::get('system', 'logfile');
  47. $loglevel = intval(Config::get('system', 'loglevel'));
  48. if (!$debugging || !$logfile) {
  49. return;
  50. }
  51. $fileHandler = new Monolog\Handler\StreamHandler($logfile . ".1", $loglevel);
  52. $logger->pushHandler($fileHandler);
  53. }
  54. // Log levels:
  55. //EMERGENCY
  56. //ALERT
  57. //CRITICAL
  58. const WARNING = 0; //ERROR
  59. const INFO = 1; //WARNING
  60. const TRACE = 2; //NOTICE(default)
  61. const DEBUG = 3; //INFO
  62. const DATA = 4; //INFO
  63. const ALL = 5; //DEBUG
  64. public static $levels = [
  65. self::WARNING => 'Warning',
  66. self::INFO => 'Info',
  67. self::TRACE => 'Trace',
  68. self::DEBUG => 'Debug',
  69. self::DATA => 'Data',
  70. self::ALL => 'All',
  71. ];
  72. /**
  73. * @brief Logs the given message at the given log level
  74. *
  75. * @param string $msg
  76. * @param int $level
  77. *
  78. * @deprecated since 2019.03 - use App->getLogger() instead
  79. */
  80. public static function log($msg, $level = self::INFO)
  81. {
  82. $a = self::getApp();
  83. $debugging = Config::get('system', 'debugging');
  84. $logfile = Config::get('system', 'logfile');
  85. $loglevel = intval(Config::get('system', 'loglevel'));
  86. if (
  87. !$debugging
  88. || !$logfile
  89. || $level > $loglevel
  90. ) {
  91. return;
  92. }
  93. $processId = session_id();
  94. if ($processId == '')
  95. {
  96. $processId = $a->process_id;
  97. }
  98. $callers = debug_backtrace();
  99. if (count($callers) > 1) {
  100. $function = $callers[1]['function'];
  101. } else {
  102. $function = '';
  103. }
  104. $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
  105. DateTimeFormat::utcNow(DateTimeFormat::ATOM),
  106. $processId,
  107. self::$levels[$level],
  108. basename($callers[0]['file']),
  109. $callers[0]['line'],
  110. $function,
  111. $msg
  112. );
  113. $stamp1 = microtime(true);
  114. @file_put_contents($logfile, $logline, FILE_APPEND);
  115. $a->saveTimestamp($stamp1, "file");
  116. }
  117. /**
  118. * @brief An alternative logger for development.
  119. * Works largely as log() but allows developers
  120. * to isolate particular elements they are targetting
  121. * personally without background noise
  122. *
  123. * @param string $msg
  124. *
  125. * * @deprecated since 2019.03 - never used
  126. */
  127. public static function devLog($msg)
  128. {
  129. $a = self::getApp();
  130. $logfile = Config::get('system', 'dlogfile');
  131. if (!$logfile) {
  132. return;
  133. }
  134. $dlogip = Config::get('system', 'dlogip');
  135. if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip)
  136. {
  137. return;
  138. }
  139. $processId = session_id();
  140. if ($processId == '')
  141. {
  142. $processId = $a->process_id;
  143. }
  144. if (!is_string($msg)) {
  145. $msg = var_export($msg, true);
  146. }
  147. $callers = debug_backtrace();
  148. $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
  149. DateTimeFormat::utcNow(),
  150. $processId,
  151. basename($callers[0]['file']),
  152. $callers[0]['line'],
  153. $callers[1]['function'],
  154. $msg
  155. );
  156. $stamp1 = microtime(true);
  157. @file_put_contents($logfile, $logline, FILE_APPEND);
  158. $a->saveTimestamp($stamp1, "file");
  159. }
  160. }