Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

java - How to configure OpenEJB logging?

How can I configure OpenEJB logging format? This is what what I see now in logs:

[...]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 sec
Running com.XXX.FooTest
Apache OpenEJB 3.1.3    build: 20101015-05:42
http://openejb.apache.org/
INFO - openejb.home = /code/XXX
INFO - openejb.base = /code/XXX
INFO - Configuring Service(id=Default Security Serv...
[...]

I would like to disable INFO messages, and change formatting of others. Changes in log4j.properties have no effect.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Keep in mind that the overriding ability you get with the OpenEJB logger works with system properties as well as InitialContext properties.

The openejb.logger.external property is really aimed at servers integrating OpenEJB, such as Geronimo, who are using different logging systems and need advanced control over logging. It is not designed for common use as with this option enabled and no other steps taken, you get no logging of any kind, not even ERROR, and no info on failed deployments. Even correct usage will still disable all options discussed below.

If the desire is to get the logging configuration in or out of the test there are many ways to do it without losing any logging functionality OpenEJB provides.

Option 1: in code via InitialContext properties

In the test case itself via InitialContext properties

Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

p.put("log4j.rootLogger", "fatal,C");
p.put("log4j.category.OpenEJB", "warn");
p.put("log4j.category.OpenEJB.options", "warn");
p.put("log4j.category.OpenEJB.server", "warn");
p.put("log4j.category.OpenEJB.startup", "warn");
p.put("log4j.category.OpenEJB.startup.service", "warn");
p.put("log4j.category.OpenEJB.startup.config", "warn");
p.put("log4j.category.OpenEJB.hsql", "warn");
p.put("log4j.category.CORBA-Adapter", "warn");
p.put("log4j.category.Transaction", "warn");
p.put("log4j.category.org.apache.activemq", "error");
p.put("log4j.category.org.apache.geronimo", "error");
p.put("log4j.category.openjpa", "warn");
p.put("log4j.appender.C", "org.apache.log4j.ConsoleAppender");
p.put("log4j.appender.C.layout", "org.apache.log4j.SimpleLayout");
p.put("openejb.nobanner", "false");

Context context = new InitialContext(p);

Option 2: a jndi.properties file

File must be in the classpath at any path that evaluates to "/jndi.properties", so not "/META-INF/jndi.properties"

In Maven this can be done by placing the file at src/test/resources/jndi.properties

log4j.rootLogger                   = fatal,C
log4j.category.OpenEJB             = warn
log4j.category.OpenEJB.options     = warn
log4j.category.OpenEJB.server      = warn
log4j.category.OpenEJB.startup     = warn
log4j.category.OpenEJB.startup.service = warn
log4j.category.OpenEJB.startup.config = warn
log4j.category.OpenEJB.hsql        = warn
log4j.category.CORBA-Adapter       = warn
log4j.category.Transaction         = warn
log4j.category.org.apache.activemq = error
log4j.category.org.apache.geronimo = error
log4j.category.openjpa             = warn
log4j.appender.C                   = org.apache.log4j.ConsoleAppender
log4j.appender.C.layout            = org.apache.log4j.SimpleLayout
openejb.nobanner = false

Here is a short video of the above option in action.

Note that finding and reading the jndi.properties file is a functionality of the java vm so if it doesn't work, it is more likely to be a configuration issue rather than a vm bug.

Option 3: Maven Surefire config

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <systemPropertyVariables>
      <log4j.rootLogger>fatal,C</log4j.rootLogger>
      <log4j.category.OpenEJB>warn</log4j.category.OpenEJB>
      <log4j.category.OpenEJB.options>warn</log4j.category.OpenEJB.options>
      <log4j.category.OpenEJB.server>warn</log4j.category.OpenEJB.server>
      <log4j.category.OpenEJB.startup>warn</log4j.category.OpenEJB.startup>
      <log4j.category.OpenEJB.startup.service>warn</log4j.category.OpenEJB.startup.service>
      <log4j.category.OpenEJB.startup.config>warn</log4j.category.OpenEJB.startup.config>
      <log4j.category.OpenEJB.hsql>warn</log4j.category.OpenEJB.hsql>
      <log4j.category.CORBA-Adapter>warn</log4j.category.CORBA-Adapter>
      <log4j.category.Transaction>warn</log4j.category.Transaction>
      <log4j.category.org.apache.activemq>error</log4j.category.org.apache.activemq>
      <log4j.category.org.apache.geronimo>error</log4j.category.org.apache.geronimo>
      <log4j.category.openjpa>warn</log4j.category.openjpa>
      <log4j.appender.C>org.apache.log4j.ConsoleAppender</log4j.appender.C>
      <log4j.appender.C.layout>org.apache.log4j.SimpleLayout</log4j.appender.C.layout>
      <openejb.nobanner>false</openejb.nobanner>
    </systemPropertyVariables>
  </configuration>
</plugin>

Option 4: any combination

Also note that all of the above techniques can be used at once, including any overrides you wish to put in individual test cases. The order of precedence is as follows:

  1. InitialContext properties
  2. jndi.properties in classpath
  3. system propertes (in this case, setup via surefire)
  4. embedded.logging.properties in classpath

Option 5: Request a feature

As always we are very happy to make things easier in any way we can. If you have a specific need or idea, we're happy to try and work it in or help you do it should you want to contribute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...