package com.emr.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Logger {
    public void log(String info) {
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format (new Date());
        File file = new File ("D:\\emr_medical_record\\logs\\"+format);
        if(!file.isDirectory ()){
            file.mkdirs ();
        }
        OutputStream out = null;
        try {
            out = getOutputStream(file.getAbsolutePath ()+"\\log.log");
            // 写入日期和日志信息
            out.write(dateFormat1.format(new Date()).getBytes("utf-8")); // 添加
            out.write(info.getBytes("utf-8"));
            out.write("\r\n".getBytes());
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public OutputStream getOutputStream(String localpath) throws IOException {
        File file = new File(localpath);
        if (!file.exists()) {
            file.createNewFile();
            return new FileOutputStream(file);
        } else {
            return new FileOutputStream(file, true);
        }
    }
}
