首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SpringBoot -糟糕的日志记录实践:使用系统输出流

SpringBoot -糟糕的日志记录实践:使用系统输出流
EN

Stack Overflow用户
提问于 2021-10-06 01:05:04
回答 2查看 79关注 0票数 1

当运行java程序而不得到加强错误Poor Logging Practice: Use of a System Output Stream时,将日志输出打印到控制台的最简单方法是什么?

我想打印出几行,这样程序完成后它就会显示出来。System.out.println不被接受为最佳实践。我还可以使用什么其他方法?

代码语言:javascript
运行
复制
public static void IBanUpdateTest(){   
    driver.get("http://10.10.10.10/demo-web-1.0-SNAPSHOT"); //define the url
    String pageTitle = driver.getTitle();       //get the title of the webpage
    System.out.println("The title of this page is ===> " +pageTitle);
    Assert.assertEquals("IBan - Business ID code", pageTitle);    //verify the title of the webpage
    driver.findElement(By.id("0iban")).clear();//clear the input field before entering any value
    driver.findElement(By.id("0iban")).sendKeys("5464564654");//enter the IBan Value
    driver.findElement(By.id("0businessIdentifierCode")).clear();
    driver.findElement(By.id("0businessIdentifierCode")).sendKeys("54645646548546465"); //enter the BIC value 
    driver.findElement(By.id("0updateRow")).click();      //click Update button
    System.out.println("Successfully updated the row");
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-06 06:41:18

我相信这是最好的解决方案:

代码语言:javascript
运行
复制
Logger logger = LoggerFactory.getLogger(YourClass.class);
票数 1
EN

Stack Overflow用户

发布于 2021-10-06 01:46:12

登录System.out通常被认为是一种糟糕的做法,Fortify正确地标记了这一点。我不建议简单地忽略它/关闭警告。

正如其他人在评论中提到的那样,Spring Boot默认配置了Logback日志库。您可以在代码中配置记录器,但过一段时间后就会变得混乱,所以很多人发现添加Lombok库并使用它的@Log注解在编译时生成记录器是很方便的。然后你的代码就会变成如下所示:

代码语言:javascript
运行
复制
@Log
public class MyClassThatLogs {
  public void myMethod() {
    log.info("myMethod was called");
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69458763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档