diff --git a/log.properties b/log.properties new file mode 100644 index 0000000..0a0e6db --- /dev/null +++ b/log.properties @@ -0,0 +1,8 @@ +handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler +.level=INFO +java.util.logging.ConsoleHandler.level=FINEST +java.util.logging.ConsoleHandler.formatter=osm.jp.hayashi.tools.log.YuuLogFormatter +java.util.logging.FileHandler.level=INFO +java.util.logging.FileHandler.pattern=Logging%u.%g.log +java.util.logging.FileHandler.formatter=osm.jp.hayashi.tools.log.YuuLogFormatter +java.util.logging.FileHandler.count=10 diff --git a/src/main/java/osm/jp/hayashi/tools/files/Directory.java b/src/main/java/osm/jp/hayashi/tools/files/Directory.java index cbf2c8f..653c183 100644 --- a/src/main/java/osm/jp/hayashi/tools/files/Directory.java +++ b/src/main/java/osm/jp/hayashi/tools/files/Directory.java @@ -11,7 +11,7 @@ public abstract class Directory { - public static Path getCurrentDirectory(Class cls) throws URISyntaxException { + public static Path getClassDirectory(Class cls) throws URISyntaxException { ProtectionDomain pd = cls.getProtectionDomain(); CodeSource cs = pd.getCodeSource(); URL location = cs.getLocation(); @@ -23,8 +23,11 @@ return path; } - public static Path getCurrentDirectory() throws URISyntaxException { - return getCurrentDirectory(Directory.class); + public static Path getClassDirectory() throws URISyntaxException { + return getClassDirectory(Directory.class); } + public static Path getCurrentDirectory() throws URISyntaxException { + return Paths.get(System.getProperty("user.dir")); + } } diff --git a/src/main/java/osm/jp/hayashi/tools/log/LoggerFactory.java b/src/main/java/osm/jp/hayashi/tools/log/LoggerFactory.java new file mode 100755 index 0000000..ac57164 --- /dev/null +++ b/src/main/java/osm/jp/hayashi/tools/log/LoggerFactory.java @@ -0,0 +1,103 @@ +package osm.jp.hayashi.tools.log; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.logging.FileHandler; +import java.util.logging.LogManager; +import java.util.logging.Logger; + +import osm.jp.hayashi.tools.files.Directory; + +/** + * ロギングファイルに動作ログを出力する簡単なモデル + * "log"+日時+".log"ファイルに出力される。 + * 利用例: + * (1) インスタンスを取得する。 + * Logger logger = LoggerFactory.getInstance(); + * (2) ログ出力例 + * logger.finest("[finest] 詳細レベル(高)"); + * logger.finer("[finer] 詳細レベル(中)"); + * logger.fine("[fine] 詳細レベル(小)"); + * logger.config("[config] 設定"); + * logger.info("[info] 情報"); + * logger.warning("[warning] 警告"); + * logger.severe("[severe] 致命的"); + * @author yuu + * @version 2010/02/07 + * @since 2010/02/07 + */ +public abstract class LoggerFactory +{ + public static void main(String[] args) { + /* + * (設定例) + * handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler + * .level=FINEST + * + * java.util.logging.ConsoleHandler.level=FINEST + * java.util.logging.ConsoleHandler.formatter=hayashi.yuu.tools.logger.YuuLogFormatter + * + * java.util.logging.FileHandler.level=WARNING + * java.util.logging.FileHandler.pattern=SampleLogging%u.%g.log + * java.util.logging.FileHandler.formatter=hayashi.yuu.tools.logger.YuuLogFormatter + * java.util.logging.FileHandler.count=10 + * + * + * 標準設定時でのログ出力。 + * info、warning、severeの3つのレベルのみ標準エラー出力に出力されます。 + * また、同時にファイルへも出力します。 + * 出力先ファイルは「Logging%u.%g.txt」。ログファイルは10個でローテーションする。 + * + * 情報: [info] 情報 + * 警告: [warning] 警告 + * 致命的: [severe] 致命的 + */ + Logger logger; + try { + logger = Logger.getLogger("log"); + FileHandler fHandler = new FileHandler("Sample.log", true); + fHandler.setFormatter(new YuuLogFormatter()); + logger.addHandler(fHandler); + + /* + logger = LoggerFactory.getInstance(); + */ + logger.finest("[finest] 詳細レベル(高)"); + logger.finer("[finer] 詳細レベル(中)"); + logger.fine("[fine] 詳細レベル(小)"); + logger.config("[config] 設定"); + logger.info("[info] 情報"); + logger.warning("[warning] 警告"); + logger.severe("[severe] 致命的"); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * ログ設定プロパティファイルのファイル内容 + * + */ + protected static final String LOGGING_PROPERTIES = "log.properties"; + + /** + * 簡単な標準ロガーを得る + * @return 標準ロガー + */ + public static Logger getInstance() { + final Logger logger = Logger.getLogger("log"); // Loggerオブジェクトの生成 + LogManager manager = LogManager.getLogManager(); + try { + manager.readConfiguration(new FileInputStream(LOGGING_PROPERTIES)); + } catch (SecurityException | IOException e) { + e.printStackTrace(); + } + return logger; + } +} diff --git a/src/main/java/osm/jp/hayashi/tools/log/YuuLogFormatter.java b/src/main/java/osm/jp/hayashi/tools/log/YuuLogFormatter.java new file mode 100755 index 0000000..d51f3fa --- /dev/null +++ b/src/main/java/osm/jp/hayashi/tools/log/YuuLogFormatter.java @@ -0,0 +1,48 @@ +package osm.jp.hayashi.tools.log; + + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.logging.Formatter; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +/** + * シンプルなサンプルログフォーマッタ + */ +public class YuuLogFormatter extends Formatter { + private final SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + + public String format(final LogRecord argLogRecord) { + final StringBuffer buf = new StringBuffer(); + + buf.append(sdFormat.format(new Date(argLogRecord.getMillis())) +" "); + + if (argLogRecord.getLevel() == Level.FINEST) { + buf.append("[FINEST]"); + } + else if (argLogRecord.getLevel() == Level.FINER) { + buf.append("[FINER]"); + } + else if (argLogRecord.getLevel() == Level.FINE) { + buf.append("[FINE]"); + } + else if (argLogRecord.getLevel() == Level.CONFIG) { + buf.append("[CONFIG]"); + } + else if (argLogRecord.getLevel() == Level.INFO) { + buf.append("[INFO]"); + } + else if (argLogRecord.getLevel() == Level.WARNING) { + buf.append("[WARN]"); + } + else if (argLogRecord.getLevel() == Level.SEVERE) { + buf.append("[SEVERE]"); + } + else { + buf.append(Integer.toString(argLogRecord.getLevel().intValue()) +" "); + } + buf.append(" "+ /* argLogRecord.getLoggerName() +" - "+ */ argLogRecord.getMessage() +"\n"); + return buf.toString(); + } +} \ No newline at end of file diff --git a/src/test/java/osm/jp/hayashi/tools/files/DirectoryTest.java b/src/test/java/osm/jp/hayashi/tools/files/DirectoryTest.java index 0bb0774..591200b 100644 --- a/src/test/java/osm/jp/hayashi/tools/files/DirectoryTest.java +++ b/src/test/java/osm/jp/hayashi/tools/files/DirectoryTest.java @@ -31,10 +31,24 @@ } @Test - public void test() { + public void testCurrent() { try { Path path = Directory.getCurrentDirectory(); - System.out.print(path.toAbsolutePath().toString()); + System.out.println("CurrentDirectory: "+ path.toAbsolutePath().toString()); + assertThat(path.isAbsolute(), is(true)); + assertThat(Files.isDirectory(path), is(true)); + assertThat(Files.exists(path), is(true)); + } + catch (Exception e) { + fail(); + } + } + + @Test + public void testClass() { + try { + Path path = Directory.getClassDirectory(); + System.out.println("ClassDirectory: " + path.toAbsolutePath().toString()); assertThat(path.isAbsolute(), is(true)); assertThat(Files.isDirectory(path), is(true)); assertThat(Files.exists(path), is(true)); diff --git a/src/test/java/osm/jp/hayashi/tools/log/LoggerFactoryTest.java b/src/test/java/osm/jp/hayashi/tools/log/LoggerFactoryTest.java new file mode 100644 index 0000000..2fd1dde --- /dev/null +++ b/src/test/java/osm/jp/hayashi/tools/log/LoggerFactoryTest.java @@ -0,0 +1,165 @@ +package osm.jp.hayashi.tools.log; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.function.Consumer; +import java.util.logging.FileHandler; +import java.util.logging.Logger; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; + +public class LoggerFactoryTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + Path path = Paths.get("test1.log"); + if (Files.exists(path)) { + path.toFile().delete(); + } + + path = Paths.get("Logging%u.%g.log"); + if (Files.exists(path)) { + path.toFile().delete(); + } + + try { + Path dir = Paths.get(System.getProperty("user.dir")); + Files.list(dir).forEach(new Consumer() { + @Override + public void accept(Path a) { + if (!Files.isDirectory(a)) { + String name = a.getFileName().toString(); + if (name.startsWith("Logging") && name.endsWith(".log")) { + a.toFile().delete(); + } + } + } + }); + } catch (IOException e) {} + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test1() { + Logger logger; + try { + logger = Logger.getLogger("log"); + FileHandler fHandler = new FileHandler("test1.log", true); + fHandler.setFormatter(new YuuLogFormatter()); + logger.addHandler(fHandler); + + logger.finest("詳細レベル(高)"); + logger.finer("詳細レベル(中)"); + logger.fine("詳細レベル(小)"); + logger.config("設定"); + logger.info("情報"); + logger.warning("警告"); + logger.severe("致命的"); + } + catch (Exception e) { + fail(); + } + Path path = Paths.get("test1.log"); + assertThat(Files.exists(path), is(true)); + assertThat(Files.isDirectory(path), is(false)); + try { + final BufferedReader fileReader = Files.newBufferedReader(path); + + String data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 詳細レベル(高)"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 詳細レベル(中)"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 詳細レベル(小)"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 設定"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 情報"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 警告"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 致命的"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(true)); + } catch (IOException e) { + fail(); + } + } + + @Test + public void test2() { + Logger logger; + try { + logger = LoggerFactory.getInstance(); + logger.finest("詳細レベル(高)"); + logger.finer("詳細レベル(中)"); + logger.fine("詳細レベル(小)"); + logger.config("設定"); + logger.info("情報"); + logger.warning("警告"); + logger.severe("致命的"); + } + catch (Exception e) { + fail(); + } + Path path = Paths.get("Logging0.0.log"); + assertThat(Files.exists(path), is(true)); + assertThat(Files.isDirectory(path), is(false)); + try { + final BufferedReader fileReader = Files.newBufferedReader(path); + String data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 情報"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 警告"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(false)); + assertThat(data.endsWith(" 致命的"), is(true)); + + data = fileReader.readLine(); + assertThat(data == null, is(true)); + } catch (IOException e) { + fail(); + } + } + +}