SpringBoot整合LogBack,分级别输出到不同的文件。并定制自定义后门查看实时日志。

不逼逼直接上代码。

SpringBoot默认集成的日志框架就是LogBack,无需做任何操作。

第一步:application.properties 配置LogBack

#logback日志配置
logging.config=classpath:logback.xml

SpringBoot整合LogBack,分级别输出到不同的文件。并定制自定义后门查看实时日志。_第1张图片

classpath会自动扫描资源包下的全部匹配到的文件。

设置编码方式,防止日志中文乱码。

spring.http.encoding.charset = UTF-8
spring.http.encoding.enabled = true
spring.http.encoding.force = true

第二步:编写logback.xml文件





    
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    

    
    
        
            ERROR
            DENY
            ACCEPT
        
        
            %msg%n
            
         
         
            log/logback.info.%d{yyyy-MM-dd}.log
        
    

    
        
            ERROR
        
        
            %msg%n
            
         
         
            log/logback.error.%d{yyyy-MM-dd}.log
        
    

    
        
        
        
    


第三步:在自己的项目里埋点输出吧,只要自己设置日志级别就行了

 

照着动手做,然后项目运行,日志会自动生成到你项目中,自己可以刷新项目看到新生成的log文件。

SpringBoot整合LogBack,分级别输出到不同的文件。并定制自定义后门查看实时日志。_第2张图片

第四步:造后门接口通过浏览器访问日志

提供2个接口,一个看info,一个看error。直接贴代码。

@RequestMapping(value = "getInfoLog", method = RequestMethod.GET)
public void getInfoLog(HttpServletResponse response) {
    BufferedReader in = null;
    PrintWriter out = null;

    try {
        //当前项目下路径
        File file = new File("");
        String pgPath = file.getAbsolutePath() + File.separator + "log";

        String fileName = "logback.info." + getToday() + ".log";
        File logFile = new File(pgPath + File.separator + fileName);

        in = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "utf-8"));
        response.setContentType("text/plain; charset=utf-8");
        out = response.getWriter();

        String line = "";
        while ((line = in.readLine()) != null) {
            out.write(line + "\r\n");
        }

        out.flush();
        out.close();
        in.close();
    } catch (Exception e) {
        logger.error("getInfoLog" + e.getMessage());
    } finally {
        out.close();
        try {
            in.close();
        } catch (IOException e) {
            logger.error("getInfoLog" + e.getMessage());
        }
    }
}
@RequestMapping(value = "getErrorLog", method = RequestMethod.GET)
public void getErrorLog(HttpServletResponse response) {
    BufferedReader in = null;
    PrintWriter out = null;

    try {
        //当前项目下路径
        File file = new File("");
        String pgPath = file.getAbsolutePath() + File.separator + "log";

        String fileName = "logback.error." + getToday() + ".log";
        File logFile = new File(pgPath + File.separator + fileName);

        in = new BufferedReader(new InputStreamReader(new FileInputStream(logFile), "utf-8"));
        response.setContentType("text/plain; charset=utf-8");
        out = response.getWriter();

        String line = "";
        while ((line = in.readLine()) != null) {
            out.write(line + "\r\n");
        }

        out.flush();
        out.close();
        in.close();
    } catch (Exception e) {
        logger.error("getErrorLog" + e.getMessage());
    } finally {
        out.close();
        try {
            in.close();
        } catch (IOException e) {
            logger.error("getErrorLog" + e.getMessage());
        }
    }
}

相关代码:

private String FormetFileSize(long fileS) {
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    if (fileS < 1024) {
        fileSizeString = df.format((double) fileS) + "B";
    } else if (fileS < 1048576) {
        fileSizeString = df.format((double) fileS / 1024) + "KB";
    } else if (fileS < 1073741824) {
        fileSizeString = df.format((double) fileS / 1048576) + "MB";
    } else {
        fileSizeString = df.format((double) fileS / 1073741824) + "GB";
    }
    return fileSizeString;
}

private String getTodayTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String str = sdf.format(new Date());
    return str;
}

private String getToday() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String str = sdf.format(new Date());
    return str;
}

就到这了,各位看官自己手动改造吧。

你可能感兴趣的:(Spring相关)