Logging with Logback

Hi Everyone,

I am trying to get my new Vaadin Project running with Logback and it does not work.

What i did:
Read the Vaadin Book: https://vaadin.com/book/-/page/advanced.logging.html

My code looks like:

import javax.servlet.annotation.WebServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("myvaadin")
public class MyvaadinUI extends UI {

    static {
        // SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();
    }

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

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = MyvaadinUI.class)
    public static class Servlet extends VaadinServlet {
    }


    @Override
    protected void init(VaadinRequest request) {

        setSizeFull();

        VerticalLayout root = new VerticalLayout();
        setContent(root);
        root.addComponent(new Label("success"));

        logger.info("success");

    }

}

To the Ivy.xml, I added:

        <dependency org="org.slf4j" name="jul-to-slf4j" rev="1.7.12"/>
        <dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.4"/>
        <dependency org="org.apache.logging.log4j" name="log4j-api" rev="2.4"/>
        <dependency org="ch.qos.logback" name="logback-classic" rev="1.1.3"/>

The Logback.xml looks like:

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

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread]
 %-5level %logger{5} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="com.example.myvaadin" level="ERROR" additivity="false">
   <appender-ref ref="STDOUT" />
  </logger>

  <!-- Strictly speaking, the level attribute is not necessary since -->
  <!-- the level of the root level is set to DEBUG by default.       -->
  <root level="ERROR">          
    <appender-ref ref="STDOUT" />
  </root>  
  
</configuration>

What I now expected, is NO Output, because I set the logging level to ERROR and all I added was … info(“…”), and there are no errors.

Interessting is that I also get an error that no logging ConsoleHandler was found.

Here is my Console Output:

[code]
01:24:37,510 WARNING [com.vaadin.server.DefaultDeploymentConfiguration]
(default task-27)

Vaadin is running in DEBUG MODE.
Add productionMode=true to web.xml to disable debug features.
To show debug window, add ?debug to your application URL.

01:24:37,602 ERROR [stderr]
(default task-30) Handler java.util.logging.ConsoleHandler is not defined
01:24:37,619 INFO [com.example.myvaadin.MyvaadinUI]
(default task-32) success
[/code]Do you have any Ideas what I did wrong? Or any suggestions what i could improve.

Thanks

You have log4j in your ivy dependencies, but I don’t see you’re using it.
I have it working just fine using a maven project running under tomcat. My dependencies are:
logback-classic 1.1.3
jcl-over-slf4j 1.7.5 (this is just in case some dependent library uses JCL)
(slf4j-api 1.7.5 comes with logback-classic)
I didn’t need the SLF4JBridgeHandler but I did add a weblistener to my UI class:

@WebListener
public static class MyLogbackConfigListener extends LogbackConfigListener {
// Need this to init logback correctly
}

That saves logback from initialising on each request, but it will work without it.
My configuration looks different to yours:

<configuration scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>%-35(%d{HH:mm:ss.SSS} [%thread]
) %-5level %logger{35} - %F:%L %msg%n</pattern>
    </encoder>
    </appender>

    <logger name="nz.co.senanque.madurarulesdemo" level="debug"/>
    <logger name="nz.co.senanque.vaadin.application" level="debug"/>

  <root level="warn">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Your encoder entry is missing a class reference. I don’t know if that’s important I’ve been cloning this file for years so maybe we no longfer need it :slight_smile: but it does work.
If your log4j dependencies are to handle dependent librarties that might use it (just as I anticipate some on mine using JCL) then you may need to add log4j-over-slf4j to your classpath.