简体   繁体   中英

logging jsf exceptions with log4j

I m trying to log exceptions in my non maven project using log4j my log4j.properties

log4j.rootLogger = debug, stdout, FILE

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

log4j.appender.FILE = org.apache.log4j.RollingFileAppender
log4j.appender.FILE.maxFileSize = 100kb
log4j.appender.FILE.maxBackupIndex = 2
log4j.appender.file.File=C:/Users/dev/Desktop/log.txt
log4j.appender.FILE.Threshold = debug
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n

and used the exception handler and factory mentioned in balusC answer

public class ErpExceptionHandler extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;
private static final long serialVersionUID = 1L;    

    private static Logger logger = Logger.getLogger(ErpExceptionHandler.class);
    public ErpExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public void handle() throws FacesException {
        FacesContext facesContext = FacesContext.getCurrentInstance();

        for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
            Throwable exception = iter.next().getContext().getException(); 

       logger.error("An exception occurred!", exception);
        }

        getWrapped().handle();
    }

    @Override
    public ExceptionHandler getWrapped() {
        return wrapped;
    }

}

and my log file is still empty what am I messing in here ???

It seems that you have enabled the log4j configuration to log at the DEBUG level. But, the actual code which is logging is not included. Probably, DEBUG level (or an appropriate level) is not used while logging in the code?


UPDATE: Please try this:- log4j.appender.file.File=C:\\\\Users\\\\dev\\\\Desktop\\\\log.txt (Here double back slashes are used instead of forward slash)

Also, please try throwing a new Exception outside of all the conditionals/if condition/etc to simplify and check if the logging works. You would have to catch and log this thrown exception then.

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