项目本来是按日志进行分割,但是后续每天产生的日志太多,打开都很不方便,怎么在按日期分割的基础上再按大小进行分割呢?出了自己写个RollingFileAppender类还有没有其他办法?

解决方案 »

  1.   

    ,dom4j在日志分类上面做的很好。但要涉及到灵活修改该文件的时候就。对于这个问题,实在是让人头大,,
    我的项目遇到需要同时使用多个log4j配置文件的问题,,,类似你的问题,弄了大半月最后还是,自己动手写了个Log4JInit类,在应用启动的时候创建log4j.xml。
    哥们你别找答案了没有的。。
      

  2.   

    写一个log4jInit吧,,在你的log4j里面设置几个变量,例如${some_day_path},然后使用apache有个项目叫configuration来替换你的变量。
    看看我的,对你有帮助否:/**
    * @Synopsis  读取多个log4j配置文件合并成一个。main-log4j.xml为主配置文件。只有主配置文件中的root与root appender
    * 才生效。
    */
    public class LoggingInit{
        private static final String CONFIG_FILE_NAME_PATTERN = "^(?!log4j)[a-zA-Z0-9]+.xml";
        private static final String LOG4J_FILE_NAME = "log4j.xml";
        private static File config_file = null;
        /**
        * @Synopsis  configure 
        * @Param File _file 
        * @Return 
        */
        public static void init(){
            LoggingInit init = new LoggingInit();
            File file = init.combined();
            config_file = init.modulate( file );
            DOMConfigurator.configure( config_file.getPath() );
        }    /**
        * @Synopsis  getConfigFile 
        * @Return 
        */
        public static File getConfigFile(){
            return config_file;
        }    /**
        * @Synopsis  replace path variables
        */
        class RealPath extends StrLookup{
            @Override
            public String lookup( String _key ){
                if( _key.trim().equals( "log_dir" ) ){
                    return Path.singleInstance().getLogPath();
                }
                return "not-found";
            }
        }
        /**
        * @Synopsis  combined 
        * @Return 
        */
        public File combined(){
            File out = new File( Path.singleInstance().getLogConfPath() + File.separator + LOG4J_FILE_NAME );
            try{
                NodeCombiner node_combiner = new MergeCombiner();
                node_combiner.addListNode( "log4j:configuration" );
                CombinedConfiguration combined = new CombinedConfiguration( node_combiner );            File dir = new File( Path.singleInstance().getLogConfPath() );
                IOFileFilter file_filter = new RegexFileFilter( CONFIG_FILE_NAME_PATTERN );
                IOFileFilter directory_filter = TrueFileFilter.INSTANCE;
                Collection< File > files = FileUtils.listFiles( dir, file_filter, directory_filter );
                for( File f : files ){
                    XMLConfiguration c = new XMLConfiguration( f );
                    combined.addConfiguration( c );
                }            XMLConfiguration configuration = new XMLConfiguration( combined );
                configuration.setPublicID( "-//log4j/log4j Configuration//EN" );
                configuration.setSystemID( "log4j.dtd" );
                configuration.setRootElementName( "log4j:configuration" );            ConfigurationInterpolator interpolator = configuration.getInterpolator();
                interpolator.registerLookup( "path", new RealPath() );(按你的规则替换成你的日志路劲)            XMLConfiguration rs = ( XMLConfiguration ) configuration.interpolatedConfiguration();            rs.save( out );
            }catch( ConfigurationException e ){
                throw new InitException( "combined exection", e );
            }
            return out;
        }
        /**
        * @Synopsis  modulate 
        * @Param File _file 
        * @Return 
        */
        public File modulate( File _file ){
            try{
                SAXReader reader = new SAXReader();
                Document dom = reader.read( _file );            Element configuration = ( Element ) dom.selectSingleNode( "/configuration" );
                List appender_list = configuration.selectNodes( "appender" );
                List logger_list = configuration.selectNodes( "logger" );
                Node root = configuration.selectSingleNode( "root" );            List< Element > elements = configuration.elements();
                elements.clear();
                for( Object object : appender_list ){
                    elements.add( ( Element ) object );
                }
                for( Object object : logger_list ){
                    elements.add( ( Element ) object );
                }
                elements.add( ( Element ) root );            OutputFormat format = OutputFormat.createPrettyPrint();
                format.setIndentSize( 4 );
                XMLWriter writer = new XMLWriter( new FileOutputStream( _file ), format );
                writer.write( dom );
                writer.close();
            }catch( DocumentException e ){
                throw new InitException( "fromat exception", e );
            }catch( FileNotFoundException e ){
                throw new InitException( "fromat exception", e );
            }catch( UnsupportedEncodingException e ){
                throw new InitException( "fromat exception", e );
            }catch( IOException e ){
                throw new InitException( "fromat exception", e );
            }
            return _file;
        }}