当前位置:网站首页>log4j. Properties log details
log4j. Properties log details
2022-07-27 18:57:00 【It's too late】
1) Import log4j rely on
Here are my imported dependencies :
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
2) add to log4j.properties
# ROOTER
log4j.rootLogger=DEBUG,CONSOLE,FILE
# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-d{
yyyy-MM-dd HH\:mm\:ss} %-5p %-20c %x %m%n
# FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=logs.log
log4j.appender.FILE.MaxBackupIndex=20
log4j.appender.FILE.MaxFileSize=10MB
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-d{
yyyy-MM-dd HH\:mm\:ss} %-5p %-20c %x %m%n
# ERROR
log4j.appender.ERR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERR.File =error.log
log4j.appender.ERR.Append = true
log4j.appender.ERR.Threshold = ERROR
log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.ERR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERR.layout.ConversionPattern = %-d{
yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
Appender
Log output , Configure the output level of the log 、 Output position, etc , It includes the following categories :
ConsoleAppender: Log output to the console ;
FileAppender: output to a file ;
RollingFileAppender: output to a file , When the file reaches a certain threshold , Automatically back up log files ;
DailyRollingFileAppender: Log files can be backed up regularly , By default, one file a day , It can also be set to one per minute 、 One per hour ;
WriterAppender: You can customize the log output location .
The level of logging
General log levels include :ALL,DEBUG, INFO, WARN, ERROR,FATAL,OFF
Log4J Recommended :DEBUG, INFO, WARN, ERROR
Layout: Log output format ,Log4j Provided layout There are the following :
org.apache.log4j.HTMLLayout( With HTML Tabular layout ),
org.apache.log4j.PatternLayout( The layout mode can be specified flexibly ),
org.apache.log4j.SimpleLayout( Contains the level of log information and the information string ),
org.apache.log4j.TTCCLayout( Include the time when the log was generated 、 Threads 、 Categories and so on )
Output format
Log4J The most commonly used log output format is :org.apache.log4j.PatternLayOut, The main parameters are :
%n - Line break
%m - Log contents
%p - The level of logging (FATAL, ERROR,WARN, INFO,DEBUG or custom)
%r - The number of milliseconds since the program started
%t - Current thread name
%d - Date and time , Generally use the format %d{
yyyy-MM-dd HH:mm:ss, SSS}
%l - Where the output log event occurred , Same as %F%L%C%M
%F - java The source file name
%L - java Source lines
%C - java Class name ,%C{
1} Output the last element
%M - java Method name
Logger log = Logger.getLogger( The current class .class);
if(log.isDebugEnabled()){
log.debug("...");
}
if(log.isInfoEnabled()){
log.info("...");
}
...
3) Write a log section
/** * Log4j Log output section */
@Aspect
@Component
public class Log4jAspect {
// Create a log object
private Logger logger = Logger.getLogger(Log4jAspect.class);
// To all service Class plus log trace
@Pointcut("execution(* com.blb.aop_demo.service.*Service.*(..))")
public void logPointcut(){
}
// Configure surround notification
@Around("logPointcut()")
public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
// Record the time before the method is executed
long start = System.currentTimeMillis();
// Print method name
if(logger.isDebugEnabled()){
logger.debug(" Current execution method :" + point.getSignature().getName());
}
// Printing parameters
Object[] args = point.getArgs();
for(Object arg : args){
if(logger.isDebugEnabled()){
logger.debug(" Parameters :"+arg);
}
}
// Print return value
Object result = point.proceed();
if(logger.isDebugEnabled()){
logger.debug(" Method return value :" +result);
}
// Print execution time
long end = System.currentTimeMillis();
if(logger.isDebugEnabled()){
logger.debug(" Method execution time :" +(end - start));
}
return result;
}
}
边栏推荐
- MySQL 01 关系型数据库设计
- MySQL 04 高级查询(二)
- Ruiji takeout notes
- 地图找房的实例
- Interceptor interceptor
- 【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
- MySQL 06 transaction, view, index, backup and recovery
- Aircraft battle with enemy aircraft
- 兆骑科创海内外引进高层次人才,创新创业项目对接
- Electric heating neck pillow chip-dltap703sc
猜你喜欢

Use mobaxtermto establish a two-tier springboard connection

JDBC-MySql 01 JDBC操作MySql(增删改查)

订单的提交

MySQL 04 advanced query (II)

MySQL 03 高级查询(一)

Electric heating neck pillow chip-dltap703sc

MySQL 03 advanced query (I)

20000 words + 30 pictures | what's the use of chatting about MySQL undo log, redo log and binlog?

Log4j epic loopholes, big companies like jd.com have been recruited

"MySQL things" explains the indexing principle in detail
随机推荐
MySQL create event execution task
MySQL 06 事务、视图、索引、备份和恢复
百度地图技术概述,及基本API与WebApi的应用开发
Functions in JS and the use of DOM to obtain elements and event attributes
Ruiji takeout SQL table
我人都傻了,CompletableFuture和OpenFegin一起使用竟然报错
TypeScript安装
LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
INSUFFICIENT_ ACCESS_ ON_ CROSS_ REFERENCE_ ENTITY APEX / SALESFORCE
Log4j epic loopholes, big companies like jd.com have been recruited
瑞吉外卖sql表
How to realize the full-text content retrieval of word, PDF and txt files?
LeetCode 刷题 第三天
Interviewer: what do you think is your biggest weakness?
订单的提交
Canvas draws graphics according to coordinate points
音乐律动七彩渐变灯芯片--DLT8S04A-杰力科创
LED带风扇护眼学习台灯触摸芯片-DLT8S12A
百度地图鹰眼轨迹服务
Vue uses keep alive to realize page caching