{{ v.name }}
{{ v.cls }}类
{{ v.price }} ¥{{ v.price }}
从最初学习使用log4j的时候,网上和书本上主要都是使用“log4j.properties”这种属性格式,配置日志。多年以来,一直使用这种格式,总的来说,简单、够用。而有十多年经验的boss,不建议使用properties格式配置,而是用xml格式配置。boss之前在阿里(支付宝、淘宝)、uc等大公司工作过。
我们有个很明显的不同:我比较注重,简单、快速。boss比较注重,规范、严谨。我的观点:没有对与错,只有适用与不适用。每个人都是根据自己的经历和追求,做出的技术选择。对于技术使用者来讲,明白不同配置的好处和坏处,才是需要注意的。就log4j.xml这种配置来说,功能确实可能更多一些,可以单独把业务日志和普通的系统日志分离,运维过程中,很容易看到业务错误,从而更快的解决问题。------------------------------------------------------------------------------------------------------------另外,boss觉得需要把log4j的输出目录配置成变量。比如:
log4j配置可在web.xml中配置log4j.xml的位置,参数名称为:log4jxmlpath。
也可以,配置log4j的日志输出位置的目录,参数名称为:log4joutputpath。
如果log4joutputpath没有值,或者对应的文件不存在,将把classpath下的log4j文件夹作为默认的输出目录。
publicclasslog4jinitextendshttpservlet{privatestaticfinallongserialversionuid=1l;publicvoidinit(servletconfigconfig)throwsservletexception{//从web.xml中找到log4j的输出目录stringlog4joutputpath=config.getinitparameter("log4joutputpath");//默认的日志输出位置if(stringutils.isblank(log4joutputpath)){log4joutputpath=log4jinit.class.getclassloader().getresource("").getfile()+"/log4j";}filelog4joutputpathfile=newfile(log4joutputpath);//如果输出文件不存在,手动创建booleanlog4xmlfileexists=log4joutputpathfile.exists();if(!log4xmlfileexists){system.out.println(log4joutputpathfile.mkdirs());}//log4j.xml文件中的变量是在这里设置的system.setproperty("log4joutputpath",log4joutputpath);//从web.xml中找到log4j.xml的输出目录stringlog4jxmlpath=config.getinitparameter("log4jxmlpath");booleanexist=false;//如果在web.xml手动配置,log4jxmlpath应该使用绝对地址,否则,就使用默认的位置和文件名就行if(stringutils.isnotblank(log4jxmlpath)){filefile=newfile(log4jxmlpath);exist=file.exists();}//log4jxmlpath默认位于classpath下log4j.xmlif(!exist){urlresource=log4jinit.class.getclassloader().getresource("log4j.xml");if(resource!=null){log4jxmlpath=resource.getfile();}}domconfigurator.configure(log4jxmlpath);}}