替换登录中的多个值 [英] Replacing multiple values in logback

查看:84
本文介绍了替换登录中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换Logback记录器中的多个值,该记录器记录了Cassandra CQL语句并使用了本文中的示例: 通过logback屏蔽日志中的敏感数据

I am trying to replace multiple values in logback logger which logs Cassandra CQL statements and used example from this post: Mask sensitive data in logs with logback

使用%replace函数的调用:

where invocation of %replace function is used:

%replace(  %replace(%msg){'regex1', 'replacement1'}  ){'regex2', 'replacement2'}

就我而言,我想替换3个字段-名称,姓氏和密码.使用的语句是:

In my case I want to replace 3 fields - name, last name and password. Statement used is:

%replace(%replace(%replace(%msg){"first_name='.*'", "first_name='xxxxx'"}){"last_name='.*'", "last_name='yyyyyy'"}){"password='.*'", "password='zzzzzz'"}%n

这似乎正常,但我注意到,如果字段顺序不同,则有时会删除值.例如

It seemed to work ok, but I noticed that if order of fields is different, values are removed sometimes. For example,

1)语句按此顺序可以:

1) when statement is in this order it is ok:

已执行:

UPDATE usertest.users SET password='secret_pw', last_name='Smith', first_name='John' where user_id = 1745;

已记录:

UPDATE usertest.users SET password=zzzzzz, last_name=yyyyyy, first_name=xxxxx where user_id = 1745;

2)在这种情况下,姓氏被删除了

2) In this case last name is removed

已执行:

UPDATE usertest.users SET password='secret_pw', first_name='John', last_name='Smith' where user_id = 1745;

已记录:

UPDATE usertest.users SET password=zzzzzz, first_name=xxxxx where user_id = 1745;

3)在这种情况下,密码已删除

3) In this case password is removed

已执行:

UPDATE usertest.users SET last_name='Smith', password='secret_pw', first_name='John' where user_id = 1745;

已记录:

UPDATE usertest.users SET last_name=yyyyyy, first_name=xxxxx where user_id = 1745;

有人可以建议为什么会发生这种情况以及如何解决它,或者还有其他解决方法吗?

Could someone advice why it could happen and how it could be fixed or is there any other way to solve?

推荐答案

这是正则表达式模式拾取的次数超出预期的问题,因此替换操作会相互覆盖.

This is an issue with the regex pattern picking up more than you intend it to, so the replaces are overwriting each other.

我复制了您看到的问题,然后将正则表达式更改为仅包含字母数字(\ w而不是.),因此模式如下所示:

I reproduced the issue you saw, and then changed the regex to include just alphanumerics (\w instead of .) so the pattern looks like this:

%replace(  %replace(  %replace(%msg){"first_name='\w*'", "first_name='xxxxx'"}  ){"last_name='\w*'", "last_name='yyyyyy'"}  ){"password='\w*'", "password='zzzzzz'"}%n

以下是一些测试:

UPDATE usertest.users SET password='secret_pw', last_name='Smith', first_name='John' where user_id = 1745;
UPDATE usertest.users SET last_name='Smith', first_name='John', password='secret_pw' where user_id = 1745;
UPDATE usertest.users SET first_name='John', password='secret_pw', last_name='Smith' where user_id = 1745;
UPDATE usertest.users SET first_name='John', last_name='Smith', password='secret_pw' where user_id = 1745;

记录结果:

Received: QUERY UPDATE usertest.users SET password='zzzzzz', last_name='yyyyyy', first_name='xxxxx' where user_id = 1745;[pageSize = 100], v=4/v4
Received: QUERY UPDATE usertest.users SET last_name='yyyyyy', first_name='xxxxx', password='zzzzzz' where user_id = 1745;[pageSize = 100], v=4/v4
Received: QUERY UPDATE usertest.users SET first_name='xxxxx', password='zzzzzz', last_name='yyyyyy' where user_id = 1745;[pageSize = 100], v=4/v4
Received: QUERY UPDATE usertest.users SET first_name='xxxxx', last_name='yyyyyy', password='zzzzzz' where user_id = 1745;[pageSize = 100], v=4/v4

这篇关于替换登录中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆