Java 线程中记录日志

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; public class ThreadLog { private final static String _detailLogFile = "log"+File.separator+"detail"; private static boolean _logFlag = true; private static HashMap _threadDetailFile = new HashMap(); private static int _fileIndex = 1; private static void generateLogFile(String fileName) { String absoluteFilePath = ProjectConfig.getNFSCAbsolutePath() + File.separator + "log" + File.separator +fileName; _threadDetailFile.put(String.valueOf(Thread.currentThread().getId()) + "_" + fileName, absoluteFilePath); File file = new File(absoluteFilePath); //create a new file if(!file.exists()) { try { file.createNewFile(); } catch(IOException ex) { ExceptionDefaultHandler.handle(ex, "threadlog.log"); } } } private static void generateLogFile() { String fileName = _detailLogFile + DateTimeUtil.getCurrentDateFormatString("yyyyMMddHHmmss") +"_"+ String.valueOf(_fileIndex++)+ ".log"; String absoluteFilePath = ProjectConfig.getNFSCAbsolutePath() + File.separator + fileName; _threadDetailFile.put(String.valueOf(Thread.currentThread().getId()), absoluteFilePath); File file = new File(absoluteFilePath); //if the file exist, we have to remove it. if(file.exists()) { try { file.delete(); } catch(SecurityException ex) { ExceptionDefaultHandler.handle(ex, "threadlog.log"); } } //create a new file try { file.createNewFile(); } catch(IOException ex) { ExceptionDefaultHandler.handle(ex, "threadlog.log"); } } private static boolean hasThreadLogFile() { return _threadDetailFile.containsKey(String.valueOf(Thread.currentThread().getId())); } private static boolean hasThreadLogFile(String fileName) { return _threadDetailFile.containsKey(String.valueOf(Thread.currentThread().getId()) + "_" + fileName); } public static void MakeLog(String detail, String fileName) { if(!_logFlag) { return; } if(!hasThreadLogFile(fileName)) { generateLogFile(fileName); } File file = new File(_threadDetailFile.get(String.valueOf(Thread.currentThread().getId()) + "_" + fileName)); if(file.canWrite()) { boolean appendFlag = true; if(file.length() > 1000000) { appendFlag = false; } PrintWriter streamWriter = null; try { streamWriter = new PrintWriter(new FileOutputStream(file, appendFlag)); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(DateTimeUtil.getCurrentDateLocalFormatString()); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); streamWriter.write(detail); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); streamWriter.close(); } catch(IOException ex) { ExceptionDefaultHandler.handle(ex, "threadlog.log"); } finally { streamWriter.close(); } } else { System.out.println("Do not have the permission to write into the "+ _threadDetailFile.get(String.valueOf(Thread.currentThread().getId()))+""); } } public static void MakeLog(String detail) { if(!_logFlag) { return; } if(!hasThreadLogFile()) { generateLogFile(); } File file = new File(_threadDetailFile.get(String.valueOf(Thread.currentThread().getId()))); if(file.canWrite()) { boolean appendFlag = true; if(file.length() > 1000000) { appendFlag = false; } PrintWriter streamWriter = null; try { streamWriter = new PrintWriter(new FileOutputStream(file, appendFlag)); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(DateTimeUtil.getCurrentDateLocalFormatString()); streamWriter.write("----------------------------------------------------------------"); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); streamWriter.write(detail); streamWriter.write(SystemProperties.newInstance().getLineSeparator()); streamWriter.close(); } catch(IOException ex) { ExceptionDefaultHandler.handle(ex, "threadlog.log"); } finally { streamWriter.close(); } } else { System.out.println("Do not have the permission to write into the "+ _threadDetailFile.get(String.valueOf(Thread.currentThread().getId()))+""); } } }  

你可能感兴趣的:(Java)