我正在使用slf4j与log4j 2.0或logback作为实现.例如,我的servlet有一个级别为ERROR的记录器,我的服务器产生了servlet的100个线程.我将在运行时获得一个特殊用户列表.当我检测到一些连接的特殊用户时.我想将这些特殊用户/线程的日志级别更改为DEBUG,并使其他线程的日志级别不受影响(仍然是ERROR).
我知道logback中的TurboFilter和log4j 2.0中的DynamicThresholdFilter,但由于我只在运行时获取特殊用户列表,所以我无法使用它们.
这是我的申请:
package com.example.logging;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import org.slf4j.*;
public class App extends HttpServlet {
private final Logger Logger = LoggerFactory.getLogger(App.class);
Map<String, String> map = new HashMap<String, String>();
public App() {
map.put("user1", "DEBUG");
map.put("user2", "DEBUG");
map.put("user3", "ERROR");
}
public void writeToLogFile(String userName) {
if (map.containsKey(userName)) {
// do something so that I can change the logger to the corresponding log level
}
Logger.error(userName + " error message");
// the logger is of level ERROR, …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置slf4j来拦截所有日志记录语句,然后根据特定条件以编程方式添加处理程序.我的代码是:
private void init()
{
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
if(condition1)
appendHandler(console, Level.DEBUG);
if(condition2)
appendHandler(logfile1, Level.INFO);
...
}
Run Code Online (Sandbox Code Playgroud)
如何编写appendHandler方法代码?我花了几个小时试图阅读文档,但找不到解决方案.有很多关于如何在配置文件中执行但不在代码中的参考.
我也是正确的,因为这段代码拦截了所有不同日志框架的所有日志记录语句?
在编写此查询之前,我查看了类似的查询:
我觉得我的查询听起来很相似但不一样.
您是否曾在请求输入时遇到日志消息分组,并在响应关闭时刷新它们.即预期的操作顺序如下:
我正在寻找目前可用的log4j实现的这种可能性.
请分享您的意见.