Clojure REST API: Logging issue

542 Views Asked by At

I have figured out one issue with logging in clojure api.

I try to implement logging mechanism for API Requests and SQL statements.

Project.clj

    (defproject clojure-dauble-business-api "0.1.0-SNAPSHOT"
      :description "FIXME: write description"
      :url "http://example.com/FIXME"
      :license {:name "Eclipse Public License"
                :url "http://www.eclipse.org/legal/epl-v10.html"}
      :ring {:handler clojure-dauble-business-api.routes/app}
      :dependencies [[org.clojure/clojure "1.8.0"]
                     [metosin/compojure-api "1.1.10"]
                     [ring/ring-core "1.4.0"]
                     [ring/ring-jetty-adapter "1.4.0"]
                     [ring/ring-defaults "0.2.0"]
                     [org.clojure/java.jdbc "0.6.1"]
                     [yesql/yesql "0.5.3"]
                     [org.postgresql/postgresql "9.4.1212"]
                     [org.clojure/data.json "0.2.6"]
                     [cheshire "5.7.1"]
                     [com.googlecode.log4jdbc/log4jdbc "1.2"]
                     [org.slf4j/slf4j-simple "1.7.12"]
                     [org.clojure/tools.logging "0.3.1"]
                     [log4j/log4j "1.2.17" :exclusions [javax.mail/mail
                                                        javax.jms/jms
                                                        com.sun.jmdk/jmxtools
                                                        com.sun.jmx/jmxri]]]
  :profiles {:dev {:resource-paths ["resources"]}}

  :plugins [[lein-ring "0.12.0"]]
  :main clojure-dauble-business-api.routes)

and

log4j.properties

# Application Logging
log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/app.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d][%p][%c] %m%n

# # the appender used for the JDBC API layer call logging above, sql only
log4j.appender.sql=org.apache.log4j.RollingFileAppender
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern= \u001b[0;31m (SQL)\u001b[m %d{yyyy-MM-dd HH:mm:ss.SSS} \u001b[0;32m %m \u001b[m %n
#
# # ==============================================================================
# # JDBC API layer call logging :
# # INFO shows logging, DEBUG also shows where in code the jdbc calls were made,
# # setting DEBUG to true might cause minor slow-down in some environments.
# # If you experience too much slowness, use INFO instead.
#
log4jdbc.drivers=org.postgresql.Driver
#
# # Log all JDBC calls except for ResultSet calls
log4j.logger.jdbc.audit=INFO,sql
log4j.additivity.jdbc.audit=false
#
# # Log only JDBC calls to ResultSet objects
log4j.logger.jdbc.resultset=INFO,sql
log4j.additivity.jdbc.resultset=false
#
# # Log only the SQL that is executed.
log4j.logger.jdbc.sqlonly=INFO,sql
log4j.additivity.jdbc.sqlonly=false
#
# # Log timing information about the SQL that is executed.
log4j.logger.jdbc.sqltiming=INFO,sql
log4j.additivity.jdbc.sqltiming=false
#
# # Log connection open/close events and connection number dump
log4j.logger.jdbc.connection=INFO,sql
log4j.additivity.jdbc.connection=false

here

[com.googlecode.log4jdbc/log4jdbc "1.2"]
[org.slf4j/slf4j-simple "1.7.12"]

overrides logging mechanism. Means, Logs are getting displayed on console instead creating app.log file(as per log4j.properties configurations).

Only API requests logs are getting displayed on console and It does not show SQL Logs.

Why logs are getting displayed on console? and How can I logs SQL logs in file?

2

There are 2 best solutions below

0
On BEST ANSWER

I have figured answer to my issue.

Modified project.clj

(defproject clojure-dauble-business-api "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :ring {:handler clojure-dauble-business-api.routes/app}
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [metosin/compojure-api "1.1.10"]
                 [ring/ring-core "1.4.0"]
                 [ring/ring-jetty-adapter "1.4.0"]
                 [ring/ring-defaults "0.2.0"]
                 [org.clojure/java.jdbc "0.6.1"]
                 [yesql/yesql "0.5.3"]
                 [org.postgresql/postgresql "9.4.1212"]
                 [org.clojure/data.json "0.2.6"]
                 [cheshire "5.7.1"]
                 [com.googlecode.log4jdbc/log4jdbc "1.2"]
                 [org.slf4j/slf4j-log4j12 "1.7.21"]
                 [org.clojure/tools.logging "0.3.1"]
                 [log4j/log4j "1.2.17" :exclusions [javax.mail/mail
                                                    javax.jms/jms
                                                    com.sun.jmdk/jmxtools
                                                    com.sun.jmx/jmxri]]]
  :profiles {:dev {:resource-paths ["resources"]}}
  :plugins [[lein-ring "0.12.0"]]
  :main clojure-dauble-business-api.routes)

Modified log4j.properties file is here

# Application Logging
log4j.rootLogger=info, file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/app.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d][%p][%c] %m%n
#
# # ==============================================================================
# # JDBC API layer call logging :
# # INFO shows logging, DEBUG also shows where in code the jdbc calls were made,
# # setting DEBUG to true might cause minor slow-down in some environments.
# # If you experience too much slowness, use INFO instead.
#
log4jdbc.drivers=org.postgresql.Driver
#
# # Log all JDBC calls except for ResultSet calls
log4j.logger.jdbc.audit=INFO,sql
log4j.additivity.jdbc.audit=false
#
# # Log only JDBC calls to ResultSet objects
log4j.logger.jdbc.resultset=INFO,sql
log4j.additivity.jdbc.resultset=false
#
# # Log only the SQL that is executed.
log4j.logger.jdbc.sqlonly=INFO,sql
log4j.additivity.jdbc.sqlonly=false
#
# # Log timing information about the SQL that is executed.
log4j.logger.jdbc.sqltiming=INFO,sql
log4j.additivity.jdbc.sqltiming=false
#
# # Log connection open/close events and connection number dump
log4j.logger.jdbc.connection=INFO,sql
log4j.additivity.jdbc.connection=false

! the appender used for the JDBC API layer call logging above, sql only
log4j.appender.sql=org.apache.log4j.FileAppender
log4j.appender.sql.File=./log/sql.log
log4j.appender.sql.Append=false
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=[%d][%p][%c] %m%n
2
On

your project should depend on slf4j-log4j12 instead of slf4j-simple? Since

Binding for Simple implementation, which outputs all events to System.err. Only messages of level INFO and higher are printed. This binding may be useful in the context of small applications.

https://www.slf4j.org/manual.html

It should be a java question.

edit:

This is a second question, actually.

The log4jdbc library using a proxy datasource to log sql statement, so you should use the datasource class provided by log4jdbc net.sf.log4jdbc.Log4jdbcProxyDataSource

you can get more info by just search google by log4jdbc

http://blog.jhades.org/logging-the-actualreal-sql-queries-of-a-springhibernate-application/