Glassfish 3 + ear + logback.xml

有些话、适合烂在心里 提交于 2019-12-23 10:03:04

问题


I'm using logback in an EAR-File which contains a JAR (ejb) and a WAR. This should run on a Glassfish v3 Server. Everything works, except the loading of the logback.xml. This can't be found. I build the Project with Netbeans. The used external libs are in the lib-Directory of the EAR (Which shouldn't make a difference where they are...). I've planed to put the logback.xml-File in the root-Directory or another Subdirectory in the EAR. The Classpath is set in the Manifest-Files of the JAR and WAR. But for some Reasons the logback.xml wasn't found... (The build ear contains the logback.xml ;) )

I've tryied every location of the logback.xml. Even in the WAR or JAR. Nothing worked...

If I use a standalone WAR then everything works fine and the logback.xml was found. (OK. Not everything. Changing the Classpath in the Manifest doesn't work...)

So my Question: Has anybody already get logback.xml to run within an EAR?

Here is my Manifest (I hope, that this ist the correct Syntax):

Manifest-Version: 1.0 
Ant-Version: Apache Ant 1.8.2 
Created-By: 1.7.0_147-icedtea-b147 (Oracle Corporation) 
Class-Path: ./ 

Hope someone can help me.

Regards


回答1:


I solved this problem creating a separated simple jar that I deploy exploded inside the EAR (using Maven and a separated module config.jar). In practice, the logback.xml was inserted in lib/config.jar/logback.xml




回答2:


I've found out a solution without putting another jar in the classpath. 1) Just put the logback.xml into the classpath of the war application (/src/java/ for instance); 2) Use a ServletContextListener to load the file using getResourceAsStream and, eventually, set some parameters (like the application name) as in the snipped below:

@Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Logback contextInitialized !!!!");
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); 
        JoranConfigurator jc = new JoranConfigurator(); 
        jc.setContext(context); context.reset(); 
        // override default configuration 
        // inject the name of the current application as "application-name" 
        // property of the LoggerContext 
        context.putProperty("application-name", "menu_dinamico"); 
        try { 
            InputStream is = getClass().getClassLoader().getResourceAsStream("logback.xml");
            if(is == null) {
                System.out.println("Logback xml file non trovato");
            }
            else {
                jc.doConfigure(is);
            }
        } catch (JoranException ex) {
            System.out.println("Logback contextInitialized error");
            StatusPrinter.print(context);
            ex.printStackTrace();
        }
    }

Now the file logback.xml is recognized.



来源:https://stackoverflow.com/questions/8668339/glassfish-3-ear-logback-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!