Spray(9)REST API Project - Logback
Spray(9)RESTAPIProject-Logback
7.HowtoWorkwithLogback
"com.typesafe"%%"scalalogging-slf4j"%"1.0.1",
"ch.qos.logback"%"logback-classic"%"1.0.3"
Addthesedependenciesinbuild.sbt
Idonotliketheakkaloggers,theyarenoisytome,soIturnthemoffinapplication.conf
akka{
loglevel=ERROR
}
Herearemyconfigurationoflogback.xml,itisalmostlikelog4j.xmlorlog4j.properties
<?xmlversion="1.0"encoding="UTF-8"?>
<configuration>
<appendername="CONSOLE"class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<pattern>%date{MM/ddHH:mm:ss}%-5level[%thread]%logger{1}-%msg%n</pattern>
</encoder>
</appender>
<appendername="FILE"class="ch.qos.logback.core.FileAppender">
<file>akka.log</file>
<append>false</append>
<encoder>
<pattern>%date{MM/ddHH:mm:ss}%-5level[%thread]%logger{1}-%msg%n</pattern>
</encoder>
</appender>
<loggername="com.sillycat.easysprayrestserver.actor"level="DEBUG"/>
<loggername="com.sillycat.easysprayrestserver.util"level="INFO"/>
<loggername="com.sillycat.easysprayrestserver.dao"level="DEBUG"/>
<rootlevel="ERROR">
<appender-refref="CONSOLE"/>
</root>
<rootlevel="ERROR">
<appender-refref="FILE"/>
</root>
</configuration>
Thereare2kindsofloggingmessageswhichIamusing.
FirstType
importcom.typesafe.scalalogging.slf4j.Logging
traitURLRouterServiceextendsHttpServicewithUsersAuthenticationDirectiveswithLogging{
logger.debug("HittingtheURIresource/admin-only")
SecondType
importorg.slf4j.LoggerFactory
vallog=LoggerFactory.getLogger(this.getClass().getName())
log.debug("env:"+env)
8.HowtoworkwithDB
comesoon...
9.HowtoWorkwithActor
comesoon…
10.HowtodoValidation
comesoon...
Tips:
1.ErrorMessage:
app[ERROR]:SLF4J:ClasspathcontainsmultipleSLF4Jbindings.
app[ERROR]:SLF4J:Foundbindingin[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:Foundbindingin[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:Seehttp://www.slf4j.org/codes.html#multiple_bindingsforanexplanation.
app[ERROR]:SLF4J:Actualbindingisoftype[org.slf4j.helpers.NOPLoggerFactory]
app[ERROR]:May2,201310:19:13AMcom.mchange.v2.log.MLog<clinit>
app[ERROR]:INFO:MLogclientsusingjava1.4+standardlogging.
app[ERROR]:May2,201310:19:14AMcom.mchange.v2.c3p0.C3P0Registrybanner
app[ERROR]:INFO:Initializingc3p0-0.9.1.2[built21-May-200715:04:56;debug?true;trace:10]
Solution:
Firststep,getridoftheotherslf4jpackage
//"org.slf4j"%"slf4j-nop"%"1.6.4",
Usethesedependenciesinstead
"com.typesafe"%%"scalalogging-slf4j"%"1.0.1",
"ch.qos.logback"%"logback-classic"%"1.0.3"
Secondstep,getridoftheerrorofc3p0
Createafilenamedchange-log.properties
com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog
com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=OFF
Thatisit,turnoffthemessageinlogofc3p0.
2.Sometimes,NotWorking
ErrorMessage:
Theserverwasnotabletoproduceatimelyresponsetoyourrequest.
Solution:
importakka.util.Timeout
traitURLRouterServiceextendsHttpServicewithUsersAuthenticationDirectiveswithLogging{
implicitvaltimeout=Timeout(30*1000)
Configurethetimeoutwillhelptosolvetheproblem.
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