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.

353 lines
7.6KB

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