q. Logging with Log4J - kimxavi/spring_tutorial GitHub Wiki

μŠ€ν”„λ§ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ Log4J κΈ°λŠ₯을 μ‚¬μš©ν•˜λŠ” 것은 μ•„μ£Ό 쉽닀. μ•„λž˜λŠ” Log4Jκ³Ό μŠ€ν”„λ§ 간에 μš΄μ˜μ— λŒ€ν•œ κ°„λ‹¨ν•œ μ˜ˆμ΄λ‹€. HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

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

MainApp.java

package com.tutorialspoint;

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.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

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

PRINT

Your Message : Hello World!

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)λ₯Ό μ‚¬μš©ν•΄μ„œ 둜그 μ°λŠ” 것을 λŒ€μ²΄ν•  수 μžˆλ‹€. 둜그 κΈ°λŠ₯을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œ rg.apache.commons.logging.Log 객체가 ν•„μš”ν•˜λ‹€. 그리고 μ•„λž˜ λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λœλ‹€.

  • fatal(Object message)
  • error(Object message)
  • warn(Object message)
  • info(Object message)
  • debug(Object message)
  • trace(Object message)

MainApp.javaλ₯Ό λŒ€μ²΄ν•΄μ„œ μž‘μ„±ν•œ 것이닀.

package com.tutorialspoint;

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");
   }
}
⚠️ **GitHub.com Fallback** ⚠️