`
sillycat
  • 浏览: 2537824 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spray(9)REST API Project - Logback

 
阅读更多
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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics