JEE7: Using Logging with SLF4j

×

Generic SLF4l Logging Overview

Overview SLf4j logging with jcl-over-slf4j

SLf4j logging with jcl-over-slf4j
Image slf4j_logging_img1.jpg  NOT Found“;[/insert_php]

pom.xml


       <!-- Logging -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.0.13</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.7.5</version>
            </dependency>

Downloaded External Libraries by MAVEN

  • logback-classic-1.0.13.jar
  • logback-core-1.0.13.jar
  • jcl-over-slf4j-1.7.5.jar
  • slf4j-api-1.7.5.jar

Jee7 Code Sample for SLF4j Logging

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

// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorld {
    private static final Logger LOGGER
            = LoggerFactory.getLogger(HelloWorld.class);

    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @Produces("text/html")
    public String getFirstMessage() {
        LOGGER.info(" ---- Inside HTTP GET Request --- ");
 ..
<

SLF4j Logger Output [ server.log payara41\glassfish\domains\domain1\logs ]

  10:43:31.058 [http-thread-pool::http-listener-1(4)] INFO  HelloWorld -  ---- Inside HTTP GET Request --- 

JSF accessing an Resource

  • #{status} translates to enum value: FEMALE
  • {backend[status] translates to backend[FEMALE]
  • backend.properties translates FEMALE to Frau

 <f:selectItem itemValue="#{null}" itemLabel="Auswahl Geschlecht" noSelectionOption="true"  itemDisabled="true" />
                <f:selectItems
                        value="#{editor.myEnumValues}"
                        var="status"
                        itemValue="#{status}"
                        itemLabel="<strong>#{backend[status]</strong>}"
                />

Runtime Logging for SLFj/Logback

Display all Classes which are avaiable for Logging via Logback

      LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
      List loggerList = loggerContext.getLoggerList();
      loggerList.stream().forEach(tmpLogger -> System.out.println(tmpLogger.toString()
                + "  Log Level:" + tmpLogger.getLevel() + "  Name:" + tmpLogger.getName() ));

An Arquillian Sample using @BeforeClass @AfterClass Annotations

 @BeforeClass
    public static void setUpClass() {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        ch.qos.logback.classic.Logger myClassLogger =  loggerContext.getLogger("de.thnuernberg.in.infohm.server.services.AccountServiceIT");
        myClassLogger.setLevel(Level.toLevel("INFO"));
    }

   @AfterClass
    public static void tearDownClass() {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        ch.qos.logback.classic.Logger myClassLogger =  loggerContext.getLogger("de.thnuernberg.in.infohm.server.services.AccountServiceIT");
        myClassLogger.setLevel(null);
    }

  @Test
    @UsingDataSet("account.xml")
    public void testRemoveAccounts()   {
...
  

Reference

Leave a Reply

Your email address will not be published. Required fields are marked *