PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / features / class-loggermaintainer.php

class-loggermaintainer.php in DecaLog 4.4.0, at includes/features/class-loggermaintainer.php

177 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Logger maintenance handling
4 *
5 * Handles all logger maintenance operations.
6 *
7 * @package Features
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace Decalog\Plugin\Feature;
13
14 use Decalog\System\Option;
15 use Decalog\Plugin\Feature\EventTypes;
16
17 /**
18 * Define the logger maintenance functionality.
19 *
20 * Handles all logger maintenance operations.
21 *
22 * @package Features
23 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
24 * @since 1.0.0
25 */
26 class LoggerMaintainer {
27
28 /**
29 * Initialize the class and set its properties.
30 *
31 * @since 1.0.0
32 */
33 public function __construct() {
34 }
35
36 /**
37 * Create an instance of $class_name.
38 *
39 * @param string $class_name The class name.
40 * @param array $args The param of the constructor for $class_name class.
41 * @return boolean|object The instance of the class if creation was possible, null otherwise.
42 * @since 1.0.0
43 */
44 private function create_instance( $class_name, $args = [] ) {
45 if ( class_exists( $class_name ) ) {
46 try {
47 $reflection = new \ReflectionClass( $class_name );
48 return $reflection->newInstanceArgs( $args );
49 } catch ( \Exception $e ) {
50 return false;
51 }
52 }
53 return false;
54 }
55
56 /**
57 * Clean the logger.
58 *
59 * @since 1.0.0
60 */
61 public function cron_clean() {
62 $tracer = new DTracer( 'plugin', DECALOG_PRODUCT_NAME, DECALOG_VERSION );
63 $span = $tracer->start_span( 'Events logs rotation', 'auto' );
64 foreach ( Option::network_get( 'loggers' ) as $key => $logger ) {
65 if ( is_array( $logger ) && array_key_exists( 'handler', $logger ) ) {
66 $classname = 'Decalog\Plugin\Feature\\' . $logger['handler'];
67 if ( class_exists( $classname ) ) {
68 $logger['uuid'] = $key;
69 $instance = $this->create_instance( $classname );
70 $instance->set_logger( $logger );
71 $instance->cron_clean();
72 }
73 }
74 }
75 $tracer->end_span( $span );
76 }
77
78 /**
79 * Update the logger.
80 *
81 * @since 1.0.0
82 */
83 public function update( $from ) {
84 foreach ( Option::network_get( 'loggers' ) as $key => $logger ) {
85 if ( is_array( $logger ) && array_key_exists( 'handler', $logger ) ) {
86 $classname = 'Decalog\Plugin\Feature\\' . $logger['handler'];
87 if ( class_exists( $classname ) ) {
88 $logger['uuid'] = $key;
89 $instance = $this->create_instance( $classname );
90 $instance->set_logger( $logger );
91 $instance->update( $from );
92 }
93 }
94 }
95 }
96
97 /**
98 * Finalize the logger.
99 *
100 * @since 1.0.0
101 */
102 public function finalize() {
103 foreach ( Option::network_get( 'loggers' ) as $key => $logger ) {
104 if ( is_array($logger) && array_key_exists('handler', $logger) ) {
105 $classname = 'Decalog\Plugin\Feature\\' . $logger['handler'];
106 if ( class_exists( $classname ) ) {
107 $logger['uuid'] = $key;
108 $instance = $this->create_instance( $classname );
109 $instance->set_logger( $logger );
110 $instance->finalize();
111 }
112 }
113 }
114 }
115
116 /**
117 * Get loggers debug info (for Site Health).
118 *
119 * @return array The loggers definitions.
120 * @since 1.0.0
121 */
122 public function debug_info() {
123 $result = [];
124 foreach ( Option::network_get( 'loggers' ) as $key => $logger ) {
125 if ( ! array_key_exists( 'configuration', $logger ) ) {
126 $logger['configuration'] = [];
127 }
128 $name = $logger['name'];
129 unset( $logger['name'] );
130 $logger['uuid'] = '{' . $key . '}';
131 $logger['running'] = $logger['running'] ? 'yes' : 'no';
132 if ( array_key_exists( $logger['level'], EventTypes::$level_names ) ) {
133 $logger['level'] = strtolower( EventTypes::$level_names[ $logger['level'] ] );
134 } else {
135 $logger['level'] = 'none';
136 }
137 $privacy = [];
138 foreach ( $logger['privacy'] as $i => $item ) {
139 if ( $item ) {
140 $privacy[] = $i;
141 }
142 }
143 $logger['privacy'] = '[' . implode(', ', $privacy ) . ']';
144 $logger['processors'] = '[' . implode(', ', $logger['processors'] ) . ']';
145 $configuration = [];
146 foreach ( $logger['configuration'] as $i => $item ) {
147 if ( in_array( $i, [ 'webhook', 'token', 'user', 'users', 'filename', 'pass', 'cloudid', 'key' ], true ) ) {
148 $configuration[] = $i . ':xxx';
149 } else {
150 $configuration[] = $i . ':' . ( is_bool( $item ) ? ( $item ? 'false' : 'true' ) : $item );
151 }
152 }
153 $logger['configuration'] = '[' . implode(', ', $configuration ) . ']';
154 $result[ $key ] = [ 'label' => $name, 'value' => $logger ];
155 }
156 return $result;
157 }
158
159 /**
160 * Get loggers debug info (for Site Health).
161 *
162 * @since 1.0.0
163 */
164 public static function forced_pause() {
165 $loggers = Option::network_get( 'loggers', [] );
166 if ( 0 < count( $loggers ) ) {
167 $new = [];
168 foreach ( $loggers as $uuid => $logger ) {
169 $logger['running'] = false;
170 $new[ $uuid ] = $logger;
171 }
172 Option::network_set( 'loggers', $new );
173 }
174 }
175
176 }
177