Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java spark framework enable logging

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?

like image 527
SergiX44 Avatar asked Jul 22 '16 13:07

SergiX44


People also ask

What is spark Java?

What is Spark Java? Spark is a Java micro framework that allows to quickly create web applications in Java 8. Spark is a lightweight and simple Java web framework designed for quick development. Sinatra, a popular Ruby micro framework, was the inspiration for it.

How to configure logging levels for spark application in SBT?

When running a Spark application from within sbt using run task, you can use the following build.sbt to configure logging levels: With the above configuration log4j.properties file should be on CLASSPATH which can be in src/main/resources directory (that is included in CLASSPATH by default).

Why do we need a logging system in spark?

Building a robust logging system within our apps could be use as a great insights of the business problems we are solving. Spark uses log4j as the standard library for its own logging. Everything that happens inside Spark gets logged to the shell console and to the configured underlying storage.

What is Apache Spark framework?

Spark Framework - Create web applications in Java rapidly. Spark is a micro web framework that lets you focus on writing your code, not boilerplate code. Download Docs Tutorials News Contact Getting Started Stopping the Server Routes Route unmapping Path Groups Request Response Query Maps Cookies Sessions Halting Filters Redirects Error Handling


2 Answers

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();
});
like image 90
Dherik Avatar answered Sep 27 '22 23:09

Dherik


Use log4j to make a logging implementation. That's why you don't have an idea why are you getting an internal server error

http://logging.apache.org/log4j/2.x/

like image 37
Augusto Avatar answered Sep 27 '22 22:09

Augusto