diff --git a/src/main/java/com/github/ucchyocean/lc/Channel.java b/src/main/java/com/github/ucchyocean/lc/Channel.java index 552e032..53eaae9 100644 --- a/src/main/java/com/github/ucchyocean/lc/Channel.java +++ b/src/main/java/com/github/ucchyocean/lc/Channel.java @@ -8,16 +8,10 @@ package com.github.ucchyocean.lc; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.logging.FileHandler; -import java.util.logging.LogRecord; -import java.util.logging.Logger; -import java.util.logging.SimpleFormatter; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -73,9 +67,6 @@ public class Channel implements ConfigurationSerializable { private static final String KEY_WORLD = "world"; private static final String KEY_RANGE = "range"; - private static final SimpleDateFormat dformat = - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - /** 参加者 */ private List members; @@ -121,7 +112,7 @@ public class Channel implements ConfigurationSerializable { private int chatRange; /** ロガー */ - private Logger logger; + private LunaChatLogger logger; /** * コンストラクタ @@ -144,7 +135,7 @@ public class Channel implements ConfigurationSerializable { this.format = DEFAULT_FORMAT_FOR_PERSONAL; } else { this.format = DEFAULT_FORMAT; - logger = makeLoggerForChannelChatLog(name); + logger = new LunaChatLogger(name); } } @@ -923,54 +914,7 @@ public class Channel implements ConfigurationSerializable { Bukkit.getLogger().info(ChatColor.stripColor(message)); } if ( LunaChat.config.isLoggingChat() && logger != null ) { - logger.info(ChatColor.stripColor(message)); + logger.log(ChatColor.stripColor(message)); } } - - /** - * チャンネルチャットロギングのためのロガーを作成して取得する。 - * @param name チャンネル名 - * @return ロガー - */ - private static Logger makeLoggerForChannelChatLog(final String name) { - - Logger logger = Logger.getLogger("LunaChatChannelLogger" + name); - - FileHandler handler = null; - try { - File dir = new File(LunaChat.instance.getDataFolder(), "logs"); - if ( !dir.exists() ) { - dir.mkdirs(); - } - - handler = new FileHandler( - dir.getAbsolutePath() + "\\" + name + ".%g.log", - 1048576, 1000, true); - handler.setFormatter(new SimpleFormatter() { - @Override - public String format(LogRecord record) { - String date = dformat.format(new Date()); - return String.format("%1$s,%2$s\r\n", - date, record.getMessage()); - } - }); - - logger.addHandler(handler); - logger.setUseParentHandlers(false); - - return logger; - - } catch (SecurityException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { -// if ( handler != null ) { -// handler.flush(); -// handler.close(); -// } - } - - return null; - } } diff --git a/src/main/java/com/github/ucchyocean/lc/LunaChatLogger.java b/src/main/java/com/github/ucchyocean/lc/LunaChatLogger.java new file mode 100644 index 0000000..f2b557a --- /dev/null +++ b/src/main/java/com/github/ucchyocean/lc/LunaChatLogger.java @@ -0,0 +1,85 @@ +/* + * @author ucchy + * @license LGPLv3 + * @copyright Copyright ucchy 2013 + */ +package com.github.ucchyocean.lc; + +import java.io.File; +import java.io.FileWriter; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * LunaChatロガー + * @author ucchy + */ +public class LunaChatLogger { + + private static final SimpleDateFormat lformat = + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private static final SimpleDateFormat dformat = + new SimpleDateFormat("yyyy-MM-dd"); + + private File file; + private String dirPath; + private String name; + + /** + * コンストラクタ + * @param name ログ名 + */ + public LunaChatLogger(String name) { + + this.name = name; + checkDir(); + } + + /** + * ログを出力する + * @param message ログ内容 + */ + public void log(String message) { + + checkDir(); + + FileWriter writer = null; + try { + writer = new FileWriter(file, true); + String str = lformat.format(new Date()) + "," + message; + writer.write(str + "\r\n"); + writer.flush(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if ( writer != null ) { + try { + writer.close(); + } catch (Exception e) { + // do nothing. + } + } + } + } + + /** + * ログの出力先フォルダをチェックし、変更されるなら更新する。 + */ + private void checkDir() { + + String temp = LunaChat.instance.getDataFolder() + File.separator + + "logs" + File.separator + dformat.format(new Date()); + + if ( temp.equals(dirPath) ) { + return; + } + dirPath = temp; + + File dir = new File(dirPath); + if ( !dir.exists() || !dir.isDirectory() ) { + dir.mkdirs(); + } + + file = new File(dir, name + ".log"); + } +}