Cli
1 year ago
Exporters
1 year ago
Importers
1 year ago
ResourceStorages
1 year ago
ResultFormatters
1 year ago
Schemas
1 year ago
Steps
1 year ago
docs
1 year ago
BuiltInExporters.php
1 year ago
BuiltInStepProcessors.php
1 year ago
ClassExtractor.php
1 year ago
Cli.php
1 year ago
ExportSchema.php
1 year ago
ImportSchema.php
1 year ago
ImportStep.php
1 year ago
Logger.php
1 year ago
ResourceStorages.php
1 year ago
StepProcessor.php
1 year ago
StepProcessorResult.php
1 year ago
UsePluginHelpers.php
1 year ago
UsePubSub.php
1 year ago
UseWPFunctions.php
1 year ago
Util.php
1 year ago
Logger.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blueprint\UseWPFunctions; |
| 6 | |
| 7 | /** |
| 8 | * Class Logger |
| 9 | */ |
| 10 | class Logger { |
| 11 | use UseWPFunctions; |
| 12 | |
| 13 | /** |
| 14 | * WooCommerce logger class instance. |
| 15 | * |
| 16 | * @var \WC_Logger_Interface |
| 17 | */ |
| 18 | private $logger; |
| 19 | |
| 20 | /** |
| 21 | * Constructor. |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | $this->logger = wc_get_logger(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Log a message as a debug log entry. |
| 29 | * |
| 30 | * @param string $message The message to log. |
| 31 | * @param string $level The log level. |
| 32 | * @param array $context The context of the log. |
| 33 | */ |
| 34 | public function log( string $message, string $level = \WC_Log_Levels::DEBUG, $context = array() ) { |
| 35 | $this->logger->log( |
| 36 | $level, |
| 37 | $message, |
| 38 | array_merge( |
| 39 | array( |
| 40 | 'source' => 'wc-blueprint', |
| 41 | 'user_id' => $this->wp_get_current_user_id(), |
| 42 | ), |
| 43 | $context |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Log the start of an export operation. |
| 50 | * |
| 51 | * @param array $exporters Array of exporters. |
| 52 | */ |
| 53 | public function start_export( array $exporters ) { |
| 54 | $export_data = $this->get_export_data( $exporters ); |
| 55 | |
| 56 | $this->log( |
| 57 | sprintf( 'Starting export of %d steps', count( $export_data['steps'] ) ), |
| 58 | \WC_Log_Levels::INFO, |
| 59 | array( |
| 60 | 'steps' => $export_data['steps'], |
| 61 | 'exporters' => $export_data['exporters'], |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Log the completion of an export operation. |
| 68 | * |
| 69 | * @param array $exporters Array of exporters. |
| 70 | */ |
| 71 | public function complete_export( array $exporters ) { |
| 72 | $export_data = $this->get_export_data( $exporters ); |
| 73 | |
| 74 | $this->log( |
| 75 | sprintf( 'Export of %d steps completed', count( $export_data['steps'] ) ), |
| 76 | \WC_Log_Levels::INFO, |
| 77 | array( |
| 78 | 'steps' => $export_data['steps'], |
| 79 | 'exporters' => $export_data['exporters'], |
| 80 | ) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Extract export step names and exporter classes from exporters. |
| 86 | * |
| 87 | * @param array $exporters Array of exporters. |
| 88 | * @return array Associative array with 'steps' and 'exporters' keys. |
| 89 | */ |
| 90 | private function get_export_data( array $exporters ) { |
| 91 | $export_steps = array(); |
| 92 | $exporter_classes = array(); |
| 93 | |
| 94 | foreach ( $exporters as $exporter ) { |
| 95 | $step_name = method_exists( $exporter, 'get_alias' ) ? $exporter->get_alias() : $exporter->get_step_name(); |
| 96 | $export_steps[] = $step_name; |
| 97 | $exporter_classes[] = get_class( $exporter ); |
| 98 | } |
| 99 | |
| 100 | return array( |
| 101 | 'steps' => $export_steps, |
| 102 | 'exporters' => $exporter_classes, |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Log an export step failure. |
| 108 | * |
| 109 | * @param string $step_name The name of the step that failed. |
| 110 | * @param \Throwable $exception The exception that was thrown. |
| 111 | */ |
| 112 | public function export_step_failed( string $step_name, \Throwable $exception ) { |
| 113 | $this->log( |
| 114 | sprintf( 'Export "%s" step failed', $step_name ), |
| 115 | \WC_Log_Levels::ERROR, |
| 116 | array( |
| 117 | 'error' => $exception->getMessage(), |
| 118 | ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Log the start of an import step. |
| 124 | * |
| 125 | * @param string $step_name The name of the step being imported. |
| 126 | * @param string $importer_class The class name of the importer. |
| 127 | */ |
| 128 | public function start_import( string $step_name, string $importer_class ) { |
| 129 | $this->log( |
| 130 | sprintf( 'Starting import "%s" step', $step_name ), |
| 131 | \WC_Log_Levels::INFO, |
| 132 | array( |
| 133 | 'importer' => $importer_class, |
| 134 | ) |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Log the successful completion of an import step. |
| 140 | * |
| 141 | * @param string $step_name The name of the step that was imported. |
| 142 | * @param StepProcessorResult $result The result of the import. |
| 143 | */ |
| 144 | public function complete_import( string $step_name, StepProcessorResult $result ) { |
| 145 | $this->log( |
| 146 | sprintf( 'Import "%s" step completed', $step_name ), |
| 147 | \WC_Log_Levels::INFO, |
| 148 | array( |
| 149 | 'messages' => $result->get_messages( 'info' ), |
| 150 | ) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Log an import step failure. |
| 156 | * |
| 157 | * @param string $step_name The name of the step that failed. |
| 158 | * @param StepProcessorResult $result The result of the import. |
| 159 | */ |
| 160 | public function import_step_failed( string $step_name, StepProcessorResult $result ) { |
| 161 | $this->log( |
| 162 | sprintf( 'Import "%s" step failed', $step_name ), |
| 163 | \WC_Log_Levels::ERROR, |
| 164 | array( |
| 165 | 'messages' => $result->get_messages( 'error' ), |
| 166 | ) |
| 167 | ); |
| 168 | } |
| 169 | } |
| 170 |