PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / WcLogger.php

WcLogger.php in Packeta trunk, at src/Packetery/Module/WcLogger.php

61 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Packetery\Module;
5
6 use Packetery\Module\Framework\WcAdapter;
7 use Packetery\Tracy\Logger;
8
9 class WcLogger {
10
11 private const LEVEL_ERROR = 'error';
12
13 /**
14 * @param string|\Throwable|mixed $message
15 * @param string $level
16 * @return void
17 */
18 private static function log( $message, string $level ): void {
19 $logCallback = static function () use ( $level, $message ): void {
20 $container = CompatibilityBridge::getContainer();
21 /** @var WcAdapter $wcAdapter */
22 $wcAdapter = $container->getByType( WcAdapter::class );
23 $wcLogger = $wcAdapter->getLogger();
24
25 $wcLogger->log(
26 $level,
27 Logger::formatMessage( $message ),
28 [ 'source' => 'packeta' ]
29 );
30 };
31
32 if ( did_action( 'woocommerce_init' ) > 0 ) {
33 $logCallback();
34 } else {
35 add_action( 'woocommerce_init', $logCallback );
36 }
37 }
38
39 /**
40 * @param string $method
41 * @param string $param
42 * @param string $expectedType
43 * @param mixed $givenValue
44 * @return void
45 */
46 public static function logArgumentTypeError( string $method, string $param, string $expectedType, $givenValue ): void {
47 self::log(
48 sprintf(
49 'Method %s expects parameter "%s" to be type of "%s", type "%s" given. %s%s',
50 $method,
51 $param,
52 $expectedType,
53 is_object( $givenValue ) ? get_class( $givenValue ) : gettype( $givenValue ),
54 PHP_EOL,
55 ( new \Exception() )->getTraceAsString()
56 ),
57 self::LEVEL_ERROR
58 );
59 }
60 }
61