상세 컨텐츠

본문 제목

java.util.logging.Logger example

소프트웨어/java

by 야솔아빠 2014. 4. 10. 08:48

본문

반응형


상위 class

package com.asiana.pip;


import java.io.IOException;


import java.util.logging.FileHandler;

import java.util.logging.Level;

import java.util.logging.Logger;

import java.util.logging.SimpleFormatter;


public class LoggerTest1 {

  private Logger logger;

  

  public LoggerTest1() {

    super();

    

    /**

     * Logger setting

     */

    this.logger = Logger.getLogger("com.asiana.pip.LoggerTest1");

    FileHandler handler = null;

    try {

      handler = new FileHandler("./log/LoggerTest1.%g.log", 1024*1024*10, 10);

    } catch (IOException e) {

      e.printStackTrace();

    }

    handler.setFormatter(new SimpleFormatter());

    this.logger.addHandler(handler);

    this.logger.setLevel(Level.CONFIG);

  }

  

  public void logging() {

    this.logger.log(Level.INFO, "LoggerTest1 Log.");

  }

}




하위 class

package com.asiana.pip.log;


import com.asiana.pip.LoggerTest1;


import java.io.IOException;


import java.util.logging.FileHandler;

import java.util.logging.Level;

import java.util.logging.Logger;

import java.util.logging.SimpleFormatter;


public class LoggerTestChild {

  private Logger logger;

  

  public LoggerTestChild() {

    super();

    

    /**

     * Logger setting

     */

    this.logger = Logger.getLogger("com.asiana.pip.LoggerTest1.LoggerTestChild");

    

  }

  

  public void logging() {

    this.logger.log(Level.INFO, "LoggerTest1 Log.");

  }

  

  public static void main(String args[]) {

    LoggerTest1 test1= new LoggerTest1();

    LoggerTestChild child = new LoggerTestChild();

    

    test1.logging();

    child.logging();

    child.logging();

    test1.logging();

    

  }

}




결과 log 파일(LoggerTest1.0.log)

2014. 4. 10 오전 8:42:11 com.asiana.pip.LoggerTest1 logging

정보: LoggerTest1 Log.

2014. 4. 10 오전 8:42:12 com.asiana.pip.log.LoggerTestChild logging

정보: LoggerTest1 Log.

2014. 4. 10 오전 8:42:12 com.asiana.pip.log.LoggerTestChild logging

정보: LoggerTest1 Log.

2014. 4. 10 오전 8:42:12 com.asiana.pip.LoggerTest1 logging

정보: LoggerTest1 Log.


반응형

관련글 더보기

댓글 영역