1

I added this to my projects POM:

    <dependency>
        <groupId>org.apache.onami.logging</groupId>
        <artifactId>org.apache.onami.logging.log4j2</artifactId>
        <version>3.4.0-incubating</version>
    </dependency>

And do @InjectLogger

@InjectLogger
Logger logger;

logger.info("Created"); // NPE here.

What could be the problem?

quarks
  • 33,478
  • 73
  • 290
  • 513

2 Answers2

1

You have to install the new Log4j2LoggingModule( Matchers.any() ). See

http://onami.incubator.apache.org/logging/log4j2.html

I personally prefer slf4j with logback but ymmv.

If you still get NPE:

Field injection happens after initialization block and constructor have run. Only after that will the fields be not null. If you reference field before then you get NPE. Method injection happens next after field injection. You can use method injection to simulate @PostConstruct or use a dedicated annotation such as in the onami-lifecycle.

@InjectLogger Logger log;

CtorInjectedStuff stuff;

@Inject Foo(CtorInjectedStuff stuff){
    this.stuff = stuff;
    log.info("created"); // null as fields are not yet injected
}     

@Inject public void postConstruct(){
    log.info("post construct"); // works
}

Sadly you cannot do

@Inject Foo(CtorInjectedStuff stuff, @InjectLogger Logger log){
    ...
}   

as custom injections work only after instance is created.

Note: The @InjectLogger support is implemented using the before mentioned custom injections via MembersInjector.

Imho: injecting the logger doesn't provide enough added value to warrant the extra hassle over plain simple static field but again ymmv.

Alen Vrečko
  • 886
  • 10
  • 13
  • I was in the impression that with Apache Onami, its very simple, no-configuration or what so ever is required: http://onami.incubator.apache.org/logging/index.html However it seems its quite harder than I have expected. – quarks May 30 '13 at 05:29
  • I've updated the answer. Hopefully it is clearer now what is going on. – Alen Vrečko May 30 '13 at 07:02
0

I am not sure if you have initialized your logger ApplicationContext or BeanPostProcessor. This link may be of some help to you:

Using java annotation to inject logger dependency

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Based on the Apache Onami doc: http://onami.incubator.apache.org/logging/index.html You only have to add the annotation and that's it? – quarks May 29 '13 at 03:08