egerhard
egerhard

Reputation: 1109

slf4j File Not Found error when building uberwar

I would like to understand why my Ring application is attempting to open the log file at compile time.

I have a webservice in Compojure and Ring. The application works without issue but whenever I compile the application to an uberwar or run lein ring server I get the below error from slf4j.

I don't understand what is causing the Clojure compiler to attempt to access this log file during compilation. The only clue which I have is that it only started happening when I included the Korma library.

If I create the directory I can get rid of the error, however, I want to understand why the application is attempting to open the log file at compile time.

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /var/log/ege/myservice/myservice.log (No such file or directory)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:270)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:133)
        at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
        at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:207)
        at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
        at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:307)
        at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:172)
        at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:104)
        at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:809)
        at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
        at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)

Upvotes: 0

Views: 214

Answers (1)

mavbozo
mavbozo

Reputation: 1261

log4j configuration is a runtime configuration of your app. Korma uses log4j configuration for its usage in runtime. So, it's not surprising that when running lein ring server, korma uses log4j configuration for its logging and the log4j logging throws error cause it can not find the log directory.

As for the error thrown during lein check or lein ring war. Maybe you have some forms which attempts to connect to database or even do some query to your database in top-level of some of your namespaces.

maybe you have something like this:

(ns my-app.db
    (:use korma.db)
    (:use korma.core))

(def db  (create-db (mysql {:db "my_db"
                        :user "root"
                        :password ""})))



;; this form below attempts to get a db connection
(def conn (get-connection db))

(defentity users)

;; this form below attempts to query the database
(select users)

when clojure compiles, it executes every form, thus, clojure connects to db or query the db. In other words, clojure runs a part of your app during compilation.

unrelated note

Everything above aside, you should separate the log4j configuration or any other runtime-configs for development environment and production environment.

I usually separate development environment and production environment configuration using :profiles in project.clj.

:profiles
   {:production
      {:ring
         {:open-browser? false, :stacktraces? false, :auto-reload? false}
       :source-paths ["prod-config"]}
    :dev
      {:dependencies [[ring-mock "0.1.5"] [ring/ring-devel "1.3.1"]]
       :source-paths ["dev-config"]}}

I put my development's log4j.properties in dev-config dir and production's log4j.properties in prod-config dir.

Upvotes: 1

Related Questions