英文:
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<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);
}
}
The console is displaying the attached image:
答案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:
<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>
答案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
<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>
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.
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论