First create your Maven-based project. In my case, I'm using version 1.1.1 of the Vaadin Plugin for NetBeans to create a new Vaadin 7.1 project.
Add the logging façade library, SLF4J.
- In the NetBeans project navigator pane, context-click on the Dependencies item.
- Choose Add Dependency.
- Type: slf4j-api
- Open the org.slf4j : slf4j-api item to choose the latest version.
In a fashion similar to SLF4J, let's add Logback. Logback is a direct implementation of that SLF4J façade. You can use nearly any other Java-based logging frameworks in conjunction with an adapter. Logback needs no adapter. Logback is the successor to Log4J, both of which were created by the same man.
We need two jars for LogBack, "classic" and "core". However, adding a dependency for "classic" will automatically get us "core".
- In the NetBeans project navigator pane, context-click on the Dependencies item.
- Choose Add Dependency.
- Type: logback-classic
- Open the ch.qos.logback : logback-classic item.
- Choose the latest version offered.
Save all your files, and do a clean-and-build of your project (Hammer & Broom icon) to get Maven to do its duty. You should find in the project navigator pane a new item Other Sources. Expand that item to find a src/main/resources item.
- Context-click the src/main/resources item to create a new XML file.
- Name the new XML file: logback.xml
- Into that file, past the following XML text seen below.
- Change the XML text, replacing "com.example" with your own project’s top package.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.example" level="TRACE"/>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
This XML is different than that older article, having replaced the deprecated layout with an encoder. This XML configures Logback to send your logging messages to the NetBeans console. You'll need to make alterations for use when deployed to production, but this should get you started in development.
Now try it out. In your Vaadin app, locate the MyVaadinUI.java file that drives your app.
- Add an import: import org.slf4j.*;
- At the top of your class definition, add the line:
static final Logger LOG = LoggerFactory.getLogger(MyVaadinUI.class); - In the init method of this class, add the following code to see if logging works.
LOG.debug("Yogi - Logging Test - Debug");
LOG.info("Yogi - Logging Test - Info");
LOG.warn("Yogi - Logging Test - Warn");
LOG.error("Yogi - Logging Test - Error");
Run your Vaadin app. You may need to do a clean-and-build (Hammer & Broom icon). On one of the tabs in the NetBeans "Output" console pane you should see your messages.
To update the version numbers of SLF4J and Logback, look in the NetBeans project navigator pane. Expand the Project Files item to locate and open "pom.xml" file. Search for "slf4j" and "logback" and update each one’s version tag with the number you know to be current. Saving the XML file may cause Maven to do its duty. If not, try a clean-and-build.
Caveat: I am a Maven and SLF4J and Logback triple newbie. The above steps worked for me, but I cannot say that I understand them fully. Follow these steps at your own risk. Backup your project first.
No comments:
Post a Comment