日志的使用-log4j
1,首先添加对log4j-core-2.6.2.jar,log4j-api-2.6.2.jar的引用.
https://files.cnblogs.com/files/renjing/log4j.zip
2,新增一个xml配置文件并防止在项目根目录下(web项目放在/src目录下),配置内容如下.
<?xml version="1.0" encoding="UTF-8"?> <configuration status="OFF"> <appenders> <Console name="CONSOLE" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> <RollingFile name="ROLLING" fileName="d:/logs/log.log" filePattern="d:/logs/log_%d{yyyy-MM-dd}_%i.log"> <PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/> <Policies> <TimeBasedTriggeringPolicy modulate="true" interval="24"/> <SizeBasedTriggeringPolicy size="1024 KB"/> </Policies> <DefaultRolloverStrategy max="20"/> </RollingFile> </appenders> <loggers> <root level="debug"> <appender-ref ref="CONSOLE" /> <appender-ref ref="ROLLING"/> </root> </loggers> </configuration>
3,测试代码
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Main1 { // 规则:1、一个类一个Logger对象。声明为static final // getLogger()这里一般用当前类的Class private static final Logger logger = LogManager.getLogger(Main1.class); public static void main(String[] args) { for(int i=0;i<10000000;i++) { logger.error("输入错误,转换失败"+i); } } }