package LogManager;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class LogTrace {
private String _filename;
private Logger _logger;
private FileHandler _fHandler;
public LogTrace(String filename){
this._filename = filename;
this._logger = java.util.logging.Logger.getLogger("net.anfamily.logging");
}
public LogTrace(String filename, java.util.logging.Logger logger){
this._filename = filename;
this._logger = logger;
}
/**
* 로그를 저장한다.
*/
public void writeLog(String log){
try{
this._fHandler = new java.util.logging.FileHandler(this._filename,true);
_logger.addHandler(this._fHandler);
_logger.setLevel(java.util.logging.Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
//로그 메시지를 남긴다.
_logger.log(Level.FINER,log);
_logger.log(Level.FINE,log);
_logger.log(Level.FINER,log);
_logger.log(Level.CONFIG,log);
_logger.log(Level.INFO,log);
_logger.log(Level.WARNING,log);
_logger.log(Level.SEVERE,log);
}catch(Exception e){
e.printStackTrace();
System.out.println("");
}
}
/**
*로그를 저장한다.
* @param log
* @param filename
*/
public void writeLog(String log, String filename){
try{
this._fHandler = new java.util.logging.FileHandler(this._filename,true);
_logger.addHandler(this._fHandler);
_logger.setLevel(java.util.logging.Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
this._fHandler.setFormatter(formatter);
_logger.log(Level.FINER,log);
_logger.log(Level.FINER,log);
_logger.log(Level.FINE,log);
_logger.log(Level.CONFIG,log);
_logger.log(Level.INFO,log);
_logger.log(Level.WARNING,log);
_logger.log(Level.SEVERE,log);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 로그를 저장한다.
* @param log
* @param logger
* @param handler
*/
public static void writeLog(String log,Logger logger, ConsoleHandler handler){
try{
logger.addHandler(handler);
logger.setLevel(java.util.logging.Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
handler.setFormatter(formatter);
logger.log(Level.FINEST,log);
logger.log(Level.FINER,log);
logger.log(Level.FINE,log);
logger.log(Level.CONFIG,log);
logger.log(Level.INFO,log);
logger.log(Level.WARNING,log);
logger.log(Level.SEVERE,log);
}catch(Exception e){
e.printStackTrace();
System.out.println("");
}
}
/**
* 로그를 파일로 저장한다.
* @param filename
* @param log
* @param logger
*/
public static void writeLog(String log,Logger logger,String filename){
try{
FileHandler handler = new java.util.logging.FileHandler(filename,true);
logger.addHandler(handler);
logger.setLevel(java.util.logging.Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
handler.setFormatter(formatter);
logger.log(Level.FINEST,log);
logger.log(Level.FINER,log);
logger.log(Level.FINE,log);
logger.log(Level.CONFIG,log);
logger.log(Level.INFO,log);
logger.log(Level.SEVERE,log);
}catch(Exception e){
e.printStackTrace();
System.out.println("");
}
}
}
==========================================================================================
package LogManager;
import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;
public class LogingTest {
public LogingTest(){
}
public static void main(String [] args){
LogTrace logs = new LogTrace("c:\\log_Test100917.txt");
logs.writeLog("Test입니다.");
LogTrace.writeLog("Test입니다._1", Logger.getLogger("new.anfamily.net"), new ConsoleHandler());
LogTrace.writeLog("Test입니다._2", Logger.getLogger("new.anfamily.net"), "c:\\log_Test100917_1.txt");
}
}
=======================================================================================
실행 결과
2010. 9. 17 오전 11:15:31 LogManager.LogTrace writeLog
아주 자세히: Test입니다._2
2010. 9. 17 오전 11:15:31 LogManager.LogTrace writeLog
더 자세히: Test입니다._2
2010. 9. 17 오전 11:15:31 LogManager.LogTrace writeLog
자세히: Test입니다._2
2010. 9. 17 오전 11:15:31 LogManager.LogTrace writeLog
구성: Test입니다._2
2010. 9. 17 오전 11:15:31 LogManager.LogTrace writeLog
정보: Test입니다._2
2010. 9. 17 오전 11:15:31 LogManager.LogTrace writeLog
심각: Test입니다._2
'Java' 카테고리의 다른 글
[ Java ] StringTokenizer 와 String.split()의 차이 (0) | 2010.09.17 |
---|---|
Java Map(HashMap, TreeMap, Hashtable) (0) | 2010.09.17 |
쿠키로 아이디 암호 기억 시켜 불러오는방법 (0) | 2010.08.24 |
TCP/IP Socket 프로그램 구현시 고려사항공부/Java (0) | 2010.08.19 |
httpsession 을 이용한 로그인 관리 (0) | 2010.08.13 |