日誌輸出
阿新 • • 發佈:2018-12-30
package com.ccservice.train.Reptile_mobile.Utils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Calendar; import java.util.Enumeration; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class WriteLogNew { public static String jsonArray_ip; public static String osname; public static String pathString_root; static { osname = System.getProperty("os.name"); pathString_root = "D:/userlog/"; if (osname != null && osname.contains("Linux")) { pathString_root = "userlog/"; } JSONArray jsonArray = getLocalHostLANAddress(); jsonArray_ip = getjsonArray_ip(jsonArray); } /** * 寫日誌<br> * * 寫logString字串到./log目錄下的檔案中 * * @param logString * 日誌字串 * * @author tower * */ public static void write_old(String fileNameHead, String logString) { try { String logFilePathName = null; Calendar cd = Calendar.getInstance();// 日誌檔案時間 int year = cd.get(Calendar.YEAR); String month = addZero(cd.get(Calendar.MONTH) + 1); String day = addZero(cd.get(Calendar.DAY_OF_MONTH)); String hour = addZero(cd.get(Calendar.HOUR_OF_DAY)); String min = addZero(cd.get(Calendar.MINUTE)); String sec = addZero(cd.get(Calendar.SECOND)); String mill = addZero(cd.get(Calendar.MILLISECOND)); String pathString = pathString_root + year + month + "/" + day + "/" + hour;//"D:/userlog/" + year + month + "/" + day + "/" + hour; // if (osname != null && osname.contains("Linux")) { // pathString = "userlog/" + year + month + "/" + day + "/" + hour; // } String path = pathString;//"D:/userlog/" + year + month + "/" + day + "/" + hour; File fileParentDir = new File(path);// 判斷log目錄是否存在 if (!fileParentDir.exists()) { fileParentDir.mkdirs(); } if (fileNameHead == null || fileNameHead.equals("")) { logFilePathName = path + "/" + year + month + day + ".log";// 日誌檔名 } else { logFilePathName = path + "/" + fileNameHead + ".log";// 日誌檔名 } PrintWriter printWriter = new PrintWriter(new FileOutputStream(logFilePathName, true));// 緊接檔案尾寫入日誌字串 String time = "[" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + mill + "] "; printWriter.println(time + logString); printWriter.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.getMessage(); } } private static String getjsonArray_ip(JSONArray jsonArray) { String s = ""; for (int i = 0; i < jsonArray.size(); i++) { s += jsonArray.getString(i); if (i < jsonArray.size() - 1) { s += "|"; } } return s; } /** * * @param foldername 資料夾名字 * @param fileNameHead 檔名 * @param logString 內容 */ public static void write_old(String foldername, String fileNameHead, String logString) { try { String logFilePathName = null; Calendar cd = Calendar.getInstance();// 日誌檔案時間 int year = cd.get(Calendar.YEAR); String month = addZero(cd.get(Calendar.MONTH) + 1); String day = addZero(cd.get(Calendar.DAY_OF_MONTH)); String hour = addZero(cd.get(Calendar.HOUR_OF_DAY)); String min = addZero(cd.get(Calendar.MINUTE)); String sec = addZero(cd.get(Calendar.SECOND)); String mill = addZero(cd.get(Calendar.MILLISECOND)); // String path = "D:/userlog/" + year + month + "/" + day + "/" + hour; String path = pathString_root + year + month + "/" + day + "/" + hour;//"D:/userlog/" + year + month + "/" + day + "/" + hour; File fileParentDir = new File(path + "/" + foldername);// 判斷log目錄是否存在 if (!fileParentDir.exists()) { fileParentDir.mkdirs(); } if (fileNameHead == null || fileNameHead.equals("")) { logFilePathName = path + "/" + foldername + "/" + year + month + day + ".log";// 日誌檔名 } else { logFilePathName = path + "/" + foldername + "/" + fileNameHead + ".log";// 日誌檔名 } PrintWriter printWriter = new PrintWriter(new FileOutputStream(logFilePathName, true));// 緊接檔案尾寫入日誌字串 String time = "[" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + mill + "] "; printWriter.println(time + logString); printWriter.flush(); } catch (FileNotFoundException e) { e.getMessage(); e.printStackTrace(); } } /** * * @param foldername 資料夾名字 * @param fileNameHead 檔名 * @param logString 內容 */ public static void writeJson(String fileNameHead, String logString, String index) { PrintWriter printWriter = null; try { String logFilePathName = null; Calendar cd = Calendar.getInstance();// 日誌檔案時間 int year = cd.get(Calendar.YEAR); String month = addZero(cd.get(Calendar.MONTH) + 1); String day = addZero(cd.get(Calendar.DAY_OF_MONTH)); String hour = addZero(cd.get(Calendar.HOUR_OF_DAY)); String min = addZero(cd.get(Calendar.MINUTE)); String sec = addZero(cd.get(Calendar.SECOND)); String mill = addZero(cd.get(Calendar.MILLISECOND)); String pathString = pathString_root + year + month + "/" + day + "/" + hour;//"D:/userlog/" + year + month + "/" + day + "/" + hour; // if (osname != null && osname.contains("Linux")) { // pathString = "userlog/" + year + month + "/" + day + "/" + hour; // } String path = pathString;//"D:/userlog/" + year + month + "/" + day + "/" + hour; File fileParentDir = new File(path);// 判斷log目錄是否存在 if (!fileParentDir.exists()) { fileParentDir.mkdirs(); } if (fileNameHead == null || fileNameHead.equals("")) { logFilePathName = path + "/" + year + month + day + ".log";// 日誌檔名 } else { logFilePathName = path + "/" + fileNameHead + ".log";// 日誌檔名 } printWriter = new PrintWriter(new FileOutputStream(logFilePathName, true));// 緊接檔案尾寫入日誌字串 String time = "[" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + mill + "] "; time = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + mill; time = year + month + day + hour + min + sec + mill; String s_println = "";//time + logString; JSONObject jsonObject = new JSONObject(); jsonObject.put("time", System.currentTimeMillis()); jsonObject.put("timeString", Long.parseLong(time)); jsonObject.put("channel", 0);//APP雲監控 jsonObject.put("index", index); jsonObject.put("fileNameHead", fileNameHead); jsonObject.put("logString", logString); jsonObject.put("ipInfo", jsonArray_ip); s_println = jsonObject.toJSONString(); printWriter.println(s_println); } catch (FileNotFoundException e) { e.getMessage(); } finally { if (printWriter != null) { printWriter.flush(); printWriter.close(); } else { System.out.println("請檢查硬碟空間是否滿!"); } } } /** * 整數i小於10則前面補0 * * @param i * * @return * * @author tower * */ public static String addZero(int i) { if (i < 10) { String tmpString = "0" + i; return tmpString; } else { return String.valueOf(i); } } public static JSONArray getLocalHostLANAddress() { //get all local ips Enumeration<NetworkInterface> interfs = null; JSONArray jsonArray = new JSONArray(); try { interfs = NetworkInterface.getNetworkInterfaces(); while (interfs.hasMoreElements()) { NetworkInterface interf = interfs.nextElement(); Enumeration<InetAddress> addres = interf.getInetAddresses(); while (addres.hasMoreElements()) { InetAddress in = addres.nextElement(); if (in instanceof Inet4Address) { jsonArray.add(in.getHostAddress()); // System.out.println("v4:" + in.getHostAddress()); } else if (in instanceof Inet6Address) { // System.out.println("v6:" + in.getHostAddress()); } } } } catch (SocketException e) { e.printStackTrace(); } return jsonArray; } /** * 根據網絡卡名字獲取到網絡卡的ip * @param networkInterfaceName * @return * @author */ public static String getIpByNetworkInterfaceName(String networkInterfaceName) { //get all local ips String ip = ""; Enumeration<NetworkInterface> interfs = null; JSONArray jsonArray = new JSONArray(); try { interfs = NetworkInterface.getNetworkInterfaces(); while (interfs.hasMoreElements()) { NetworkInterface interf = interfs.nextElement(); if (networkInterfaceName.equals(interf.getName())) { Enumeration<InetAddress> addres = interf.getInetAddresses(); boolean isBreak = false; while (addres.hasMoreElements()) { InetAddress in = addres.nextElement(); if (in instanceof Inet4Address) { jsonArray.add(in.getHostAddress()); ip = in.getHostAddress(); isBreak = true; break; // System.out.println("v4:" + in.getHostAddress()); } else if (in instanceof Inet6Address) { // System.out.println("v6:" + in.getHostAddress()); } } if (isBreak) { break; } } } } catch (SocketException e) { e.printStackTrace(); } return ip; } public static void writeJson2_0(String fileNameHead, String logString, String orderid, String sequence_no, String datatypeflag, String biztype, String username) { writeJson2_0(fileNameHead, logString, orderid, sequence_no, datatypeflag, biztype, username, 3); } /** * * @param fileNameHead 日誌檔名 * @param logString log內容 * @param orderid 訂單id * @param sequence_no 電子單號 * @param datatypeflag 請求介面型別 * @param biztype 業務型別 * @param username 12306使用者名稱 * @param channel 3);//APP代購 4雲監控 * @author */ public static void writeJson2_0(String fileNameHead, String logString, String orderid, String sequence_no, String datatypeflag, String biztype, String username, int channel) { PrintWriter printWriter = null; try { String logFilePathName = null; Calendar cd = Calendar.getInstance();// 日誌檔案時間 int year = cd.get(Calendar.YEAR); String month = addZero(cd.get(Calendar.MONTH) + 1); String day = addZero(cd.get(Calendar.DAY_OF_MONTH)); String hour = addZero(cd.get(Calendar.HOUR_OF_DAY)); String min = addZero(cd.get(Calendar.MINUTE)); String sec = addZero(cd.get(Calendar.SECOND)); String mill = addZero(cd.get(Calendar.MILLISECOND)); String pathString = pathString_root + year + month + "/" + day + "/" + hour;//"D:/userlog/" + year + month + "/" + day + "/" + hour; // if (osname != null && osname.contains("Linux")) { // pathString = "userlog/" + year + month + "/" + day + "/" + hour; // } String path = pathString;//"D:/userlog/" + year + month + "/" + day + "/" + hour; File fileParentDir = new File(path);// 判斷log目錄是否存在 if (!fileParentDir.exists()) { fileParentDir.mkdirs(); } if (fileNameHead == null || fileNameHead.equals("")) { logFilePathName = path + "/" + year + month + day + ".log";// 日誌檔名 } else { logFilePathName = path + "/" + fileNameHead + ".log";// 日誌檔名 } printWriter = new PrintWriter(new FileOutputStream(logFilePathName, true));// 緊接檔案尾寫入日誌字串 String time = "[" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + mill + "] "; time = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + mill; time = year + month + day + hour + min + sec + mill; String s_println = "";//time + logString; JSONObject jsonObject = new JSONObject(); jsonObject.put("time", System.currentTimeMillis()); jsonObject.put("timeString", Long.parseLong(time)); jsonObject.put("channel", channel); // 3:APP代購 jsonObject.put("logString", logString); jsonObject.put("ipInfo", jsonArray_ip); jsonObject.put("orderid", orderid); jsonObject.put("sequence_no", sequence_no); jsonObject.put("datatypeflag", datatypeflag); jsonObject.put("biztype", biztype); jsonObject.put("username", username); s_println = jsonObject.toJSONString(); printWriter.println(s_println); } catch (FileNotFoundException e) { e.getMessage(); } finally { if (printWriter != null) { printWriter.flush(); printWriter.close(); } } } /** * 測試用的,用於記錄小塊功能塊耗時明細 * * @param cdn * @param info * @param orderId * @param proxyIP * @param costTime * @return * @time 2018年7月4日 上午11:10:05 * @author fiend */ public static String logInformationByJson(String cdn, String info, String orderId, String proxyIP, long costTime) { JSONObject jsonObject = new JSONObject(); jsonObject.put("cdn", cdn); jsonObject.put("info", info); jsonObject.put("orderId", orderId); jsonObject.put("proxyIP", proxyIP); jsonObject.put("costTime", costTime); return jsonObject.toJSONString(); } public static String GetJson_yjk_ysb(String cdn, String 請求12306的步驟, String 訂單ID, String 代理IP, String 請求開始時間, String 請求結束時間, String 請求結果) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("cdn", cdn); jsonObject.put("請求12306的步驟", 請求12306的步驟); jsonObject.put("訂單ID", 訂單ID); jsonObject.put("代理IP", 代理IP); jsonObject.put("請求開始時間", 請求開始時間); jsonObject.put("請求結束時間", 請求結束時間); jsonObject.put("請求結果", "");//請求結果); } catch (Exception e) { e.printStackTrace(); } return jsonObject.toJSONString(); } public static void main(String[] args) { try { // JSONArray jsonArray = getLocalHostLANAddress(); // System.out.println(jsonArray); // WriteLogNew.writeJson2_0("writeJson2_0_tets", "記錄的log", "123", "E660288606", "1013", "下單", "username"); // System.out.println(Long.MAX_VALUE); // List<String> listCdn = new ArrayList<String>(); // listCdn.add("1"); // System.out.println(listCdn.toString()); } catch (Exception e) { e.printStackTrace(); } } }