package com.emr.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class Logger {

    @Value("${export_pdf_path}")
    private String exportPdfPath;

    public void log(String info) {
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
        String format = dateFormat.format (new Date());
        File file = new File (exportPdfPath+format);
        if(!file.isDirectory ()){
            file.mkdirs ();
        }
        OutputStream out = null;
        try {
            out = getOutputStream(file.getAbsolutePath ()+"\\log.log");
            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);
        }
    }
}
