我是SpringBoot的新手,我正在尝试配置Log4J,但我正面临有线行为,请帮忙。 这是我的项目结构: enter image description here
以下是我的档案代码: - Log4J2PropertiesConf:
package Log4J;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Log4J2PropertiesConf {
private static Logger logger = LogManager.getLogger();
public static void main(String[] args) {
logger.info("Started");
TestClass.test("Before");
SpringApplication.run(Log4J2PropertiesConf.class, args);
TestClass.test("After");
}
}
识别TestClass:
package Log4J;
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
public class TestClass {
private static Logger logger = LogManager.getLogger(TestClass.class);
public static void test(String s) {
logger.info("IN Class Test: " + s);
}
}
的TestController:
package Log4J.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Testcontroller {
private static Logger logger = LogManager.getLogger(Testcontroller.class);
@RequestMapping(method = RequestMethod.GET, value = "/Get")
public ResponseEntity<String> GetValue() {
logger.debug("This is a debug message: Test1");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
return new ResponseEntity<String>("Reached", HttpStatus.OK);
}
}
Log4J2PropertiesConfTest:
package Log4J;
import org.junit.Test;
public class Log4J2PropertiesConfTest {
@Test
public void testPerformSomeTask() throws Exception {
TestClass.test("from Test");
}
}
log4j2.properties:
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/propertieslogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=Log4J
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
logger.file.additivity = false
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
这就是我正在尝试的,我正在使用maven,我在pom文件中添加了所有依赖项。 现在,当我作为Springboot应用程序运行时,我在控制台上面了:
[main] INFO org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer - Tomcat started on port(s): 10053 (http)
[main] INFO Log4J.Log4J2PropertiesConf - Started Log4J2PropertiesConf in 2.982 seconds (JVM running for 3.868)
2017-01-04 21:59:13.832 INFO 7544 --- [ main] L.TestClass : **IN Class Test: After**
我同时在日志文件中得到以下内容:
[INFO ] 2017-01-04 21:59:10.740 [main] Log4J2PropertiesConf - **Started**
[INFO ] 2017-01-04 21:59:10.742 [main] TestClass - **IN Class Test: Before**
现在,如果我调用GetValue(http:localhost:10053 / Get)方法,我将在控制台上显示以下内容:
2017-01-04 22:02:18.531 INFO 7544 --- [io-10053-exec-1] L.c.Testcontroller : **This is an info message**
2017-01-04 22:02:18.532 WARN 7544 --- [io-10053-exec-1] L.c.Testcontroller : **This is a warn message**
2017-01-04 22:02:18.532 ERROR 7544 --- [io-10053-exec-1] L.c.Testcontroller : **This is an error message**
2017-01-04 22:02:18.532 FATAL 7544 --- [io-10053-exec-1] L.c.Testcontroller : **This is a fatal message**
在日志中没有任何内容更新。
当我运行单元测试时,我在日志文件中得到以下内容:
[INFO ] 2017-01-04 22:03:36.650 [main] TestClass - **IN Class Test: from Test**
所以,我希望每个日志条目都进入日志文件,而不是控制台。虽然我的属性文件中也有logger.file.additivity = false
,但不确定如何将一些信息提供给控制台和一些文件。
我希望将所有内容都放入日志文件中。请帮忙。