Skip to content

Commit 49b7dc9

Browse files
jackyspzz-jason
authored andcommitted
*: add a variable tidb_query_log_max_len to set the max length of the query string in the log dynamically (#8183) (#8200)
1 parent 89140e2 commit 49b7dc9

File tree

8 files changed

+24
-5
lines changed

8 files changed

+24
-5
lines changed

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type Log struct {
8989
SlowQueryFile string `toml:"slow-query-file" json:"slow-query-file"`
9090
SlowThreshold uint64 `toml:"slow-threshold" json:"slow-threshold"`
9191
ExpensiveThreshold uint `toml:"expensive-threshold" json:"expensive-threshold"`
92-
QueryLogMaxLen uint `toml:"query-log-max-len" json:"query-log-max-len"`
92+
QueryLogMaxLen uint64 `toml:"query-log-max-len" json:"query-log-max-len"`
9393
}
9494

9595
// Security is the security section of the config.
@@ -275,7 +275,7 @@ var defaultConf = Config{
275275
},
276276
SlowThreshold: logutil.DefaultSlowThreshold,
277277
ExpensiveThreshold: 10000,
278-
QueryLogMaxLen: 2048,
278+
QueryLogMaxLen: logutil.DefaultQueryLogMaxLen,
279279
},
280280
Status: Status{
281281
ReportStatus: true,

executor/adapter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ func (a *ExecStmt) LogSlowQuery(txnTS uint64, succ bool) {
347347
return
348348
}
349349
sql := a.Text
350-
if len(sql) > int(cfg.Log.QueryLogMaxLen) {
351-
sql = fmt.Sprintf("%.*q(len:%d)", cfg.Log.QueryLogMaxLen, sql, len(a.Text))
350+
if maxQueryLen := atomic.LoadUint64(&cfg.Log.QueryLogMaxLen); uint64(len(sql)) > maxQueryLen {
351+
sql = fmt.Sprintf("%.*q(len:%d)", maxQueryLen, sql, len(a.Text))
352352
}
353353
sessVars := a.Ctx.GetSessionVars()
354354
sql = QueryReplacer.Replace(sql) + sessVars.GetExecuteArgumentsInfo()

executor/set_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ func (s *testSuite) TestSetVar(c *C) {
241241
tk.MustQuery("select @@session.tidb_slow_log_threshold;").Check(testkit.Rows("1"))
242242
_, err = tk.Exec("set global tidb_slow_log_threshold = 0")
243243
c.Assert(err, NotNil)
244+
245+
tk.MustExec("set tidb_query_log_max_len = 0")
246+
tk.MustQuery("select @@session.tidb_query_log_max_len;").Check(testkit.Rows("0"))
247+
tk.MustExec("set tidb_query_log_max_len = 20")
248+
tk.MustQuery("select @@session.tidb_query_log_max_len;").Check(testkit.Rows("20"))
249+
_, err = tk.Exec("set global tidb_query_log_max_len = 20")
250+
c.Assert(err, NotNil)
244251
}
245252

246253
func (s *testSuite) TestSetCharset(c *C) {

sessionctx/variable/session.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
588588
atomic.StoreUint32(&ProcessGeneralLog, uint32(tidbOptPositiveInt32(val, DefTiDBGeneralLog)))
589589
case TiDBSlowLogThreshold:
590590
atomic.StoreUint64(&config.GetGlobalConfig().Log.SlowThreshold, uint64(tidbOptInt64(val, logutil.DefaultSlowThreshold)))
591+
case TiDBQueryLogMaxLen:
592+
atomic.StoreUint64(&config.GetGlobalConfig().Log.QueryLogMaxLen, uint64(tidbOptInt64(val, logutil.DefaultQueryLogMaxLen)))
591593
case TiDBRetryLimit:
592594
s.RetryLimit = tidbOptInt64(val, DefTiDBRetryLimit)
593595
case TiDBDisableTxnAutoRetry:

sessionctx/variable/sysvar.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ var defaultSysVars = []*SysVar{
662662
/* The following variable is defined as session scope but is actually server scope. */
663663
{ScopeSession, TiDBGeneralLog, strconv.Itoa(DefTiDBGeneralLog)},
664664
{ScopeSession, TiDBSlowLogThreshold, strconv.Itoa(logutil.DefaultSlowThreshold)},
665+
{ScopeSession, TiDBQueryLogMaxLen, strconv.Itoa(logutil.DefaultQueryLogMaxLen)},
665666
{ScopeSession, TiDBConfig, ""},
666667
{ScopeGlobal | ScopeSession, TiDBDDLReorgWorkerCount, strconv.Itoa(DefTiDBDDLReorgWorkerCount)},
667668
{ScopeSession, TiDBDDLReorgPriority, "PRIORITY_LOW"},

sessionctx/variable/tidb_vars.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ const (
9898
// tidb_slow_log_threshold is used to set the slow log threshold in the server.
9999
TiDBSlowLogThreshold = "tidb_slow_log_threshold"
100100

101+
// tidb_query_log_max_len is used to set the max length of the query in the log.
102+
TiDBQueryLogMaxLen = "tidb_query_log_max_len"
103+
101104
// tidb_retry_limit is the maximum number of retries when committing a transaction.
102105
TiDBRetryLimit = "tidb_retry_limit"
103106

sessionctx/variable/varsutil.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ func GetSessionOnlySysVars(s *SessionVars, key string) (string, bool, error) {
8787
return mysql.Priority2Str[mysql.PriorityEnum(atomic.LoadInt32(&ForcePriority))], true, nil
8888
case TiDBSlowLogThreshold:
8989
return strconv.FormatUint(atomic.LoadUint64(&config.GetGlobalConfig().Log.SlowThreshold), 10), true, nil
90+
case TiDBQueryLogMaxLen:
91+
return strconv.FormatUint(atomic.LoadUint64(&config.GetGlobalConfig().Log.QueryLogMaxLen), 10), true, nil
9092
}
9193
sVal, ok := s.systems[key]
9294
if ok {
@@ -327,7 +329,9 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
327329
TIDBMemQuotaIndexLookupReader,
328330
TIDBMemQuotaIndexLookupJoin,
329331
TIDBMemQuotaNestedLoopApply,
330-
TiDBRetryLimit, TiDBSlowLogThreshold:
332+
TiDBRetryLimit,
333+
TiDBSlowLogThreshold,
334+
TiDBQueryLogMaxLen:
331335
_, err := strconv.ParseInt(value, 10, 64)
332336
if err != nil {
333337
return value, ErrWrongValueForVar.GenWithStackByArgs(name)

util/logutil/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const (
3535
defaultLogLevel = log.InfoLevel
3636
// DefaultSlowThreshold is the default slow log threshold in millisecond.
3737
DefaultSlowThreshold = 300
38+
// DefaultQueryLogMaxLen is the default max length of the query in the log.
39+
DefaultQueryLogMaxLen = 2048
3840
)
3941

4042
// FileLogConfig serializes file log related config in toml/json.

0 commit comments

Comments
 (0)