没有在你的环境中初始化log4j

解决方案 »

  1.   

    没有
    org.apache.log4j.*;
    服务器
    了知道就搞不知道就的等
    -------------------------------
    啥时候结束郁闷
      

  2.   

    把你的log4j.props文件读出来!system.log4j.configfile=log4j.props
    我是这样做的 把system.log4j.configfile=log4j.props 这句放到一个log.props的文件里。
    用线程初始化!其实就是把log4j.props
    的所有的东西都读到Properties对象里!随时备用!·
      

  3.   

    meizizi(怪怪) :
       具体怎么操作,能否教我?
      

  4.   

    读配置文件的类:
    import java.io.*;
    import java.util.*;/**
     * 此配置文件可以动态的将改动的配置文件更新。
     * 对多个系统可设置不同的属性配置文件 。
     * 是用一个config.props映射文件来完成这个功能的。
     * 这个文件有这样一个格式: 
     * <blockquote>
     * <pre>  
     * systemName1.1=filename1
     * systemName1.2=filename2 
     * systemName2.1=filename3
     * systemName2.2=filename4
     * </pre>
     * </blockquote>
     *
     * 在上边的映射文件中 systemName1 和 systemName2 是两个系统,每个有两个属性配置文件
     * filename1和filename2 是systemName1 的不同的两个属性配置文件。
     * filename3和filename4 是systemName2 的不同的两个属性配置文件。
     * 
     * 每个属性配置文件可以动态的更新,但映射文件不能动态更新。
     */
    public class ConfigManager {
        private static final Hashtable instances = new Hashtable();
        private static final String mappingFileName;
        private static final Properties mapping = new Properties();
        private static PropertiesFile mappingfile;
        private static final long interval = 60000;
        private static Monitor monitor = new Monitor();    private final String systemName;
        private final Properties properties = new Properties();
        private final PropertiesFile[] files;    static {
            for (Enumeration e = System.getProperties().keys(); e.hasMoreElements(); ) {
                String key = (String)e.nextElement();
                System.out.println(key + "=" + System.getProperty(key));
            }
            //"config.props" 是一个默认值
            
            mappingFileName = System.getProperty("config.file", "config.props");
            monitor.start();
        }/**
     *得到一个默认的配置
     */
        public static ConfigManager getInstance() throws InvalidConfigException {
            return getInstance("default");
        }/**
     * 根据给定的系统名得到一个配置。 
     * 如果映射文件或者不能装入文件将会抛出一个运行期的异常。
     * @param 系统名称.
     */
        public static ConfigManager getInstance(String systemName) throws InvalidConfigException {
            if (mappingfile == null) {
                initMapping();
            }
            ConfigManager instance = (ConfigManager)instances.get(systemName);
            if (instance == null) {
                initInstance(systemName);
                instance = (ConfigManager)instances.get(systemName);
            }
            return instance;
        }/**
     * 
     * 装入映射文件,再装入失败后,下次在进行配置申请的时候,可再次装入,,不依赖服务器的启动。
     * 
     */
        private static void initMapping() throws InvalidConfigException {
            synchronized(mapping) {
                if (mappingfile == null) {
                 //System.out.println("**************mappingFileName***************** : "+ mappingFileName);
                    mappingfile = new PropertiesFile(mappingFileName);
                    mapping.clear();
                    mappingfile.load(mapping);
                }
            }
        }/**
     * 
     *创建一个配置的实例,并把它放到缓存中
     * 
     * @param 系统名
     */
        private static void initInstance(String systemName) throws InvalidConfigException {
            synchronized(instances) {
                if (instances.get(systemName) == null) {
                    Vector v = new Vector();
                    for (int i = 1; true; i++) {
                     //System.out.println("The SystemName is : "+ systemName + "." + i);
                        String filename = mapping.getProperty(systemName + "." + i);
                        //System.out.println("The fileName is : "+ filename);
                        if (filename != null) {
                            PropertiesFile file = new PropertiesFile(filename);
                            v.add(file);
                        } else {
                            break;
                        }
                    }
                    if (v.size() == 0) {
                        LogManager.getInstance("system.util").logError("ConfigManager: Cannot get the property file for" + systemName);
                        new Throwable().printStackTrace();
                        throw new InvalidConfigException("ConfigManager: Cannot get the property file for " + systemName);
                    }
                    PropertiesFile[] files = new PropertiesFile[v.size()];
                    v.toArray(files);
                    instances.put(systemName, new ConfigManager(systemName, files));
                }
            }
        }
        private ConfigManager(String systemName, PropertiesFile[] files) {
            this.systemName = systemName;
            this.files = files;
            this.reload();
        }
        public String getProperty(String key) {
            return this.properties.getProperty(key);
        }    public String getProperty(String key, String def) {
            return this.properties.getProperty(key, def);
        }
        public Enumeration propertyNames() {
             return this.properties.propertyNames();
        }/**
     * 
     * 检查映射文件和属性配置文件是否改动,如果改动,则清除是实例,从新装入。
     * 
     */
        protected static void refreshAll() {
            synchronized(mapping) {
                if (mappingfile != null && mappingfile.isModified()) {
                    mapping.clear();
                    mappingfile.load(mapping);
                    synchronized(instances) {
                        instances.clear();
                    }
                }
            }
            synchronized(instances) {
                for (Enumeration e = instances.elements(); e.hasMoreElements(); ) {
                    ((ConfigManager)e.nextElement()).refresh();
                }
            }
        }    protected void refresh() {
            for (int i = 0; i < this.files.length; i++) {
                if (this.files[i].isModified()) {
                    this.reload();
                    return;
                }
            }
        }    private void reload() {
            System.out.println("ConfigManager: [" + this.systemName + "] reload.");
            synchronized(properties) {
                properties.clear();
                for (int i = 0; i < this.files.length; i++) {
                    this.files[i].load(properties);
                }
            }
        }
      

  5.   

    接上
        static class Monitor implements Runnable {
            private Thread thread = null;
            public void start() {
                if (this.thread == null) {
                    this.thread = new Thread(this, "ConfigManager Monitor");
                }
                this.thread.start();
            }        public void stop() {
                this.thread = null;
            }        public void run() {
                while (this.thread == Thread.currentThread()) {
                    refreshAll();
                    try {
                        this.thread.sleep(interval);
                    } catch (InterruptedException e) {}
                }
            }
        }    static class PropertiesFile {
            private final String filename;
            private long lastModified = 0;        PropertiesFile(String filename) {
                this.filename = filename;
            }        protected synchronized boolean isModified() {
                return (new File(this.filename).lastModified() != this.lastModified);
            }        protected synchronized void load(Properties properties) {
                try {
                 System.out.println("load() the file name is :  "+ filename);
             //String path= new File("").getAbsolutePath();
                    File file = new File(this.filename);
                    //System.out.println(" the file if here or not ssssssssddddddd"+path);
                    
                    InputStream in = null;
                    try {
                      //in = this.getClass().getResourceAsStream("/com/philwong/fortress/apps/system/" + this.filename);
                        in = new java.io.FileInputStream(file);
                        properties.load(in);
                    }catch(Exception e ) {
                      LogManager.getInstance("system.util").logError("ConfigManager: Cannot properties.load(in)");
                     //System.out.println("ConfigManager: Cannot properties.load(in)");
                    } finally {
                        try {
                            if (in != null) in.close();
                        } catch (Exception e) {}
                    }
                    this.lastModified = file.lastModified();
                } catch (Exception e) {
                 LogManager.getInstance("system.util").logError("ConfigManager: Cannot load " + this.filename);
                    System.out.println("ConfigManager: Cannot load " + this.filename);
                    e.printStackTrace();
                }
            }
        }
    }
      

  6.   

    配置文件的路径你要自己确定,可以hardcode!