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.

359 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. * @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::mapLegacyConfigDebugLevel($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 mapLegacyConfigDebugLevel($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. * @see LoggerInterface::emergency()
  128. *
  129. * @param string $message
  130. * @param array $context
  131. *
  132. * @return void
  133. *
  134. */
  135. public static function emergency($message, $context = [])
  136. {
  137. if (!isset(self::$logger)) {
  138. return;
  139. }
  140. $stamp1 = microtime(true);
  141. self::$logger->emergency($message, $context);
  142. self::getApp()->saveTimestamp($stamp1, 'file');
  143. }
  144. /**
  145. * Action must be taken immediately.
  146. * @see LoggerInterface::alert()
  147. *
  148. * Example: Entire website down, database unavailable, etc. This should
  149. * trigger the SMS alerts and wake you up.
  150. *
  151. * @param string $message
  152. * @param array $context
  153. *
  154. * @return void
  155. *
  156. */
  157. public static function alert($message, $context = [])
  158. {
  159. if (!isset(self::$logger)) {
  160. return;
  161. }
  162. $stamp1 = microtime(true);
  163. self::$logger->alert($message, $context);
  164. self::getApp()->saveTimestamp($stamp1, 'file');
  165. }
  166. /**
  167. * Critical conditions.
  168. * @see LoggerInterface::critical()
  169. *
  170. * Example: Application component unavailable, unexpected exception.
  171. *
  172. * @param string $message
  173. * @param array $context
  174. *
  175. * @return void
  176. *
  177. */
  178. public static function critical($message, $context = [])
  179. {
  180. if (!isset(self::$logger)) {
  181. return;
  182. }
  183. $stamp1 = microtime(true);
  184. self::$logger->critical($message, $context);
  185. self::getApp()->saveTimestamp($stamp1, 'file');
  186. }
  187. /**
  188. * Runtime errors that do not require immediate action but should typically
  189. * be logged and monitored.
  190. * @see LoggerInterface::error()
  191. *
  192. * @param string $message
  193. * @param array $context
  194. *
  195. * @return void
  196. *
  197. */
  198. public static function error($message, $context = [])
  199. {
  200. if (!isset(self::$logger)) {
  201. echo "not set!?\n";
  202. return;
  203. }
  204. $stamp1 = microtime(true);
  205. self::$logger->error($message, $context);
  206. self::getApp()->saveTimestamp($stamp1, 'file');
  207. }
  208. /**
  209. * Exceptional occurrences that are not errors.
  210. * @see LoggerInterface::warning()
  211. *
  212. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  213. * that are not necessarily wrong.
  214. *
  215. * @param string $message
  216. * @param array $context
  217. *
  218. * @return void
  219. *
  220. */
  221. public static function warning($message, $context = [])
  222. {
  223. if (!isset(self::$logger)) {
  224. return;
  225. }
  226. $stamp1 = microtime(true);
  227. self::$logger->warning($message, $context);
  228. self::getApp()->saveTimestamp($stamp1, 'file');
  229. }
  230. /**
  231. * Normal but significant events.
  232. * @see LoggerInterface::notice()
  233. *
  234. * @param string $message
  235. * @param array $context
  236. *
  237. * @return void
  238. *
  239. */
  240. public static function notice($message, $context = [])
  241. {
  242. if (!isset(self::$logger)) {
  243. return;
  244. }
  245. $stamp1 = microtime(true);
  246. self::$logger->notice($message, $context);
  247. self::getApp()->saveTimestamp($stamp1, 'file');
  248. }
  249. /**
  250. * Interesting events.
  251. * @see LoggerInterface::info()
  252. *
  253. * Example: User logs in, SQL logs.
  254. *
  255. * @param string $message
  256. * @param array $context
  257. *
  258. * @return void
  259. *
  260. */
  261. public static function info($message, $context = [])
  262. {
  263. if (!isset(self::$logger)) {
  264. return;
  265. }
  266. $stamp1 = microtime(true);
  267. self::$logger->info($message, $context);
  268. self::getApp()->saveTimestamp($stamp1, 'file');
  269. }
  270. /**
  271. * Detailed debug information.
  272. * @see LoggerInterface::debug()
  273. *
  274. * @param string $message
  275. * @param array $context
  276. *
  277. * @return void
  278. */
  279. public static function debug($message, $context = [])
  280. {
  281. if (!isset(self::$logger)) {
  282. return;
  283. }
  284. $stamp1 = microtime(true);
  285. self::$logger->debug($message, $context);
  286. self::getApp()->saveTimestamp($stamp1, 'file');
  287. }
  288. /**
  289. * @brief Logs the given message at the given log level
  290. *
  291. * @param string $msg
  292. * @param int $level
  293. *
  294. * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
  295. */
  296. public static function log($msg, $level = LogLevel::NOTICE)
  297. {
  298. if (!isset(self::$logger)) {
  299. return;
  300. }
  301. $stamp1 = microtime(true);
  302. self::$logger->log($level, $msg);
  303. self::getApp()->saveTimestamp($stamp1, "file");
  304. }
  305. /**
  306. * @brief An alternative logger for development.
  307. * Works largely as log() but allows developers
  308. * to isolate particular elements they are targetting
  309. * personally without background noise
  310. *
  311. * @param string $msg
  312. * @param string $level
  313. */
  314. public static function devLog($msg, $level = LogLevel::DEBUG)
  315. {
  316. if (!isset(self::$logger)) {
  317. return;
  318. }
  319. $stamp1 = microtime(true);
  320. self::$devLogger->log($level, $msg);
  321. self::getApp()->saveTimestamp($stamp1, "file");
  322. }
  323. }