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.

308 lines
6.6KB

  1. <?php
  2. /**
  3. * @file src/Core/Logger.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\BaseObject;
  7. use Psr\Log\LoggerInterface;
  8. use Psr\Log\LogLevel;
  9. /**
  10. * @brief Logger functions
  11. */
  12. class Logger extends BaseObject
  13. {
  14. /**
  15. * @see Logger::error()
  16. */
  17. const WARNING = LogLevel::ERROR;
  18. /**
  19. * @see Logger::warning()
  20. */
  21. const INFO = LogLevel::WARNING;
  22. /**
  23. * @see Logger::notice()
  24. */
  25. const TRACE = LogLevel::NOTICE;
  26. /**
  27. * @see Logger::info()
  28. */
  29. const DEBUG = LogLevel::INFO;
  30. /**
  31. * @see Logger::debug()
  32. */
  33. const DATA = LogLevel::DEBUG;
  34. /**
  35. * @see Logger::debug()
  36. */
  37. const ALL = LogLevel::DEBUG;
  38. /**
  39. * @var array the legacy loglevels
  40. * @deprecated 2019.03 use PSR-3 loglevels
  41. * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
  42. *
  43. */
  44. public static $levels = [
  45. self::WARNING => 'Warning',
  46. self::INFO => 'Info',
  47. self::TRACE => 'Trace',
  48. self::DEBUG => 'Debug',
  49. self::DATA => 'Data',
  50. self::ALL => 'All',
  51. ];
  52. /**
  53. * @var LoggerInterface A PSR-3 compliant logger instance
  54. */
  55. private static $logger;
  56. /**
  57. * @var LoggerInterface A PSR-3 compliant logger instance for developing only
  58. */
  59. private static $devLogger;
  60. /**
  61. * Sets the default logging handler for Friendica.
  62. *
  63. * @param LoggerInterface $logger The Logger instance of this Application
  64. */
  65. public static function setLogger($logger)
  66. {
  67. self::$logger = $logger;
  68. }
  69. /**
  70. * Sets the default dev-logging handler for Friendica.
  71. *
  72. * @param LoggerInterface $logger The Logger instance of this Application
  73. */
  74. public static function setDevLogger($logger)
  75. {
  76. self::$devLogger = $logger;
  77. }
  78. /**
  79. * System is unusable.
  80. *
  81. * @see LoggerInterface::emergency()
  82. *
  83. * @param string $message
  84. * @param array $context
  85. *
  86. * @return void
  87. * @throws \Exception
  88. */
  89. public static function emergency($message, $context = [])
  90. {
  91. if (!isset(self::$logger)) {
  92. return;
  93. }
  94. $stamp1 = microtime(true);
  95. self::$logger->emergency($message, $context);
  96. self::getApp()->GetProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  97. }
  98. /**
  99. * Action must be taken immediately.
  100. * @see LoggerInterface::alert()
  101. *
  102. * Example: Entire website down, database unavailable, etc. This should
  103. * trigger the SMS alerts and wake you up.
  104. *
  105. * @param string $message
  106. * @param array $context
  107. *
  108. * @return void
  109. * @throws \Exception
  110. */
  111. public static function alert($message, $context = [])
  112. {
  113. if (!isset(self::$logger)) {
  114. return;
  115. }
  116. $stamp1 = microtime(true);
  117. self::$logger->alert($message, $context);
  118. self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  119. }
  120. /**
  121. * Critical conditions.
  122. * @see LoggerInterface::critical()
  123. *
  124. * Example: Application component unavailable, unexpected exception.
  125. *
  126. * @param string $message
  127. * @param array $context
  128. *
  129. * @return void
  130. * @throws \Exception
  131. */
  132. public static function critical($message, $context = [])
  133. {
  134. if (!isset(self::$logger)) {
  135. return;
  136. }
  137. $stamp1 = microtime(true);
  138. self::$logger->critical($message, $context);
  139. self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  140. }
  141. /**
  142. * Runtime errors that do not require immediate action but should typically
  143. * be logged and monitored.
  144. * @see LoggerInterface::error()
  145. *
  146. * @param string $message
  147. * @param array $context
  148. *
  149. * @return void
  150. * @throws \Exception
  151. */
  152. public static function error($message, $context = [])
  153. {
  154. if (!isset(self::$logger)) {
  155. echo "not set!?\n";
  156. return;
  157. }
  158. $stamp1 = microtime(true);
  159. self::$logger->error($message, $context);
  160. self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  161. }
  162. /**
  163. * Exceptional occurrences that are not errors.
  164. * @see LoggerInterface::warning()
  165. *
  166. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  167. * that are not necessarily wrong.
  168. *
  169. * @param string $message
  170. * @param array $context
  171. *
  172. * @return void
  173. * @throws \Exception
  174. */
  175. public static function warning($message, $context = [])
  176. {
  177. if (!isset(self::$logger)) {
  178. return;
  179. }
  180. $stamp1 = microtime(true);
  181. self::$logger->warning($message, $context);
  182. self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  183. }
  184. /**
  185. * Normal but significant events.
  186. * @see LoggerInterface::notice()
  187. *
  188. * @param string $message
  189. * @param array $context
  190. *
  191. * @return void
  192. * @throws \Exception
  193. */
  194. public static function notice($message, $context = [])
  195. {
  196. if (!isset(self::$logger)) {
  197. return;
  198. }
  199. $stamp1 = microtime(true);
  200. self::$logger->notice($message, $context);
  201. self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  202. }
  203. /**
  204. * Interesting events.
  205. * @see LoggerInterface::info()
  206. *
  207. * Example: User logs in, SQL logs.
  208. *
  209. * @param string $message
  210. * @param array $context
  211. *
  212. * @return void
  213. * @throws \Exception
  214. */
  215. public static function info($message, $context = [])
  216. {
  217. if (!isset(self::$logger)) {
  218. return;
  219. }
  220. $stamp1 = microtime(true);
  221. self::$logger->info($message, $context);
  222. self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  223. }
  224. /**
  225. * Detailed debug information.
  226. * @see LoggerInterface::debug()
  227. *
  228. * @param string $message
  229. * @param array $context
  230. *
  231. * @return void
  232. * @throws \Exception
  233. */
  234. public static function debug($message, $context = [])
  235. {
  236. if (!isset(self::$logger)) {
  237. return;
  238. }
  239. $stamp1 = microtime(true);
  240. self::$logger->debug($message, $context);
  241. self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
  242. }
  243. /**
  244. * @brief Logs the given message at the given log level
  245. *
  246. * @param string $msg
  247. * @param string $level
  248. *
  249. * @throws \Exception
  250. * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
  251. */
  252. public static function log($msg, $level = LogLevel::INFO)
  253. {
  254. if (!isset(self::$logger)) {
  255. return;
  256. }
  257. $stamp1 = microtime(true);
  258. self::$logger->log($level, $msg);
  259. self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
  260. }
  261. /**
  262. * @brief An alternative logger for development.
  263. * Works largely as log() but allows developers
  264. * to isolate particular elements they are targetting
  265. * personally without background noise
  266. *
  267. * @param string $msg
  268. * @param string $level
  269. * @throws \Exception
  270. */
  271. public static function devLog($msg, $level = LogLevel::DEBUG)
  272. {
  273. if (!isset(self::$logger)) {
  274. return;
  275. }
  276. $stamp1 = microtime(true);
  277. self::$devLogger->log($level, $msg);
  278. self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
  279. }
  280. }