2

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 ???

Haya
  • 21
  • 4

1 Answers1

0

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.

A_C
  • 905
  • 6
  • 18
  • I edit it to show where I am logging I know that there is something missing but I just cant know exactly what – Haya Oct 18 '18 at 07:20
  • Which Operating system are you using? Or are you running this code via Eclipse or some IDE? – A_C Oct 18 '18 at 08:06
  • Please try this:- log4j.appender.file.File=C:\\Users\\dev\\Desktop\\log.txt (Here double back slashes are used instead of forward slash) – A_C Oct 18 '18 at 08:10
  • im using netbeans – Haya Oct 18 '18 at 08:12
  • I tried using log4j.appender.file.File=C:\\Users\\dev\\Desktop\\log.txt yet it didn't work – Haya Oct 18 '18 at 08:16
  • Please put logger code outside of all conditions and just throw and log a new error message just to simplify and test – A_C Oct 18 '18 at 08:18
  • I tried to debug it and it executes it but I don't get any output on the log file – Haya Oct 18 '18 at 08:27
  • The location and name of the log4j.properties file also matters. It should ideally be inside the root of your source / resource folder and name should be in small case letters. – A_C Oct 18 '18 at 08:55
  • made sure that it is right but my log is still empty – Haya Oct 18 '18 at 10:19
  • Are you sure that the log4j jar is available in the classpath while running the application? – A_C Oct 18 '18 at 11:51
  • yes it is there , in netbeans the log4j.propertie should be in the resources folder right? – Haya Oct 18 '18 at 12:30
  • See this thread:- https://stackoverflow.com/questions/1485987/where-should-i-put-the-log4j-properties-file – A_C Oct 18 '18 at 12:55