How to log in with slf4j from within a jboss module?

2

How can I log in to the console / server.log from within a jboss module?

Let's say I have a class:

public class MyClass {

    private static final Logger logger = LoggerFactory.getLogger(MyClass.class);

    private boolean done = false;

    public void doSomething() {
        logger.info("Look ma, I'm logging!");
        done = true;
    }

    public boolean isDone() {
        return done;
    }
}

If I want to log something from within a deployed artifact (e.g., MyWebProject.war ), all I have to do is:

  • Compile against slf4j-api

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
        <scope>provided</scope>
    </dependency>
    
  • Deploy

    ./jboss-cli.sh -c "deploy  MyWebProject.war"
    
  • And soon

    2015-10-19 11:04:02,445 INFO  [com.myCompany.MyClass] (default task-13) Look ma, I'm logging!
    
  • But I can not, by any means, get the same effect from within a jboss module.

    Example: If MyWebProject.war uses MyModule.jar , and MyModule.jar is deployed as a jboss module:

    ${jbossHome}/modules/com/mycompany/mymodule/main
                                                |____ MyModule.jar
                                                |____ module.xml
    

    Module.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.1" name="com.mycompany.mymodule">
      <resources>
        <resource-root path="MyModule.jar" />
      </resources>
      <dependencies>
        <module name="org.slf4j" />
      </dependencies>
    </module>
    

    If I move MyClass to MyModule.jar and use it in MyWebProject.war I can see the side effects (e.g. isDone() == true ) but nothing is written to server.log .

    What am I forgetting? Do I need some other dependency besides slf4j?

    Crosspost: Original question - SOen - How to log with slf4j from within a jboss module?

        
    asked by anonymous 19.10.2015 / 23:29

    1 answer

    2

    For future reference, my problem had nothing to do with logging . The above recipe works as expected. In fact, I suffered because of false information: my original module.xml was never actually used. I was loading an old class with the same name into another module. This old version of the class did not have logging and should not be there in the first place.

    Anyway, I think the root cause of my problem (aside from lack of attention) was a small bug in jboss-cli .

    I was doing deploy of mymodule with the following command:

    module add --name=com.mycompany.mymodule \
                --resources=MyModule.jar \
                --dependencies=org.slf4j \
                --main-class=com.mycompany.mymodule.Main 
    

    This command was generating module.xml like this:

    <?xml version="1.0" ?>
    
    <module xmlns="urn:jboss:module:1.1" name="com.mycompany.mymodule">
    
        <main-class value="com.mycompany.mymodule.Main"/>
    
        <resources>
            <resource-root path="MyModule.jar"/>
        </resources>
    
        <dependencies>
            <module name="org.slf4j"/>
        </dependencies>
    </module>
    

    When I finally got my web project to attempt to load mymodule it failed with a stack trace like this:

    18:45:59,999 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-7) MSC000001: Failed to start service jboss.module.service."deployment.MyWebProject.war".main: org.jboss.msc.service.StartException in service jboss.module.service."deployment.MyWebProject.war".main: WFLYSRV0179: Failed to load module: deployment.MyWebProject.war.war:main
        at org.jboss.as.server.moduleservice.ModuleLoadService.start(ModuleLoadService.java:91)
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: org.jboss.modules.ModuleLoadException: Error loading module from C:\opt\server\wildfly-9.0.1.Final\modules\com\mycompany\mymodule\main\module.xml
        at org.jboss.modules.ModuleXmlParser.parseModuleXml(ModuleXmlParser.java:150)
        at org.jboss.modules.ModuleXmlParser.parseModuleXml(ModuleXmlParser.java:127)
        at org.jboss.modules.LocalModuleFinder$1.run(LocalModuleFinder.java:150)
        at org.jboss.modules.LocalModuleFinder$1.run(LocalModuleFinder.java:144)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.jboss.modules.LocalModuleFinder.findModule(LocalModuleFinder.java:144)
        at org.jboss.modules.ModuleLoader.findModule(ModuleLoader.java:452)
        at org.jboss.modules.ModuleLoader.loadModuleLocal(ModuleLoader.java:355)
        at org.jboss.modules.ModuleLoader.preloadModule(ModuleLoader.java:302)
        at org.jboss.modules.ModuleLoader.preloadExportedModule(ModuleLoader.java:313)
        at org.jboss.modules.ModuleLoader.preloadModule(ModuleLoader.java:326)
        at org.jboss.as.server.moduleservice.ServiceModuleLoader.preloadModule(ServiceModuleLoader.java:149)
        at org.jboss.modules.ModuleLoader.loadModule(ModuleLoader.java:234)
        at org.jboss.as.server.moduleservice.ModuleLoadService.start(ModuleLoadService.java:74)
        ... 5 more
    Caused by: org.jboss.modules.xml.XmlPullParserException: Unexpected content of type 'element start' named 'main-class', text is: '<main-class value="com.mycompany.mymodule.Main"/>' (position: START_TAG seen ...n-class value="com.mycompany.mymodule.Main"/>... @5:54) 
        at org.jboss.modules.ModuleXmlParser.unexpectedContent(ModuleXmlParser.java:179)
        at org.jboss.modules.ModuleXmlParser.parseMainClass(ModuleXmlParser.java:620)
        at org.jboss.modules.ModuleXmlParser.parseModuleContents(ModuleXmlParser.java:445)
        at org.jboss.modules.ModuleXmlParser.parseDocument(ModuleXmlParser.java:261)
        at org.jboss.modules.ModuleXmlParser.parseModuleXml(ModuleXmlParser.java:148)
        ... 18 more
    

    Looking at module-1_1.xsd I found that the main-class element was expecting a name attribute instead of a value attribute. So I manually modified module.xml to:

    <main-class name="com.mycompany.mymodule.Main"/>
    

    After restarting WildFly and redeploy my web project everything worked as expected.

        
    28.10.2015 / 20:48