| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Java java.util.logging

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Java java.util.logging

Java |

簡易にロギングしたいときのサンプル
package info.typea.sample;

import java.io.ByteArrayInputStream;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class LoggingSample {
	
	protected static final String LOGGING_PROPERTIES_DATA
	    = "handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler\n"
	    + ".level=INFO\n"
	    + "java.util.logging.ConsoleHandler.level=INFO\n"
	    + "java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter\n"
	    + "java.util.logging.FileHandler.pattern=/home/piroto/work/log/LoggingSample%u.log\n"
	    + "java.util.logging.FileHandler.append=true\n"
	    + "java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter\n"
	    ;

	private static Logger logger = null;
	static {
		try {
			LogManager.getLogManager().readConfiguration(
					new ByteArrayInputStream(LOGGING_PROPERTIES_DATA.getBytes("UTF-8")));
			logger = Logger.getLogger(LoggingSample.class.getName());
		} catch(Exception e){
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		LoggingSample me = new LoggingSample();
		me.test();	
	}

	public void test() {
		logger.info("TEST");
	}

}