Skip to content

Commit 90cd712

Browse files
MDEV-27087: Add thread ID and database / table, where the error occured
to SQL error plugin New plugin variable "with_db_and_thread_info" is added which prints the thread id and databse name to the logfile. the value is stored in variable "with_db_and_thread_info" log_sql_errors() is responsible for printing in the log. If detailed is enabled, print thread id and database name both, otherwise skip it.
1 parent 4ef9c9b commit 90cd712

File tree

6 files changed

+84
-2
lines changed

6 files changed

+84
-2
lines changed

mysql-test/include/read_head.inc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Purpose:
2+
# Print first LINES_TO_READ from a file.
3+
# The environment variables SEARCH_FILE and LINES_TO_READ must be set
4+
# before sourcing this routine.
5+
# Use:
6+
# When the test is slow ( example because of ASAN build) then it
7+
# may not flush the lines when 'cat' command is called and the
8+
# test could fail with missing lines. Hence this can be used to
9+
# to print first N lines.
10+
#
11+
12+
perl;
13+
14+
use strict;
15+
16+
my $search_file = $ENV{SEARCH_FILE} or die "SEARCH_FILE not set";
17+
my $lines_to_read = $ENV{LINES_TO_READ} or die "LINES_TO_READ not set";
18+
19+
open(FILE, '<', $search_file) or die "Can't open file $search_file: $!";
20+
21+
my $line_count = 0;
22+
while ($line_count < $lines_to_read and my $line = <FILE>)
23+
{
24+
print $line;
25+
$line_count++;
26+
}
27+
28+
close(FILE);
29+
30+
EOF
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
show variables like 'sql_error_log%';
2+
Variable_name Value
3+
sql_error_log_filename sql_errors.log
4+
sql_error_log_rate 1
5+
sql_error_log_rotate OFF
6+
sql_error_log_rotations 9
7+
sql_error_log_size_limit 1000000
8+
sql_error_log_with_db_and_thread_info ON
9+
set global sql_error_log_rate=1;
10+
select * from t_doesnt_exist;
11+
ERROR 42S02: Table 'test.t_doesnt_exist' doesn't exist
12+
THREAD_ID DATABASE_NAME TIME HOSTNAME ERROR 1146: Table 'test.t_doesnt_exist' doesn't exist : select * from t_doesnt_exist

mysql-test/suite/plugins/r/sql_error_log.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ sql_error_log_rate 1
88
sql_error_log_rotate OFF
99
sql_error_log_rotations 9
1010
sql_error_log_size_limit 1000000
11+
sql_error_log_with_db_and_thread_info OFF
1112
set global sql_error_log_rate=1;
1213
select * from t_doesnt_exist;
1314
ERROR 42S02: Table 'test.t_doesnt_exist' doesn't exist
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--plugin-load-add=$SQL_ERRLOG_SO --sql-error-log-with-db-and-thread-info=1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--source include/not_embedded.inc
2+
3+
if (!$SQL_ERRLOG_SO) {
4+
skip No SQL_ERROR_LOG plugin;
5+
}
6+
7+
show variables like 'sql_error_log%';
8+
set global sql_error_log_rate=1;
9+
--error ER_NO_SUCH_TABLE
10+
select * from t_doesnt_exist;
11+
12+
let $MYSQLD_DATADIR= `SELECT @@datadir`;
13+
--let SEARCH_FILE= $MYSQLD_DATADIR/sql_errors.log
14+
--let LINES_TO_READ=1
15+
--replace_regex /[1-9]* test [1-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [ 0-9][0-9]:[0-9][0-9]:[0-9][0-9] [^E]*/THREAD_ID DATABASE_NAME TIME HOSTNAME /
16+
--source include/read_head.inc
17+
18+
remove_file $MYSQLD_DATADIR/sql_errors.log;

plugin/sql_errlog/sql_errlog.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static unsigned int rate;
3939
static unsigned long long size_limit;
4040
static unsigned int rotations;
4141
static char rotate;
42+
static char with_db_and_thread_info;
4243

4344
static unsigned int count;
4445
LOGGER_HANDLE *logfile;
@@ -67,12 +68,19 @@ static MYSQL_SYSVAR_STR(filename, filename,
6768
"The file to log sql errors to", NULL, NULL,
6869
"sql_errors.log");
6970

71+
static MYSQL_SYSVAR_BOOL(with_db_and_thread_info, with_db_and_thread_info,
72+
PLUGIN_VAR_READONLY | PLUGIN_VAR_OPCMDARG,
73+
"Show details about thread id and database name in the log",
74+
NULL, NULL,
75+
0);
76+
7077
static struct st_mysql_sys_var* vars[] = {
7178
MYSQL_SYSVAR(rate),
7279
MYSQL_SYSVAR(size_limit),
7380
MYSQL_SYSVAR(rotations),
7481
MYSQL_SYSVAR(rotate),
7582
MYSQL_SYSVAR(filename),
83+
MYSQL_SYSVAR(with_db_and_thread_info),
7684
NULL
7785
};
7886

@@ -93,12 +101,24 @@ static void log_sql_errors(MYSQL_THD thd __attribute__((unused)),
93101

94102
count = 0;
95103
(void) localtime_r(&event_time, &t);
96-
logger_printf(logfile, "%04d-%02d-%02d %2d:%02d:%02d "
104+
if (with_db_and_thread_info)
105+
{
106+
logger_printf(logfile, "%llu %s %04d-%02d-%02d %2d:%02d:%02d "
107+
"%s ERROR %d: %s : %s \n",
108+
event->general_thread_id, event->database.str, t.tm_year + 1900,
109+
t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec,
110+
event->general_user, event->general_error_code,
111+
event->general_command, event->general_query);
112+
}
113+
else
114+
{
115+
logger_printf(logfile, "%04d-%02d-%02d %2d:%02d:%02d "
97116
"%s ERROR %d: %s : %s\n",
98117
t.tm_year + 1900, t.tm_mon + 1,
99118
t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec,
100119
event->general_user, event->general_error_code,
101120
event->general_command, event->general_query);
121+
}
102122
}
103123
}
104124
}
@@ -157,7 +177,7 @@ maria_declare_plugin(sql_errlog)
157177
0x0100,
158178
NULL,
159179
vars,
160-
"1.0",
180+
"1.1",
161181
MariaDB_PLUGIN_MATURITY_STABLE
162182
}
163183
maria_declare_plugin_end;

0 commit comments

Comments
 (0)