动态修改log4j日志级别

时间:2021-06-19 21:51:47
  • 设置包的日志等级

Level level = Level.toLevel(Level.DEBUG);
Logger logger = LogManager.getLogger(“package”);
logger.setLevel(level);

  • 设置全局日志等级

Level level = Level.toLevel(Level.DEBUG);
LogManager.getRootLogger().setLevel(level);

  • http控制

@Path("/conf")
@Component("configurationResource")
public class ConfigurationResource {
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/log/package/{package}/{level}")
public Response index(@PathParam("package") String p, @PathParam("level") String l) {
Level level = Level.toLevel(l);
Logger logger = LogManager.getLogger(p);
logger.setLevel(level);
return Response.ok().build();
}

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/log/root/{level}")
public Response index(@PathParam("level") String l) {
Level level = Level.toLevel(l);
LogManager.getRootLogger().setLevel(level);
return Response.ok().build();
}

}