Surpassing yesterday's series (4)
Building a project using Maven has many advantages. We strongly recommend that you use Maven for initial project construction. The Log Module isProject. Modules are spliced to learn how to build a complete and awesome project.
With this command, a project is automatically created:
MVN archetype: Create-dgroupid = com. oreilly-dartifactid = My-app
Generate a project directory like this:
Recommended: In You can query the dependency of POM. xml on the following website: Http://search.maven.org/
The configuration of POM. XML is as follows:
< Project Xmlns = "Http://maven.apache.org/POM/4.0.0" Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance" Xsi: schemalocation = "Http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion > 4.0.0 </ Modelversion > < Groupid > Com. KKK. WAP </ Groupid > < Artifactid > Testapp</ Artifactid > < Version > 1.0-Snapshot </ Version > < Packaging > Jar </ Packaging > < Name > Testapp </ Name > < URL > Http://maven.apache.org </ URL > < Properties > < Project. Build. sourceencoding > UTF-8 </ Project. Build. sourceencoding > </ Properties > < Dependencies > < Dependency > < Groupid > Ch. QoS. logback</ Groupid > < Artifactid > Logback-classic </ Artifactid > < Version > 1.0.13 </ Version > </ Dependency > < Dependency > < Groupid > JUnit </ Groupid > < Artifactid > JUnit </ Artifactid > < Version > 3.8.1 </ Version > < Scope > Test </ Scope > </ Dependency > </ Dependencies > </ Project >
Run the following command in the project root directory:MVN install command
Will download the jar package:
Test class:
/** * Hello world! * */ Public Class APP { Private Static Logger logger = loggerfactory. getlogger (App. Class ); Public Static Void Main (string [] ARGs) {logger. Info ( "App said: Show me the money! "); System. Out. println ( "Hello world! " ); Logger. Info ( "You said: Yo! Yo! " ); Try {String s = "12 W" ; Integer. parseint (s );} Catch (Exception e) {logger. Error ( "You have a big poblem:" + E );}}}
Logback. xml configuration:
<? XML version = "1.0" encoding = "UTF-8" ?> <! -- Logback configuration. --> < Configuration Debug = "False" > < Appender Name = "Alllog" Class = "Ch. QoS. logback. Core. Rolling. rollingfileappender" > < File > D:/alllog. Log </ File > < Encoding > UTF-8 </ Encoding > < Rollingpolicy Class = "Ch. QoS. logback. Core. Rolling. timebasedrollingpolicy" > < Filenamepattern > D:/alllog. % d {yyyy-mm-dd}. Log </ Filenamepattern > </ Rollingpolicy > < Append > True </ Append > < Layout Class = "Ch. QoS. logback. Classic. patternlayout" > < Pattern > <! [CDATA [ % N %-4r [% d {yyyy-mm-dd hh: mm: SS} % x {productionmode}]-% x {method} % x {requesturiwithquerystring} [IP = % x {remoteaddr}, ref = % x {referrer }, UA = % x {useragent}, SID = % x {cookie. JSESSIONID}] % N %-5 level % logger {35}-% m % N ]> </ Pattern > </ Layout > </ Appender > < Root > < Level Value = "Info" /> < Appender-ref Ref = "Alllog" /> </ Root > </ Configuration >
Explain: Logs are stored on disk D.In the alllog folder, after logging is completed every day, he will change his nameD:/alllog. % d {Yyyy-Mm- Dd}. Log format. Note that the logs being played are inIn alllog. log. About
Logback. xmlFor configuration, refer to the blog: Http://blog.csdn.net/haidage/article/details/6794509
Log output result:
419 [ 2013 - 06 - 07 16 : 02 : 22 ]-[IP =, ref =, UA =, SID = ] Info com. KKK. WAP. app -App said: Show me the money! 434 [ 2013 - 06 - 07 16 : 02 : 22 ]-[IP =, ref =, UA =, SID = ] Info com. KKK. WAP. app -You said: Yo! Yo!435 [ 2013 - 06 - 07 16 : 02 : 22 ]-[IP =, ref =, UA =, SID = ] Error com. KKK. WAP. app -You have a big poblem: Java. Lang. numberformatexception: for Input String : " 12 W "