日志工具 - log4j
步骤
:
1>新建文件 -> 注意:配置文件不是xml,而是properties(命名log4j.properties)。
2>依赖项
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
解析properties键值对
//定义一个日志方案对象{appenderName} 确定日志的显示级别[level]
//DEBUG,INFO,WARN,ERROR注意:级别向上兼容
log4j.rootLogger = [level],appenderName,appenderName2,...
//执行方式 参数设置
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.optionN = valueN
//日志内容 格式设置
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.optionN = valueN
3>API
Logger logger = Logger.getLogger(BookAdvice.class);
logger.error(strb.toString()); //注意:向上兼容
代码实现
配置log4j.properties
log4j.rootLogger = [ERROR],mylog4j,mylogfile
log4j.appender.mylog4j = org.apache.log4j.ConsoleAppender
log4j.appender.mylog4j.Threshold = ERROR
log4j.appender.mylog4j.ImmediateFlush = true
log4j.appender.mylog4j.Target = System.err
log4j.appender.mylog4j.layout = org.apache.log4j.PatternLayout
log4j.appender.mylog4j.layout.ConversionPattern = [%-10p]-%m:[%30d]
log4j.appender.mylogfile = org.apache.log4j.FileAppender
log4j.appender.mylogfile.Threshold = ERROR
log4j.appender.mylogfile.ImmediateFlush = true
log4j.appender.mylogfile.File = F:/tmp/log/book.log
log4j.appender.mylogfile.Append = true
log4j.appender.mylogfile.layout = org.apache.log4j.PatternLayout
log4j.appender.mylogfile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
面向切面代理类BookAdvice.java:通过代理完成日志信息填写,代理service层的所有类的所有方法
//<aop:aspect ref="bookAdvice">
@Aspect //--->代理类
@Service("bookAdvice")
public class BookAdvice {
private Logger logger;
//<aop:pointcut expression="execution(* com.uplooking.service..*.*(..))" id="p1"/>
@Pointcut("execution(* com.uplooking.service..*.*(..))")
public void p1(){}
//<aop:around method="invoke" pointcut-ref="p1"/>
@Around("p1()")
public Object invoke(ProceedingJoinPoint pjp) throws Throwable{
StringBuilder strb = new StringBuilder();
strb.append("****************业 务 日 志******************\r\n");
strb.append("业务对象:"+pjp.getTarget()+"\r\n");
strb.append("业务名称:"+pjp.getSignature().getName()+"\r\n");
strb.append("业务参数:"+Arrays.toString(pjp.getArgs())+"\r\n");
strb.append("执行时间:"+DateTimeUtils.getLocalDateTime()+"\r\n");
strb.append("*****************************************\r\n\r\n");
logger = Logger.getLogger(BookAdvice.class);
logger.error(strb.toString());
//是否回调原业务
Object result = pjp.proceed();
return result;
}
/*
@Before("p1()")
public void hello(){
System.out.println("欢迎使用网上书籍管理APP");
}
@After("p1()")
public void bye(){
System.out.println("谢谢使用网上书籍管理APP");
}
@AfterReturning(pointcut="p1()",returning="result")
public void result(Object result){
System.out.println("###################################");
System.out.println("业务结果:"+result);
System.out.println("###################################");
}
@AfterThrowing(pointcut="p1()",throwing="error")
public void error(Throwable error){
System.out.println("###################################");
System.out.println("发生异常:"+error);
System.out.println("###################################");
}
*/
}
测试运行类:MyTest.java
public class MyTest {
public void test1() {
Logger logger = Logger.getLogger(MyTest.class);
logger.error("Hello World");
}
@Test
public void test() {
String configLocation = "applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
BookService bookService = (BookService) ctx.getBean("bookService");
System.out.println(bookService.removeBook(1));
}
}
运行结果
[ERROR ]-****************业 务 日 志******************
业务对象:com.uplooking.service.impl.BookServiceImpl@3a52dba3
业务名称:removeBook
业务参数:[1]
执行时间:2021-05-09 16:09:19
*****************************************
:[ 2021-05-09 16:09:19,810]业务>>>新书入库
书籍编号:1
持久化 DELETE ...
{code=200, messgae=书籍移除完成}
日志文件变化