Skip to content

Commit a9d55eb

Browse files
committed
Refactor MySQL_Proxy class, handle all socket errors, add logger
1 parent 44046ee commit a9d55eb

File tree

5 files changed

+427
-110
lines changed

5 files changed

+427
-110
lines changed

packages/wp-mysql-proxy/bin/wp-mysql-proxy.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
use WP_MySQL_Proxy\MySQL_Proxy;
44
use WP_MySQL_Proxy\Adapter\SQLite_Adapter;
5+
use WP_MySQL_Proxy\Logger;
56

67
require_once __DIR__ . '/../vendor/autoload.php';
78

89
define( 'WP_SQLITE_AST_DRIVER', true );
910

1011
// Process CLI arguments:
11-
$shortopts = 'h:d:p';
12-
$longopts = array( 'help', 'database:', 'port:' );
12+
$shortopts = 'h:d:p:l:';
13+
$longopts = array( 'help', 'database:', 'port:', 'log-level:' );
1314
$opts = getopt( $shortopts, $longopts );
1415

1516
$help = <<<USAGE
1617
Usage: php mysql-proxy.php [--database <path/to/db.sqlite>] [--port <port>]
1718
1819
Options:
19-
-h, --help Show this help message and exit.
20-
-d, --database=<path> The path to the SQLite database file. Default: :memory:
21-
-p, --port=<port> The port to listen on. Default: 3306
20+
-h, --help Show this help message and exit.
21+
-d, --database=<path> The path to the SQLite database file. Default: :memory:
22+
-p, --port=<port> The port to listen on. Default: 3306
23+
-l, --log-level=<level> The log level to use. One of "error", "warning", "info", "debug". Default: info
2224
2325
USAGE;
2426

@@ -38,9 +40,19 @@
3840
exit( 1 );
3941
}
4042

43+
// Log level.
44+
$log_level = $opts['l'] ?? $opts['log-level'] ?? 'info';
45+
if ( ! in_array( $log_level, Logger::LEVELS, true ) ) {
46+
fwrite( STDERR, 'Error: --log-level must be one of: ' . implode( ', ', Logger::LEVELS ) . ". Use --help for more information.\n" );
47+
exit( 1 );
48+
}
49+
4150
// Start the MySQL proxy.
4251
$proxy = new MySQL_Proxy(
4352
new SQLite_Adapter( $db_path ),
44-
array( 'port' => $port )
53+
array(
54+
'port' => $port,
55+
'log_level' => $log_level,
56+
)
4557
);
4658
$proxy->start();
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php declare( strict_types = 1 );
2+
3+
namespace WP_MySQL_Proxy;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* A simple logger for the MySQL proxy.
9+
*/
10+
class Logger {
11+
// Log levels.
12+
const LEVEL_ERROR = 'error';
13+
const LEVEL_WARNING = 'warning';
14+
const LEVEL_INFO = 'info';
15+
const LEVEL_DEBUG = 'debug';
16+
17+
/**
18+
* Log levels in order of severity.
19+
*
20+
* @var array
21+
*/
22+
const LEVELS = array(
23+
self::LEVEL_ERROR,
24+
self::LEVEL_WARNING,
25+
self::LEVEL_INFO,
26+
self::LEVEL_DEBUG,
27+
);
28+
29+
/**
30+
* The current log level.
31+
*
32+
* @var string
33+
*/
34+
private $log_level;
35+
36+
/**
37+
* Constructor.
38+
*
39+
* @param string $log_level The log level to use. Default: Logger::LEVEL_WARNING
40+
*/
41+
public function __construct( string $log_level = self::LEVEL_WARNING ) {
42+
$this->set_log_level( $log_level );
43+
}
44+
45+
/**
46+
* Get the current log level.
47+
*
48+
* @return string
49+
*/
50+
public function get_log_level(): string {
51+
return $this->log_level;
52+
}
53+
54+
/**
55+
* Set the current log level.
56+
*
57+
* @param string $level The log level to use.
58+
*/
59+
public function set_log_level( string $level ): void {
60+
if ( ! in_array( $level, self::LEVELS, true ) ) {
61+
throw new InvalidArgumentException( 'Invalid log level: ' . $level );
62+
}
63+
$this->log_level = $level;
64+
}
65+
66+
/**
67+
* Check if a log level is enabled.
68+
*
69+
* @param string $level The log level to check.
70+
* @return bool
71+
*/
72+
public function is_log_level_enabled( string $level ): bool {
73+
$level_index = array_search( $level, self::LEVELS, true );
74+
if ( false === $level_index ) {
75+
return false;
76+
}
77+
return $level_index <= array_search( $this->log_level, self::LEVELS, true );
78+
}
79+
80+
/**
81+
* Log a message.
82+
*
83+
* @param string $level The log level.
84+
* @param string $message The message to log.
85+
* @param array $context The context to log.
86+
*/
87+
public function log( string $level, string $message, array $context = array() ): void {
88+
// Check log level.
89+
if ( ! $this->is_log_level_enabled( $level ) ) {
90+
return;
91+
}
92+
93+
// Handle PSR-3 placeholder syntax.
94+
$replacements = array();
95+
foreach ( $context as $key => $value ) {
96+
$replacements[ '{' . $key . '}' ] = $value;
97+
}
98+
$message = str_replace( array_keys( $replacements ), $replacements, $message );
99+
100+
// Format and log the message.
101+
fprintf( STDERR, '%s [%s] %s' . PHP_EOL, gmdate( 'Y-m-d H:i:s' ), $level, $message );
102+
}
103+
104+
/**
105+
* Log an error message.
106+
*
107+
* @param string $message The message to log.
108+
* @param array $context The context to log.
109+
*/
110+
public function error( string $message, array $context = array() ): void {
111+
$this->log( self::LEVEL_ERROR, $message, $context );
112+
}
113+
114+
/**
115+
* Log a warning message.
116+
*
117+
* @param string $message The message to log.
118+
* @param array $context The context to log.
119+
*/
120+
public function warning( string $message, array $context = array() ): void {
121+
$this->log( self::LEVEL_WARNING, $message, $context );
122+
}
123+
124+
/**
125+
* Log an info message.
126+
*
127+
* @param string $message The message to log.
128+
* @param array $context The context to log.
129+
*/
130+
public function info( string $message, array $context = array() ): void {
131+
$this->log( self::LEVEL_INFO, $message, $context );
132+
}
133+
134+
/**
135+
* Log a debug message.
136+
*
137+
* @param string $message The message to log.
138+
* @param array $context The context to log.
139+
*/
140+
public function debug( string $message, array $context = array() ): void {
141+
$this->log( self::LEVEL_DEBUG, $message, $context );
142+
}
143+
}

0 commit comments

Comments
 (0)