0

I have a Java EE application that consists in EAR, WAR and EJB packages.

I have configured logback by defining a @Produces method in an EJB class, like so:

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

@Named
@Singleton
public class LoggerProducer {

    @Produces
    public Logger produceLogger(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember()
                                       .getDeclaringClass().getName());
    }

}

This works fine, injecting the interface with

@Inject
private Logger logger;

and logging with

logger.error("This is the error");

I have these dependencies in my pom.xml:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

Now, I'm trying to include a logback.xml file to the deployment, I added it to the EJB classpath:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

But it doesn't work, it logs with the standard/default format.

I added the following jboss-deployment-structure.xml file to the EJB/META-INF folder to exclude logging:

<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging"/>
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

But it doesn't work either. If I put the jboss-deployment-structure.xml in EAR/META-INF I get the following error at startup (pointing to the WAR classes only):

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Logger with qualifiers @Default

Where to put the logback.xml file? How to configure the jboss-deployment-structure.xml file and where to put it?

ps0604
  • 1,227
  • 23
  • 133
  • 330

1 Answers1

0

Put it in the resources folder or any folder that's on the runtime classpath (CLASSPATH system variable should have an entry for it). For example, make your "make" file copy it to WEB-INF/classes (not a very good choice though).

  • Since the `@Produces` class is in the EJB I prefer to put it in that package instead of WEB-INF/classes that's in WAR, where can I put it? – ps0604 Mar 01 '19 at 17:43
  • A better way is to externalize & make it part of application startup. -Dlogback.configurationFile=/path/to/config.xml. See this link https://logback.qos.ch/faq.html#configFileLocation and this post https://stackoverflow.com/questions/50757241/logbackslf4j-does-no-log-in-javaee-7-application – sachin1410 Mar 01 '19 at 17:51
  • I can add it as a variable, but where exacly should I put the logback.xml if it's in /path/to/config folder? where's this folder in the EAR or EJB? – ps0604 Mar 01 '19 at 17:58
  • I don't know much about wildfly but if you have a dependency conflict, you will need to exclude one of them (i would prefer to exclude the unintentional WildFly's slf4j in your pom if you use maven).
    
    ...
             
         
                 ch.qos.logback
                 logback-classic
        
    
    – sachin1410 Mar 01 '19 at 19:53