Spring-logging-with-log4j

提供:Dev Guides
移動先:案内検索

Spring-Log4Jでのロギング

これは、Springアプリケーション内で非常に使いやすいLog4J機能です。 次の例では、Log4JとSpringの簡単な統合を説明する簡単な手順を紹介します。

すでにマシンに log4J がインストールされていると想定しています。 お持ちでない場合は、https://logging.apache.org/からダウンロードして、任意のフォルダーに圧縮ファイルを抽出することができます。 プロジェクトでは log4j-x.y.z.jar のみを使用します。

次に、作業中のEclipse IDEを用意し、次の手順を実行して、Spring Web Frameworkを使用して動的フォームベースのWebアプリケーションを開発します。

Steps Description
1 Create a project with a name SpringExample and create a package com.finddevguides under the *src *folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Add log4j library log4j-x.y.z.jar as well in your project using using Add External JARs.
4 Create Java classes HelloWorld and MainApp under the com.finddevguides package.
5 Create Beans configuration file Beans.xml under the* src *folder.
6 Create log4J configuration file log4j.properties under the* src* folder.
7 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.
*HelloWorld.java* ファイルの内容は次のとおりです。
package com.finddevguides;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage() {
      System.out.println("Your Message : " + message);
   }
}

2番目のファイル MainApp.java の内容は次のとおりです。

package com.finddevguides;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.log4j.Logger;

public class MainApp {
   static Logger log = Logger.getLogger(MainApp.class.getName());

   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      log.info("Going to create HelloWord Obj");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();

      log.info("Exiting the program");
   }
}

情報メッセージを生成したのと同様の方法で*デバッグ*および*エラー*メッセージを生成できます。 Beans.xml ファイルの内容を見てみましょう

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "helloWorld" class = "com.finddevguides.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

以下は、Log4Jがログメッセージを生成するために必要な標準ルールを定義する log4j.properties の内容です。

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:\\log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, overwrite
log4j.appender.FILE.Append=false

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがアプリケーションで問題ない場合、これはEclipseコンソールに次のメッセージを出力します-

Your Message : Hello World!

C:\\ドライブをチェックすると、次のようなログメッセージ log.out がさまざまなログメッセージと共に見つかるはずです-

<!-- initialization log messages -->

Going to create HelloWord Obj
Returning cached instance of singleton bean 'helloWorld'
Exiting the program

Jakarta Commons Logging(JCL)API

または、* Jakarta Commons Logging(JCL)* APIを使用して、Springアプリケーションでログを生成できます。 JCLはhttps://jakarta.apache.org/commons/logging/からダウンロードできます。 このパッケージから技術的に必要なファイルは_commons-logging-x.y.z.jar_ファイルのみです。このファイルは、上の例で_log4j-x.y.z.jar_を配置したのと同様の方法でクラスパスに配置する必要があります。

ロギング機能を使用するには、_org.apache.commons.logging.Log_オブジェクトが必要です。その後、要件に応じて次のいずれかのメソッドを呼び出すことができます-

  • fatal(オブジェクトメッセージ)
  • エラー(オブジェクトメッセージ)
  • warn(オブジェクトメッセージ)
  • info(オブジェクトメッセージ)
  • debug(オブジェクトメッセージ)
  • trace(オブジェクトメッセージ)

以下は、JCL APIを使用するMainApp.javaの置換です。

package com.finddevguides;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.logging. Log;
import org.apache.commons.logging. LogFactory;

public class MainApp {
   static Log log = LogFactory.getLog(MainApp.class.getName());

   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      log.info("Going to create HelloWord Obj");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();

      log.info("Exiting the program");
   }
}

プログラムをコンパイルして実行する前に、プロジェクトにcommons-logging-x.y.z.jarファイルが含まれていることを確認する必要があります。

上記の例では、残りの構成とコンテンツを変更せずに維持します。アプリケーションをコンパイルして実行すると、Log4J APIを使用した場合と同様の結果が得られます。