Java 線程中記錄日誌

來源:互聯網
上載者:User

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<String, String> _threadDetailFile = new HashMap<String, String>(); 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()))+""); } } }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.