//log4j不妥当的写法
public class Xml2DataParserImpl {
private final Log log = LogFactory.getLog(Xml2DataParserImpl.class);
}
妥当的写法
public class Xml2DataParserImpl {
private static final Log log = LogFactory.getLog(Xml2DataParserImpl.class);
}
实际上, 如果class被实例化的次数比较小的时候, 这个对性能影响也不大。 一个小小的命令可以检查:
find . |grep -e "\.java$" |xargs grep "LogFactory.getLog" |grep -v static (commons log4j)或者 find . |grep -e "\.java$" |xargs grep "LoggerFactory.getLogger" |grep -v static (slf4j)
--摘自工作中线上性能方面
本文探讨了Log4j在实际应用中常见的不当使用方式及其对性能的影响,并提供了一种更优的解决方案。通过对比静态与非静态日志对象实例化的方式,说明了静态变量在提高应用程序性能方面的优势。
1862

被折叠的 条评论
为什么被折叠?



