现在比较吊的就是这个log4j2这个日志框架了,功能强悍.slf4j是个日志框架的统一接口,方便扩展,切换框架啥的.
配置SSM+log4J2+SL4J
https://blog.csdn.net/chy2z/article/details/80080580
一般常用的使用方式
public class JavaTest {
private final static Logger logger = LoggerFactory.getLogger(JavaTest.class);
public static void main(String[] args) {
logger.error("a{}b{}c",1,"2");
}
}
有关日志配置文件三篇比较强悍的博客
https://blog.csdn.net/yangshangwei/article/details/75146896
https://www.cnblogs.com/hafiz/p/6170702.html
https://www.cnblogs.com/SummerinShire/p/6498977.html
正八经儿吊的还是去这看 https://logging.apache.org/log4j/2.x/
项目中一个大神老哥写的
<?xml version="1.0" encoding="UTF-8"?> <Configuration monitorInterval="60"> <Appenders> <Console name="stdout" target="SYSTEM_OUT"> <PatternLayout pattern="%date{DEFAULT} |%-20.20thread |%-30.30logger{1}|%level{length=1}| %message%n" /> </Console> <RollingFile name="ofbiz" fileName="runtime/logs/ofbiz.log" filePattern="runtime/logs/ofbiz-%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="%date{DEFAULT} |%-20.20thread |%-30.30logger{1}|%level{length=1}| %message%n" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="1 MB" /> </Policies> <DefaultRolloverStrategy max="10" /> </RollingFile> <RollingFile name="error" fileName="runtime/logs/error.log" filePattern="runtime/logs/error-%d{yyyy-MM-dd}-%i.log"> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="%date{DEFAULT} |%-20.20thread |%-30.30logger{1}|%level{length=1}| %message%n" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="1 MB" /> </Policies> <DefaultRolloverStrategy max="3" /> </RollingFile> <RollingFile name="sql" fileName="runtime/logs/sql.log" filePattern="runtime/logs/sql-%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="%date{DEFAULT} |%-20.20thread |%-30.30logger{1}|%level{length=1}| %message%n" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="1 MB" /> </Policies> <DefaultRolloverStrategy max="3" /> </RollingFile> <Async name="async"> <AppenderRef ref="ofbiz" /> <AppenderRef ref="stdout" /> <AppenderRef ref="error" /> </Async> <Async name="sqlAsync"> <AppenderRef ref="sql" /> </Async> </Appenders> <Loggers> <logger name="org.ofbiz.base.converter.Converters" level="warn" /> <logger name="org.apache" level="warn" /> <logger name="freemarker" level="warn" /> <!-- sql 以及耗费的执行时间 --> <logger name="jdbc.sqltiming" level="WARN" additivity="false"> <appender-ref ref="sqlAsync" /> </logger> <!-- 只有sql,没有执行时间 --> <logger name="jdbc.sqlonly" level="OFF" additivity="false"> <appender-ref ref="sqlAsync" /> </logger> <!-- 执行之后的返回结果 --> <logger name="jdbc.resultsettable" level="OFF" additivity="false"> <appender-ref ref="sqlAsync" /> </logger> <logger name="jdbc.resultset" level="OFF" additivity="false"> <appender-ref ref="sqlAsync" /> </logger> <!-- 能看到数据库的连接数 --> <logger name="jdbc.connection" level="OFF" additivity="false"> <appender-ref ref="sqlAsync" /> </logger> <logger name="jdbc.audit" level="OFF" additivity="false"> <appender-ref ref="sqlAsync" /> </logger> <Root level="all"> <AppenderRef ref="async" /> </Root> </Loggers> </Configuration>
简单来说结构就是这样
日志配置
日志定义
输出位置1
输出位置2
输出位置3
输出位置4
异步输出1
输出位置1
输出位置2
输出位置3
异步输出2
输出位置4
日志配置
配置包中的日志输出级别1(默认为root配置)
配置包中的日志输出级别2(默认为root配置)
配置包中的日志输出级别3(使用异步输出2中的配置)
配置包中的日志输出级别3(使用异步输出2中的配置)
root配置(使用异步输出1的配置)