} } }

    maven设备简单的log模块-超出昨天的本身系列(4)

    添加时间:2013-6-7 点击量:

    超出昨天的本身系列(4)


    用maven构建项目长处太多了,强烈建议项目初期构建应用maven。log模块是项目中必不成少的。一个个模块拼接起来,慢慢进修更便利的构建一个完全,的项目。




     

    经由过程如许的号令,会主动创建一个项目:

     



    mvn archetype:create -DgroupId=com.oreilly -DartifactId=my-app



     


    产生类似如许一个项目目次:

     




     

     



    推荐:pom.xml写依附的时辰可以经由过程下面这个网站来查询:


    http://search.maven.org/

     


    pom.xml的设备如下:

     



    <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>


    在项目根目次履行:mvn install 号令  


    会一下jar包:




    测试类:

     



    /
    
    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
    = 12w ;
    Integer .parseInt ( s) ;
    }
    catch (Exception e) {
    logger .error (
    you have a big poblem: + e ) ;
    }

    }
    }



    logback.xml的设备:



    <? 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 %-5level %logger{35} - %m%n
    ]]> </pattern>
    </layout>
    </appender>

    <root>
    <level value = info />
    <appender-ref ref = AllLOG />
    </root>
    </configuration>



     

    申明一下:日记打在d盘的allLog文件夹下,每一天打完日记他会批改本身名字为d:/allLog.%d{yyyy-MM- dd}.log 如许的格局。重视正在打的日记是在allLog.log里。

     

    关于logback.xml设备可以参考博文:

     



    http://blog.csdn.net/haidage/article/details/6794509



     

     

    log输出成果:

     



    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: 12w












    彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
    分享到: