簡單使用SLF4J日誌

固步自封發表於2014-04-01

6.1 SLF4J

SLF4J(www.slf4j.org),是一個輕便小巧的JAVA日誌框架,很多應用框架都採用SLF4J配合其它日誌框架(如java.util.logging,logback,log4j等)來記錄日誌。

本節工作目錄為:~/project/t06。

6.2 SLF4J的HelloWorld

6.2.1 IVY配置檔案

在工作目錄下編輯IVY配置檔案ivy.xml,內容如下:

<?xml version="1.0" encoding="utf-8"?>
<ivy-module version="2.0">
    <info organisation="my" module="app"/>
    <dependencies>
        <dependency org="org.slf4j" name="slf4j-simple" rev="1.7.6"/>
    </dependencies>
</ivy-module>

說明:

  • info標籤指明我們的組織名為my,模組名為app;
  • dependency標籤指明依賴到org.slf4j組織的名為slf4j-simple的JAR包,版本號為1.7.6;

6.2.2 ANT配置檔案

在工程目錄下編輯ANT配置檔案build.xml, 內容如下:

<?xml version="1.0" encoding="utf-8"?>
<project name="myapp" default="run" xmlns:ivy="antlib:org.apache.ivy.ant">
  <!-- 定義變數 -->
  <property name="lib.dir" value="lib" />
  <property name="build.dir" value="build" />
  <property name="src.dir" value="src" />

  <!-- 定義編譯和執行時用到的路徑  -->
  <path id="libpath">
    <fileset dir="${lib.dir}" />
  </path>
  <path id="runpath">
    <path refid="libpath" />
    <path location="${build.dir}" />
  </path>

  <!-- 任務:下載依賴到的JAR包 -->
  <target name="resolve">
    <ivy:retrieve/>
  </target>

  <!-- 任務:生成JAR包間依賴關係的報告 -->
  <target name="report" depends="resolve">
    <ivy:report todir="${build.dir}"/>
  </target>

  <!-- 任務:執行 -->
  <target name="run" depends="resolve">
    <mkdir dir="${build.dir}" />
    <javac srcdir="${src.dir}" destdir="${build.dir}"
       classpathref="libpath" includeAntRuntime="false"/>
    <!-- 定義變星msg,值為"hello ivy !" -->
    <property name="msg" value="hello ivy !"/>
    <!-- 執行java,加命令列引數,引用上面定義的變數msg -->
    <java classpathref="runpath" classname="my.app.Hello">
      <arg value="-message"/>
      <arg value="${msg}"/>
    </java>
  </target>

  <!-- 任務:清除 -->
  <target name="clean">
    <delete includeemptydirs="true">
      <fileset dir="${basedir}">
    <exclude name="src/**" />
    <exclude name="build.xml" />
    <exclude name="ivy.xml" />
      </fileset>
    </delete>
  </target>

  <!-- 任務:清除IVY的快取 -->
  <target name="clean-cache">
    <ivy:cleancache />
  </target>
</project>

ANT的配置檔案build.xml中關於IVY的內容:

  • 在project標籤中加入了xmlns:ivy="antlib:org.apache.ivy.ant"
  • ivy:retrieve 標籤,下載相關的JAR包
  • ivy:report todir="${build.dir}" 標籤,生成依賴關係的報告到${build.dir}
  • ivy:cleancache 標籤,清除快取(注意:不要輕易使用,否則你曾經下載過的東西都要重新來過!)

6.2.3 編輯原始碼

建立原始碼目錄src,編輯原始碼src/my/app/Hello.java,內容如下:

package my.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Hello {
    public static void main(String args[]) {
        Logger logger = LoggerFactory.getLogger(Hello.class);
        logger.info("Hello World!");
        if(args.length > 1) {
            System.out.println("args : " + args[1]);
        }
    }
}

6.2.4 執行ant

在命令列中直接執行ant命令,效果如下:

song@ubuntu:~/project/t06$ ant
Buildfile: /home/song/project/t06/build.xml

resolve:
[ivy:retrieve] :: Apache Ivy 2.3.0 - 20130110142753 :: http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: url = jar:file:/home/song/tools/ant/lib/ivy-2.3.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: my#app;working@ubuntu
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     found org.slf4j#slf4j-simple;1.7.6 in public
[ivy:retrieve]     found org.slf4j#slf4j-api;1.7.6 in public
[ivy:retrieve] downloading http://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.6/slf4j-simple-1.7.6.jar ...
[ivy:retrieve] ....... (10kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]     [SUCCESSFUL ] org.slf4j#slf4j-simple;1.7.6!slf4j-simple.jar (2666ms)
[ivy:retrieve] downloading http://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.6/slf4j-simple-1.7.6-sources.jar ...
[ivy:retrieve] ........ (11kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]     [SUCCESSFUL ] org.slf4j#slf4j-simple;1.7.6!slf4j-simple.jar(source) (1729ms)
[ivy:retrieve] downloading http://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.6/slf4j-simple-1.7.6-javadoc.jar ...
[ivy:retrieve] ............................................. (61kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]     [SUCCESSFUL ] org.slf4j#slf4j-simple;1.7.6!slf4j-simple.jar(javadoc) (2865ms)
[ivy:retrieve] downloading http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.6/slf4j-api-1.7.6.jar ...
[ivy:retrieve] .................... (28kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]     [SUCCESSFUL ] org.slf4j#slf4j-api;1.7.6!slf4j-api.jar (8621ms)
[ivy:retrieve] :: resolution report :: resolve 15958ms :: artifacts dl 15895ms
    ---------------------------------------------------------------------
    |                  |            modules            ||   artifacts   |
    |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
    ---------------------------------------------------------------------
    |      default     |   2   |   2   |   2   |   0   ||   4   |   4   |
    ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: my#app
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     4 artifacts copied, 0 already retrieved (111kB/11ms)

run:
    [mkdir] Created dir: /home/song/project/t06/build
    [javac] Compiling 1 source file to /home/song/project/t06/build
     [java] [main] INFO my.app.Hello - Hello World!
     [java] args : hello ivy !

BUILD SUCCESSFUL
Total time: 33 seconds

上面的執行資訊說明編譯執行成功,輸出了一條日誌資訊和我們傳遞的引數。

6.2.5 檢視下載的JAR包

輸入命令列:ls lib,檢視下載到本地的JAR包

song@ubuntu:~/project/t06$ ls lib
slf4j-api-1.7.6.jar     slf4j-simple-1.7.6-javadoc.jar
slf4j-simple-1.7.6.jar  slf4j-simple-1.7.6-sources.jar

可見預設的情況下原始碼包和文件包都下載了。

6.2.6 輸出依賴關係報告

在命令列輸入:ant report,輸出效果如下:

song@ubuntu:~/project/t06$ ant report
Buildfile: /home/song/project/t06/build.xml

resolve:
[ivy:retrieve] :: Apache Ivy 2.3.0 - 20130110142753 :: http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: url = jar:file:/home/song/tools/ant/lib/ivy-2.3.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: my#app;working@ubuntu
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     found org.slf4j#slf4j-simple;1.7.6 in public
[ivy:retrieve]     found org.slf4j#slf4j-api;1.7.6 in public
[ivy:retrieve] :: resolution report :: resolve 178ms :: artifacts dl 7ms
    ---------------------------------------------------------------------
    |                  |            modules            ||   artifacts   |
    |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
    ---------------------------------------------------------------------
    |      default     |   2   |   0   |   0   |   0   ||   4   |   0   |
    ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: my#app
[ivy:retrieve]     confs: [default]
[ivy:retrieve]     0 artifacts copied, 4 already retrieved (0kB/9ms)

report:
[ivy:report] Processing /home/song/.ivy2/cache/my-app-default.xml to /home/song/project/t06/build/my-app-default.html
[ivy:report] Processing /home/song/.ivy2/cache/my-app-default.xml to /home/song/project/t06/build/my-app-default.graphml

BUILD SUCCESSFUL
Total time: 1 second

輸出報告成功,用瀏覽器開啟/home/song/project/t06/build/my-app-default.html檢視。

需要說明的是,IVY的本地庫快取目錄預設為~/.ivy2/cache,每次下載的JAR包都會儲存到這個目錄中,同時也會複製到本工程的庫目錄(project/t06/lib)中,下載完成後我們就不需要再做下載操作了,可以將IVY相關的操作註釋掉,避免誤操作引起不必要的麻煩。

JUNIT單元測試工具是JAVA開發的必備工具,下一節我們簡單研究一下。

相關文章