/wp-content/plugins/the-events-calendar/common/src/Tribe/Log/Service_Provider.php

https://github.com/livinglab/openlab · PHP · 176 lines · 57 code · 27 blank · 92 comment · 4 complexity · 03205434732679d0f9fd8958d5772a57 MD5 · raw file

  1. <?php
  2. /**
  3. * ${CARET}
  4. *
  5. * @since 4.9.16
  6. *
  7. * @package Tribe\Log
  8. */
  9. namespace Tribe\Log;
  10. use Monolog\Handler\ErrorLogHandler;
  11. use Monolog\Logger;
  12. class Service_Provider extends \tad_DI52_ServiceProvider {
  13. /**
  14. * Binds and sets up implementations.
  15. *
  16. * @since 4.9.16
  17. */
  18. public function register() {
  19. $this->container->singleton( 'log', $this );
  20. $this->container->singleton( static::class, $this );
  21. $this->container->singleton( Logger::class, [ $this, 'build_logger' ] );
  22. $this->container->singleton( 'monolog',
  23. function () {
  24. return $this->container->make( Logger::class );
  25. }
  26. );
  27. add_action( 'tribe_log', [ $this, 'dispatch_log' ], 10, 3 );
  28. /**
  29. * Filters whether to make the Action Logger available as logger or not.
  30. *
  31. * @since 4.9.16
  32. *
  33. * @param bool $use_action_logger Whether to allow logging messages from the \Tribe\Log\Logger class using the
  34. * `tribe_log` action or not.
  35. */
  36. $use_action_logger = apply_filters( 'tribe_log_use_action_logger', false );
  37. if ( $use_action_logger ) {
  38. add_filter( 'tribe_common_logging_engines', [ $this, 'add_logging_engine' ] );
  39. }
  40. }
  41. /**
  42. * Builds and returns the Monolog Logger instance that will listen to the `tribe_log` action.
  43. *
  44. * To avoid the over-head introduced by filtering the filters are applied here, only once, when the instance is
  45. * first built. Any later call will use the singleton instance stored in the container.
  46. *
  47. * @since 4.9.16
  48. *
  49. * @return Logger
  50. */
  51. public function build_logger() {
  52. /**
  53. * Filters the level of the messages that will be logged.
  54. *
  55. * The threshold is inclusive of the level; it default to log any warning and above.
  56. *
  57. * @since 4.9.16
  58. *
  59. * @param int The threshold level; if the level of a message is this level or above, then it will be logged.
  60. *
  61. * @see \Monolog\Logger for possible levels.
  62. */
  63. $level_threshold = apply_filters( 'tribe_log_level', Logger::WARNING );
  64. $error_log_handler = new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $level_threshold );
  65. /**
  66. * Filters whether to use canonical format for the logs or not.
  67. *
  68. * @since 4.9.16
  69. *
  70. * @param bool $use_canonical_format Whether to use canonical format for the logs or not; defaults to `true`.
  71. */
  72. $use_canonical_format = apply_filters( 'tribe_log_canonical', true );
  73. if ( $use_canonical_format ) {
  74. $error_log_handler->setFormatter( new Canonical_Formatter() );
  75. }
  76. $handlers = [
  77. 'default' => $error_log_handler
  78. ];
  79. /**
  80. * Filters the list of handlers that will handle dispatched log messages.
  81. *
  82. * All handlers should implement the `\Monolog\Handler\HandlerInterface`.
  83. *
  84. * @since 4.9.16
  85. *
  86. * @param array $handlers An array of default log handlers.
  87. */
  88. $handlers = apply_filters( 'tribe_log_handlers', $handlers );
  89. // Monolog will log to stderr when no handlers are set.
  90. $logger = new Monolog_Logger( Monolog_Logger::DEFAULT_CHANNEL );
  91. $logger->setHandlers( $handlers );
  92. return $logger;
  93. }
  94. /**
  95. * Dispatch a message of a specific level.
  96. *
  97. * Available levels are: `debug`, `info`, `notice`, `warning`, `error`, `critical`, `alert`, `emergency`.
  98. *
  99. * @since 4.9.16
  100. *
  101. * @param string|int $level Either the log level or the log pretty name, see long description.
  102. * @param string $message The message to log.
  103. * @param array $context An array of values to define the context.
  104. *
  105. * @see \Monolog\Logger for the log level constants and names.
  106. */
  107. public function dispatch_log( $level = 'debug', $message = '', array $context = [] ) {
  108. // Goes from something like `debug` to `100`.
  109. $level = is_numeric( $level ) ? $level : Logger::toMonologLevel( $level );
  110. /** @var Logger $logger */
  111. $logger = $this->container->make( Logger::class );
  112. $logger->log( $level, $message, $context );
  113. }
  114. /**
  115. * Makes the action-based logging engine available in the backend.
  116. *
  117. * @since 4.9.16
  118. *
  119. * @param array $logging_engines An array of available logging engines.
  120. *
  121. * @return array The updated array of logging engines.
  122. */
  123. public function add_logging_engine( array $logging_engines = [] ) {
  124. $logging_engines[ Action_Logger::class ] = new Action_Logger();
  125. return $logging_engines;
  126. }
  127. /**
  128. * Enables logging in the service provider, if not already enabled.
  129. *
  130. * @since 4.12.15
  131. */
  132. public function enable() {
  133. if ( has_action( 'tribe_log', [ $this, 'dispatch_log' ] ) ) {
  134. return;
  135. }
  136. add_action( 'tribe_log', [ $this, 'dispatch_log' ] );
  137. }
  138. /**
  139. * Disables the logging functions.
  140. *
  141. * @since 4.12.15
  142. */
  143. public function disable() {
  144. if ( ! has_action( 'tribe_log', [ $this, 'dispatch_log' ] ) ) {
  145. return;
  146. }
  147. remove_action( 'tribe_log', [ $this, 'dispatch_log' ] );
  148. }
  149. }