一、示例说明
通过该自定义日志工具类可以兼容和屏蔽项目或组件中对于log4j的不同配置文件(log4j2.xml、log4j.properties及log4j.xml)差异化自定义,设置打印公共的说明和依赖说明,主要依赖包如下:log4j-1.2-api-2.3.jar、log4j-api-2.3.jar、log4j-core-2.3.jar、commons-logging-1.2.jar及spring-core-3.2.4.RELEASE.jar
二、代码示例
如下Log4j2Utils解析log4j.xml配置文件,代码如下
import java.io.File;@b@import java.io.IOException;@b@import java.net.URI;@b@@b@public class Log4j2Utils {@b@ @b@ private static final String FQCN = org.apache.log4j.Logger.class.getName();@b@ @b@ public static org.apache.logging.log4j.spi.LoggerContext _getContext() { @b@ return org.apache.logging.log4j.LogManager.getFactory().getContext(FQCN, org.apache.log4j.Logger.class.getClassLoader()@b@ ,null,false);@b@ }@b@ @b@ @b@ public static void reconfigure(final URI configLocation){@b@ final org.apache.logging.log4j.core.LoggerContext ctx = (org.apache.logging.log4j.core.LoggerContext)_getContext();@b@ ctx.setConfigLocation(configLocation); @b@ @b@ }@b@ @b@ public static File resolveLocalConfigureFile() throws Exception{@b@ String config_dir=System.getProperty("xwood.log.home");@b@ if(config_dir==null || (config_dir=config_dir.trim()).length()==0){@b@ config_dir=System.getProperty("user.home");@b@ config_dir=config_dir+"/xwood-test/";@b@ }@b@ config_dir=config_dir+"/log4j";@b@ //@b@ File configDirectory=new File(config_dir);@b@ //----------------------------------------------------------------@b@ configDirectory.mkdirs();@b@ if(!configDirectory.exists()){@b@ throw new Exception("Directory:"+config_dir+" created failure,privliege error for user="+System.getProperty("user.name"));@b@ }@b@ if(!configDirectory.isDirectory()){@b@ throw new Exception("Directory:"+config_dir+" not be directory.");@b@ }@b@ File configFile=new File(configDirectory,"log4j.xml");@b@ if(configFile.exists()){@b@ if(!configFile.delete()){@b@ throw new Exception("File:"+configFile+" delete failure,privliege error for user="+System.getProperty("user.name"));@b@ }@b@ }@b@ try {@b@ if(!configFile.createNewFile()){@b@ throw new Exception("File:"+configFile+" create failure,privliege error for user="+System.getProperty("user.name"));@b@ }@b@ } catch (IOException e) {@b@ throw new Exception("File:"+configFile+" create failure,privliege error for user="+System.getProperty("user.name"));@b@ }@b@ return configFile;@b@ }@b@@b@}
主要使用工具类DefaultLogUtils,代码如下
import java.io.FileNotFoundException;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.springframework.util.ClassUtils;@b@import org.springframework.util.ResourceUtils;@b@@b@public class DefaultLogUtils {@b@@b@ private static final String LOG_NAME="XWOOD";@b@ @b@ private volatile static boolean isLog4j_2=true;@b@ @b@ static{@b@ try {@b@ ClassUtils.forName("org.apache.logging.log4j.Logger",ClassUtils.getDefaultClassLoader());@b@ } catch (Exception e) {@b@ isLog4j_2=false;@b@ }@b@ if(!defConfigFileEnable()){@b@ if(isLog4j2()){@b@ try {@b@ Log4j2Utils.reconfigure(ResourceUtils.toURI(getResourceName("log4j2.xml")));@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ }else{@b@ try {@b@ org.apache.log4j.PropertyConfigurator.configure(ResourceUtils.getURL(getResourceName("log4j.properties")));@b@ } catch (FileNotFoundException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ }@b@ }@b@ @b@ protected static boolean isLog4j2(){@b@ return isLog4j_2;@b@ }@b@ @b@ @b@ @b@ @b@ @b@ public static String getResourceName(String fileSimpleName){@b@ return "classpath:"+DefaultLogUtils.class.getPackage().getName().replaceAll("\\.", "/")+"/"+fileSimpleName;@b@ }@b@ @b@ @b@ @b@ public static Log getLogger(){@b@ return LogFactory.getLog(LOG_NAME);@b@ }@b@ @b@ protected static boolean defConfigFileEnable(){@b@ if(isLog4j2()){@b@ try {@b@ ResourceUtils.getFile("classpath:log4j2.xml");@b@ return true;@b@ } catch (FileNotFoundException e) {@b@ }@b@ }else{@b@ try {@b@ ResourceUtils.getFile("classpath:log4j.properties");@b@ return true;@b@ } catch (FileNotFoundException e) {@b@ }@b@ try {@b@ ResourceUtils.getFile("classpath:log4j.xml");@b@ return true;@b@ } catch (FileNotFoundException e) {}@b@ }@b@ @b@ return false;@b@ }@b@@b@ public static void main(String[] args) {@b@ DefaultLogUtils.getLogger().info(" 【测试模块】 logger test ... ");@b@ }@b@@b@}
log4j2.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>@b@<Configuration status="info">@b@ <Appenders>@b@ <Console name="CONSOLE" target="SYSTEM_OUT">@b@ <PatternLayout pattern="[xwood-test][%t][%d{HH:mm:ss}] [%-3p] %x %c{1}: %m%n" />@b@ </Console>@b@ <Async name="Async">@b@ <AppenderRef ref="CONSOLE"/> @b@ </Async>@b@ </Appenders>@b@@b@ <Loggers>@b@ <Root level="info" includeLocation="true">@b@ <AppenderRef ref="Async" />@b@ </Root>@b@ </Loggers>@b@</Configuration>
执行控制台结果
[xwood-test][main][13:00:20] [INFO] [] XWOOD: 【测试模块】 logger test ...