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.

363 lines
7.8KB

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