Java spark framework enable logging

SergiX44 picture SergiX44 · Jul 22, 2016 · Viewed 9.1k times · Source

I'm building a java application with Spark framework with embedded Jetty and handlebars template engine. But when i get an 500 Internal Error, the console didn't say anything. I have added to my pom.xml the dependencies here: http://sparkjava.com/documentation.html#add-a-logger but does not print all exceptions / errors (like errors 500)

Here my pom.xml dependecies

<dependencies>

    <!-- FRAMEWORK:     Spark -->
    <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-core</artifactId>
        <version>2.5</version>
    </dependency>

    <!-- TEMPLATES:     Handlebars -->
    <dependency>
        <groupId>com.sparkjava</groupId>
        <artifactId>spark-template-handlebars</artifactId>
        <version>2.3</version>
    </dependency>

    <!-- DB-MAPPING:    sql2o -->
    <dependency>
        <groupId>org.sql2o</groupId>
        <artifactId>sql2o</artifactId>
        <version>1.5.4</version>
    </dependency>

    <!-- DRIVERS: sqlite-->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.8.11.2</version>
    </dependency>

    <!-- LOGGER:        slf4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>

</dependencies>

How i can enable all the logging for spark?

Answer

Dherik picture Dherik · Nov 24, 2016

To enable logging, just add the following dependency to your project:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.21</version>
</dependency>

and you can register a catch-all Spark exception handler to log uncaught exceptions:

Spark.exception(Exception.class, (exception, request, response) -> {
    exception.printStackTrace();
});