我正在尝试使用面向方面编程和log4j配置来实现日志记录。我的要求是独特的,所以我坚持使用相同的技术。请在下面的代码中告诉我,为什么日志记录不起作用,即使我可以看到System.out.println
给我适当的结果。
注意:log4j属性文件没有问题,如果独立调用它的工作正常。
package com.test.media.core.logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Component
@Aspect
public class LoggingAspect {
//private final Log log = LogFactory.getLog(this.getClass());
private static final Log log = LogFactory.getLog(LoggingAspect.class);
@Before("execution(* com.test.media.core.*.*.*(..))")
public void logBefore(JoinPoint joinPoint) {
Log log = LogFactory.getLog(joinPoint.getClass());
System.out.println("logBefore is running " +joinPoint.getClass());
log.debug("medthod " + joinPoint.getSignature().getName()+"() is hijacked for execution");
log.debug("******");
}
@After("execution(* com.test.media.core.*.*.*(..))")
public void logAfter(JoinPoint joinPoint) {
log.debug("medthod " + joinPoint.getSignature().getName()+"() log After execution");
log.debug("******");
}
@AfterReturning(pointcut = "execution(* com.test.media.core.*.*.*(..))", returning = "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
log.debug("medthod " + joinPoint.getSignature().getName()+"() log After Returning");
log.debug("Method returned value is : " + result);
log.debug("******");
}
@AfterThrowing(pointcut = "execution(* com.test.media.core.*.*.*(..))", throwing = "error")
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
log.debug("medthod " + joinPoint.getSignature().getName()+"() is thowing some exception");
log.debug("Exception : " + error);
log.debug("******");
}
@Around("execution(* com.test.media.core.*.*.*(..))")
public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object retVal = joinPoint.proceed();
stopWatch.stop();
StringBuffer logMessage = new StringBuffer();
logMessage.append(joinPoint.getTarget().getClass().getName());
logMessage.append(".");
logMessage.append(joinPoint.getSignature().getName());
logMessage.append("(");
// append args
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
logMessage.append(args[i]).append(",");
}
if (args.length > 0) {
logMessage.deleteCharAt(logMessage.length() - 1);
}
logMessage.append(")");
logMessage.append(" execution time: ");
logMessage.append(stopWatch.getTotalTimeMillis());
logMessage.append(" ms");
log.debug(logMessage.toString());
return retVal;
}
}
如果有人对log4j属性文件有疑问,我也会添加它。
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\logs\\tool\\tool.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=INFO
# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL
答案 0 :(得分:0)
感谢所有建议。我得到了解决方案,基本上它不是java代码问题。由于垃圾桶条目,这种未知行为正在发生。工作pom条目如下:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0</version>
</dependency>