title: How to inject a Logger in an enum
body: <p>When I try to inject my Logger producer in an enum, I get a NPE. How can I inject a Logger in an enum?</p>
  
  <p>Example:</p>
  
  <pre><code>public enum MyEnum {
  
      HI("Hi there!"),
      HELLO("Hello mister!");
  
      @Inject
      private Logger log;
  
      private final String greeting;
  
      private MyEnum(String greeting) {
          this.greeting = greeting;
  //        this.log = LoggerFactory.getLogger(this.getClass());
      }
  
      public String getGreeting() {
          log.debug("Method getGreeting called");
          return this.greeting;
      }
  
  }
  </code></pre>
  
  <p>This class gives me a NPE on the <code>log.debug()</code> line. When I remove the <code>@Inject</code> and uncomment the <code>this.log</code> line it works. </p>
  
  <p>Testcase looks like this:</p>
  
  <pre><code>@RunWith(Arquillian.class)
  public class CoverKindTest {
  
      @Deployment
      public static WebArchive createDeployment() {
          return ShrinkWrap.create(WebArchive.class, "test.war")
                  .addClass(MyEnum.class)
                  .addClass(LoggerProducer.class)
                  .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
      }
  
      @Test
      public void testEnum() {
          MyEnum myEnum = MyEnum.HI;
          String greeting = myEnum.getGreeting();
          assertThat("Should give the greeting.", greeting, is("Hi there!"));
      }
  
  }
  </code></pre>
  
  <p>Complete testable project for this question can be found here, <code>MyEnum.class</code> is the original question, <code>MyEnum1.class</code> is the solution without injection (working, but not what I am looking for) and <code>MyEnum2.class</code> is a suggested answer.</p>
  
  <p>Edit: Updated the GitHub repo with a working solution.
  <a href="https://github.com/martijnburger/how-to-inject-a-logger-in-an-enum" rel="nofollow noreferrer">https://github.com/martijnburger/how-to-inject-a-logger-in-an-enum</a></p>