Spray(9)REST API Project - Logback

本文介绍了如何在Akka项目中使用Logback进行日志记录配置,包括排除不需要的日志框架、配置logback.xml文件以及解决多SLF4J绑定问题。此外还提供了解决c3p0错误消息的方法,并介绍了如何设置超时来解决响应延迟问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spray(9)REST API Project - Logback

7. How to Work with Logback
"com.typesafe" %% "scalalogging-slf4j" % "1.0.1",

"ch.qos.logback" % "logback-classic" % "1.0.3"

Add these dependencies in build.sbt

I do not like the akka loggers, they are noisy to me, so I turn them off in application.conf
akka {
loglevel = ERROR

}

Here are my configuration of logback.xml, it is almost like log4j.xml or log4j.properties
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<pattern>%date{MM/dd HH:mm:ss} %-5level[%thread] %logger{1} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE"class="ch.qos.logback.core.FileAppender">
<file>akka.log</file>
<append>false</append>
<encoder>
<pattern>%date{MM/dd HH:mm:ss} %-5level[%thread] %logger{1} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.sillycat.easysprayrestserver.actor" level="DEBUG"/>
<logger name="com.sillycat.easysprayrestserver.util" level="INFO"/>
<logger name="com.sillycat.easysprayrestserver.dao" level="DEBUG"/>
<root level="ERROR">
<appender-ref ref="CONSOLE"/>
</root>
<rootlevel="ERROR">
<appender-ref ref="FILE"/>
</root>

</configuration>

There are 2 kinds of logging messages which I am using.
First Type
import com.typesafe.scalalogging.slf4j.Logging
trait URLRouterService extends HttpService with UsersAuthenticationDirectives with Logging {
logger.debug("Hitting the URI resource/admin-only")

Second Type
import org.slf4j.LoggerFactory
val log = LoggerFactory.getLogger(this.getClass().getName())
log.debug("env: " + env)


8. How to work with DB
come soon...


9. How to Work with Actor
come soon…


10. How to do Validation
come soon...


Tips:
1. Error Message:
app[ERROR]: SLF4J: Class path contains multiple SLF4J bindings.
app[ERROR]: SLF4J: Found binding in [jar:file:/Users/carl/.ivy2/cache/org.slf4j/slf4j-nop/jars/slf4j-nop-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
app[ERROR]: SLF4J: Found binding in [jar:file:/Users/carl/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.0.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
app[ERROR]: SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
app[ERROR]: SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]
app[ERROR]: May 2, 2013 10:19:13 AM com.mchange.v2.log.MLog <clinit>
app[ERROR]: INFO: MLog clients using java 1.4+ standard logging.
app[ERROR]: May 2, 2013 10:19:14 AM com.mchange.v2.c3p0.C3P0Registry banner
app[ERROR]: INFO: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]

Solution:
First step, get rid of the other slf4j package
//"org.slf4j" % "slf4j-nop" % "1.6.4",

Use these dependencies instead
"com.typesafe" %% "scalalogging-slf4j" % "1.0.1",

"ch.qos.logback" % "logback-classic" % "1.0.3"

Second step, get rid of the error of c3p0
Create a file named change-log.properties
com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog

com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=OFF

That is it, turn off the message in log of c3p0.

2. Sometimes, Not Working
Error Message:
The server was not able to produce a timely response to your request.

Solution:
import akka.util.Timeout

trait URLRouterService extends HttpService with UsersAuthenticationDirectives with Logging {

implicitvaltimeout = Timeout(30 * 1000)

Configure the timeout will help to solve the problem.

References:
http://doc.akka.io/docs/akka/2.1.0/scala/logging.html
http://tantrajnana.blogspot.com/2012/10/using-c3p0-with-logback.html
http://doc.akka.io/docs/akka/snapshot/java/logging.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值