PluginProbe
seQura / 4.3.1
seQura v4.3.1
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Core / Implementation / Infrastructure / Logger / Interfaces / class-default-logger-adapter.php

class-default-logger-adapter.php in seQura 4.3.1, at src/Core/Implementation/Infrastructure/Logger/Interfaces/class-default-logger-adapter.php

106 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Default Logger
4 *
5 * @package SeQura\WC
6 */
7
8 namespace SeQura\WC\Core\Implementation\Infrastructure\Logger\Interfaces;
9
10 use SeQura\Core\Infrastructure\Logger\Interfaces\DefaultLoggerAdapter;
11 use SeQura\Core\Infrastructure\Logger\LogData;
12 use SeQura\Core\Infrastructure\Logger\Logger;
13 use SeQura\Core\Infrastructure\Utility\TimeProvider;
14 use SeQura\WC\Services\Log\Interface_Log_File;
15 use Throwable;
16
17 /**
18 * Default Logger
19 */
20 class Default_Logger_Adapter implements DefaultLoggerAdapter {
21
22 /**
23 * The log file path.
24 *
25 * @var Interface_Log_File
26 */
27 private $log_file;
28
29 /**
30 * The time provider.
31 *
32 * @var TimeProvider
33 */
34 private $time_provider;
35
36 /**
37 * Constructor.
38 */
39 public function __construct( Interface_Log_File $log_file, TimeProvider $time_provider ) {
40 $this->log_file = $log_file;
41 $this->time_provider = $time_provider;
42 }
43
44 /**
45 * Get the level name.
46 *
47 * @param int $level The level.
48 */
49 private function get_level_name( int $level ): string {
50 switch ( $level ) {
51 case Logger::DEBUG:
52 return 'DEBUG';
53 case Logger::INFO:
54 return 'INFO';
55 case Logger::WARNING:
56 return 'WARNING';
57 case Logger::ERROR:
58 return 'ERROR';
59 default:
60 return '';
61 }
62 }
63
64 /**
65 * Log message in system.
66 *
67 * @param LogData $data Log data.
68 * @return void
69 */
70 public function logMessage( LogData $data ): void {
71 try {
72 if ( $this->log_file->setup() ) {
73 $datetime = $this->time_provider->getDateTime( (int) ( $data->getTimestamp() / 1000 ) ); // Original timestamp is in milliseconds.
74 $ctx_data = '';
75
76 if ( ! empty( $data->getContext() ) ) {
77 $ctx = array();
78 foreach ( $data->getContext() as $log_context_data ) {
79 $arr = $log_context_data->toArray();
80 if ( isset( $arr['value'] ) && is_string( $arr['value'] ) ) {
81 $decoded_value = json_decode( $arr['value'], true );
82 if ( null !== $decoded_value ) {
83 $arr['value'] = $decoded_value;
84 }
85 }
86 $ctx[] = $arr;
87 }
88 $ctx_data .= ' ' . \wp_json_encode( $ctx, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_LINE_TERMINATORS );
89 }
90
91 $formatted_message = sprintf(
92 "%s\t%s\t%s\t%s\r\n",// phpcs:ignore
93 $this->get_level_name( $data->getLogLevel() ),
94 $this->time_provider->serializeDate( $datetime, 'Y-m-d H:i:s' ),
95 $data->getMessage(),
96 $ctx_data
97 );
98
99 $this->log_file->append_content( $formatted_message );
100 }
101 } catch ( Throwable $e ) { // phpcs:ignore
102 // Do nothing.
103 }
104 }
105 }
106