日志输出

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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/aiyongbo123456/article/details/81333146
今日推荐