Java – Slf4j在控制台上不显示:

huangapple go评论73阅读模式
英文:

Java - Slf4j not showing on the console :

问题

我正在尝试在控制台上显示这个日志信息 log.info("String is:" + str1); 但是根本没有显示如何在控制台上显示 log.info 信息

@Slf4j
@RequiredArgsConstructor
@Component
public class TTD {

public ResponseEntity<InputStreamResource> transform() {


    try {
        ObjectMapper mapper = new ObjectMapper();
        Jsonb jsonb = JsonbBuilder.create();

        EcrionIntegration ei = new EcrionIntegration();
        ei.setHelloWord("Hello Sharon");

        String str1 = mapper.writeValueAsString(ei);

        log.info("String is:" + str1);

    } catch (Exception ex) {

        throw new RuntimeException(ex);
    }
}
}

控制台显示的是附带的图片

[附件中的控制台][1]


  [1]: https://i.stack.imgur.com/mwVFy.png
英文:

I am trying to display this log.info("String is:"+str1); on the console but it doesn't show at all, how do I display log.info on the console?

@Slf4j
@RequiredArgsConstructor
@Component
public class TTD {

public ResponseEntity&lt;InputStreamResource&gt; transform() {


    try {
        ObjectMapper mapper = new ObjectMapper();
        Jsonb jsonb = JsonbBuilder.create();

        EcrionIntegration ei = new EcrionIntegration();
        ei.setHelloWord(&quot;Hello Sharon&quot;);


        String str1 = mapper.writeValueAsString(ei);

        log.info(&quot;String is:&quot;+str1);

    } catch (Exception ex) {

        throw new RuntimeException(ex);
    }
}

The console is displaying the attached image:

The console without the logs

答案1

得分: 1

以下是翻译好的内容:

不需要拥有 log4j.xml 或 logback.xml 文件进行日志配置。

如果你在使用 Lombok 导入 Slf4j,请确保在你的 pom.xml 文件中添加以下依赖项:

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.24</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.26</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.21</version>
</dependency>
英文:

It is not required to have a log4j.xml or logback.xml files for logging configuration.

If you are importing Slf4j with Lombok, make sure you have the following dependencies in your pom.xml file:

&lt;dependency&gt;
  &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
  &lt;artifactId&gt;lombok&lt;/artifactId&gt;
  &lt;version&gt;1.18.24&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
  &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
  &lt;version&gt;1.7.26&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
  &lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
  &lt;version&gt;1.7.21&lt;/version&gt;
&lt;/dependency&gt;

答案2

得分: -1

你需要在资源文件夹中创建一个配置文件,其中包含标准输出(STDOUT)的追加器。

例如:

log4j.xml
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
 
  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

或者在 application.properties 文件中,例如:

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
英文:

You need a configuration file in your resources folder with STDOUT appender.

For example:

log4j.xml
&lt;configuration&gt;
  &lt;appender name=&quot;STDOUT&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
    &lt;encoder&gt;
      &lt;pattern&gt;%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n&lt;/pattern&gt;
    &lt;/encoder&gt;
  &lt;/appender&gt;
 
  &lt;root level=&quot;debug&quot;&gt;
    &lt;appender-ref ref=&quot;STDOUT&quot; /&gt;
  &lt;/root&gt;
&lt;/configuration&gt;

Or in application.properties file, eg:

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n

答案3

得分: -1

你需要在 XML 文件的 <configuration>...</configuration> 内部添加以下行:
ref="CONSOLE" 将帮助在控制台上打印。

<root>
    <level value="INFO" />
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="FILE" />
</root>
英文:

You need to add the below line in your XML file inside <configuration>...</configuration>
ref="CONSOLE" will help to print on console.

    	&lt;root&gt;
    		&lt;level value=&quot;INFO&quot; /&gt;
    		&lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
			&lt;appender-ref ref=&quot;FILE&quot; /&gt;
    	&lt;/root&gt;

huangapple
  • 本文由 发表于 2020年8月19日 22:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63489477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定