Spring Boot日志文件可以由application.properties设置,但不能由System.setProperty(“ logging.file”,name)设置

时间:2019-01-06 12:58:22

标签: spring-boot log4j slf4j

我有一个Spring boot应用程序,可以从application.properties中读取硬编码的日志文件名,如下所示:

选项1 :(工作正常)

        File file = new File(Environment.getExternalStorageDirectory().toString());
    file.mkdir();

    if (file != null) {
        Log.d("created", "notnull");
    } else {
        Log.d("created", "null");
    }

    File gpxfile = new File(file, "samples.txt");
    FileWriter writer = null;
    try {
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(gpxfile), Charset.forName("ASCII")));
        //writer = new FileWriter(gpxfile);
        //writer.write(billHTML());
        out.write(billHTML());
        //Log.d("writer", "" + billHTML());
        //writer.close();
        out.close();

       **here i read file** 
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(gpxfile), "ASCII"));
        String sData;
        while ((sData = in.readLine()) != null) {
            System.out.println(sData);
        }
        Log.d("files", "" + in);

    } catch (IOException e) {
        e.printStackTrace();
    }

选项2 :(不起作用)

由于某种原因,如果我将其设置为

    public String billHTML() {

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");

    Calendar c = Calendar.getInstance();

    StringBuilder sb = new StringBuilder();
    sb.append("<html>"
            + "<body>");
    sb.append("<br>");
    sb.append("<h1><br><center> Texto mi marca </<center></h1>");
    sb.append("<h2><center> Texto mi marca </<center></h2>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<center>Fecha:  &emsp;" + sdf.format(c.getTime()));
    sb.append("<br>");
    sb.append("<center>Factura:  &emsp;" + sdf.format(c.getTime()));
    sb.append("<br>");
    sb.append("<center>Cllente al contado");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<h2><center>-------------------------------------------</h2>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<br>");
    sb.append("<table width=550>" +
            "  <tr>" +
            "    <th  width='50%' align='left'> Description</th>" +
            "    <th  width='10%' align='left'>Units</th>" +
            "    <th  width='10%' align='right'>Price</th>" +
            "  </tr>");
    for (int i = 0; i < Util.itemArrayList.size(); i++) {
        sb.append("<tr>"
                + "<td>").append(Util.itemArrayList.get(i).description)
                .append("</td>"
                        + "<td align='left'>")
                .append(Util.itemArrayList.get(i).units)
                .append("</td>"
                        + "<td align='right'>")
                .append(Util.itemArrayList.get(i).price + "€")
                .append("</td>"
                        + "</tr>");
        oldpay = Util.itemArrayList.get(i).price;
        totalprice = totalprice + oldpay;
    }
    float basevalue = (float) (totalprice / 1.21);
    float ivaValue = totalprice - basevalue;
    sb.append("<tr>"
            + "<td>").append("-----------------------")
            .append("</td>"
                    + "<td align='right'>");
    sb.append("<tr>"
            + "<td>")
            .append("(Total)Base imponible")
            .append("</td>"
                    + "<td align='right'>")
            .append("</td>  "
                    + "<td align='right'>")
            .append(decimalFormat.format(basevalue) + "€")
            .append("</td>"
                    + "</tr>");

    sb.append("<tr>"
            + "<td>")
            .append("IVA (+21%)")
            .append("</td>"
                    + "<td align='right'>")
            .append("</td>  "
                    + "<td align='right'>")
            .append(decimalFormat.format(ivaValue) + "€")
            .append("</td>"
                    + "</tr>");

    sb.append("<tr>" +
            "<td>").append("==============================================").append("</td>" +
            "<td>==========</td>" +
            "<td>===========</td>");

    sb.append("<tr>"
            + "<td>").
            append("<b>Total </b>")
            .append("</td>"
                    + "<td align='right'>")
            .append("</td>  "
                    + "<td align='right'>")
            .append(decimalFormat.format(totalprice) + "€")
            .append("</td>"
                    + "</tr>");

    sb.append("</table>");
    System.out.print(sb.toString());


    return sb.toString();
}

它不起作用,我有一个非常类似的Spring boot应用程序,它使用Option 2读取文件名,你知道这里缺少什么吗?

我通过以下方式使用slf4j:

 logging.file=C\\outputFolder\\fileName3.log

1 个答案:

答案 0 :(得分:1)

您正在通过Spring Boot完成日志记录配置后设置属性值,调用System.setProperty("logging.file", "C:\\outputFolder\\fileName2.log")不会生效。

您可以使用System.getProperty("logging.file")

从第二个应用程序中读取文件位置