MXS-1652 Add possibility to log SQL statements

With the flag --debug=enable-statement-logging it is now possible
to instruct MaxScale to log all SQL statements it sends to the
servers.

The format of the logged string looks like:

    notice : SQL(127.0.0.1): 0, "SELECT ..."

First the fixed string "SQL", followed by the server address in
parenthesis followed by the actual return value of mysql_query(),
followed by the statement itself.

The "SQL" string makes the lines easy to grep for and having the
return value before the statement makes it easier to spot since
the length of the return value string does not wary much, but the
length of the statements do wary a lot.
This commit is contained in:
Johan Wikman 2018-02-06 13:42:26 +02:00
parent 4a478d31f3
commit 90fdbf8860
3 changed files with 74 additions and 0 deletions

View File

@ -124,4 +124,19 @@ mxs_mysql_name_kind_t mxs_mysql_name_to_pcre(char *pcre,
*/
void mxs_mysql_set_server_version(MYSQL* mysql, SERVER* server);
/**
* Enable/disable the logging of all SQL statements MaxScale sends to
* the servers.
*
* @param enable If true, enable, if false, disable.
*/
void mxs_mysql_set_log_statements(bool enable);
/**
* Returns whether SQL statements sent to the servers are logged or not.
*
* @return True, if statements are logged, false otherwise.
*/
bool mxs_mysql_get_log_statements();
MXS_END_DECLS

View File

@ -43,6 +43,7 @@
#include <maxscale/housekeeper.h>
#include <maxscale/log_manager.h>
#include <maxscale/maxscale.h>
#include <maxscale/mysql_utils.h>
#include <maxscale/paths.h>
#include <maxscale/query_classifier.h>
#include <maxscale/server.h>
@ -188,6 +189,8 @@ static bool modules_process_init();
static void modules_process_finish();
static void disable_module_unloading(const char* arg);
static void enable_module_unloading(const char* arg);
static void enable_statement_logging(const char* arg);
static void disable_statement_logging(const char* arg);
static void redirect_output_to_file(const char* arg);
static bool user_is_acceptable(const char* specified_user);
static bool init_sqlite3();
@ -217,6 +220,14 @@ const DEBUG_ARGUMENT debug_arguments[] =
"redirect-output-to-file", redirect_output_to_file,
"redirect stdout and stderr to the file given as an argument"
},
{
"enable-statement-logging", enable_statement_logging,
"enable the logging of SQL statements sent by MaxScale to the servers"
},
{
"disable-statement-logging", disable_statement_logging,
"disable the logging of SQL statements sent by MaxScale to the servers"
},
{NULL, NULL, NULL}
};
@ -3185,6 +3196,16 @@ static void disable_module_unloading(const char* arg)
unload_modules_at_exit = false;
}
static void enable_statement_logging(const char* arg)
{
mxs_mysql_set_log_statements(true);
}
static void disable_statement_logging(const char* arg)
{
mxs_mysql_set_log_statements(false);
}
static void redirect_output_to_file(const char* arg)
{
if (arg)

View File

@ -32,6 +32,21 @@
#include <maxscale/debug.h>
#include <maxscale/log_manager.h>
namespace
{
struct THIS_UNIT
{
bool log_statements; // Should all statements sent to server be logged?
};
static THIS_UNIT this_unit =
{
false
};
}
/**
* @brief Calculate the length of a length-encoded integer in bytes
*
@ -221,6 +236,19 @@ int mxs_mysql_query(MYSQL* conn, const char* query)
rc = mysql_query(conn, query);
}
if (this_unit.log_statements)
{
const char* host;
if (mariadb_get_info(conn, MARIADB_CONNECTION_HOST, &host) != 0)
{
// No idea about the host, but let's use something that looks like
// an IP-address as a placeholder.
host = "0.0.0.0";
}
MXS_NOTICE("SQL(%s): %d, \"%s\"", host, rc, query);
}
return rc;
}
@ -368,3 +396,13 @@ void mxs_mysql_set_server_version(MYSQL* mysql, SERVER* server)
server_set_version(server, version_string, version);
}
}
void mxs_mysql_set_log_statements(bool enable)
{
this_unit.log_statements = enable;
}
bool mxs_mysql_get_log_statements()
{
return this_unit.log_statements;
}