简体   繁体   中英

Adding simple log4j on my app

I'm trying to add log4j to my app but it's not printing the warning message in the file or console.

I need to be able to print info, error, warn in my atp.log and in the console output (intellij)

My config

    # Root logger option
log4j.rootLogger= stdout, file

# Avoid spam from hibernate
log4j.logger.org.hibernate=info

# Redirect log messages to console
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{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

#outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/atp.log
log4j.appender.file.MaxFileSize=200MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Code

It enters this logger but simply doesn't print anything

@Component("accessDeniedHandler")

public class MyCustomAccessDeniedHandler implements AccessDeniedHandler {

private static final Logger logger = LoggerFactory.getLogger(MyCustomAccessDeniedHandler.class);

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exc) throws IOException {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        logger.info("User: " + auth.getName()
                + " attempted to access the protected URL: "
                + request.getRequestURI());
    }

    response.sendRedirect(request.getContextPath() + "/error403");
}

}

You should read it

Also your config file must be called log4j.properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM