日志模式log4j

1、新建一个maven工程:
mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch04 -DartifactId=simple-weather -DpackageName=org.sonatype.mavenbook

2、创建java文件:
Main.java:关键代码

// Configure Log4J        取log4j的属性文件的时候,可以直接这么取 
PropertyConfigurator.configure(Main.class.getClassLoader().getResource("log4j.properties");

 
Weather.java
WeatherFormatter.java
YahooParser.java
YahooRetriever.java

3、创建资源文件:
log4j.properties
output.vm

4、构建项目:mvn install

5、运行程序:
mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main
   显示结果:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simple-weather 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.1:java (default-cli) @ simple-weather ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
0    INFO  YahooRetriever  - Retrieving Weather Data
411  INFO  YahooParser  - Creating XML Reader
518  INFO  YahooParser  - Parsing XML Response
559  INFO  WeatherFormatter  - Formatting Weather Data
642  INFO  mavenbook  - *********************************

Current Weather Conditions for:

  Evanston, IL, US



Temperature: 72

   Condition: Partly Cloudy

    Humidity: 87

  Wind Chill: 72

*********************************
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.831s
[INFO] Finished at: Wed Jun 25 10:21:45 CST 2014
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------

相关推荐