0

我发现了一个登录 clojure api 的问题。

我尝试为 API 请求和 SQL 语句实现日志记录机制。

项目.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)

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

这里

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

覆盖日志记录机制。意味着,日志显示在控制台上,而不是创建 app.log 文件(根据 log4j.properties 配置)。

只有 API 请求日志显示在控制台上,并且不显示 SQL 日志。

为什么日志会显示在控制台上?以及如何在文件中记录 SQL 日志?

4

2 回答 2

1

你的项目应该依赖slf4j-log4j12而不是slf4j-simple?自从

Binding for Simple implementation,将所有事件输出到 System.err。只打印级别 INFO 和更高级别的消息。此绑定在小型应用程序的上下文中可能很有用。

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

这应该是一个java问题。

编辑:

这实际上是第二个问题。

log4jdbc库使用代理数据源来记录 sql 语句,因此您应该使用提供的数据源类log4jdbc net.sf.log4jdbc.Log4jdbcProxyDataSource

你可以通过搜索谷歌获得更多信息log4jdbc

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

于 2017-07-12T09:26:48.590 回答
0

我想出了我的问题的答案。

修改项目.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)

修改后的 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
#
# # ==============================================================================
# # 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
于 2017-07-12T11:36:33.837 回答