package com.docus.server.collection.util;

import com.docus.core.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Date;

/**
 * @author wyb
 */
public class PersistRequestUtil {
    private final static Logger logger = LoggerFactory.getLogger(PersistRequestUtil.class);

    public static void persistRequest(String method, String content) {
        String persistDir = currentPath() + File.separator + "persist";
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            File persistDirFile = new File(persistDir);
            if (!persistDirFile.exists()) {
                persistDirFile.mkdirs();
            }
            String persistFilePath = persistDir + File.separator + method;
            fileWriter = new FileWriter(persistFilePath, true);
            bufferedWriter = new BufferedWriter(fileWriter);
            content = "[" + DateUtil.formatDateTime(new Date()) + "] " + content + "\n";
            bufferedWriter.write(content);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (Exception ex) {
                }
            }
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (Exception ex) {
                }
            }
        }
    }

    public static void main(String[] args) {
        persistRequest("test","{xxcc}");
    }
    public static String currentPath() {
        File dir = new File(".");
        String currentpath = "";

        try {
            currentpath = dir.getCanonicalPath();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return currentpath;
    }
}
