org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Agencia].[action] [2007-02-28 09:30:12,529] ERROR - Servlet.service() for servlet action threw exception
javax.servlet.ServletException: A URL do menu é inválida
at br.com.xxxx.agencia.controle.AutorizacaoFilter.doFilter(AutorizacaoFilter.java:127)
O log4j só está registrando via JDBCAppender a mensagem
O que não ajuda em nada a identificar o erro. E até pode ser descartado.
Gostaria de saber se há como tratar melhor as mensagens que vão para o banco de dados?
Eu adotei uma solução diferente.
Criei uma classe estendendo JDBCAppender e sobreescrevi o metodo getLogStatement(LoggingEvent evt)
através do parametro evt eu consigo todas as informações para serem postas na mensagem.
Também gostaria de postar minha sugestão com log4j.
Fiz uma alteração no smtpAppender que aceita autenticação do Ceki Gulcu.
packagebr.com.drogaraia.recweb.service.util;importjava.util.Date;importorg.apache.commons.mail.Email;importorg.apache.commons.mail.EmailException;importorg.apache.commons.mail.MultiPartEmail;importorg.apache.log4j.AppenderSkeleton;importorg.apache.log4j.Level;importorg.apache.log4j.helpers.CyclicBuffer;importorg.apache.log4j.helpers.LogLog;importorg.apache.log4j.helpers.OptionConverter;importorg.apache.log4j.spi.LoggingEvent;importorg.apache.log4j.spi.TriggeringEventEvaluator;/** * Send an e-mail when a specific logging event occurs, typically on errors or * fatal errors. * * <p> * The number of logging events delivered in this e-mail depend on the value of * <b>BufferSize</b> option. The <code>SMTPAppender</code> keeps only the * last <code>BufferSize</code> logging events in its cyclic buffer. This * keeps memory requirements at a reasonable level while still delivering useful * application context. * * @author Ceki Gülcü * @since 1.0 */publicclassSMTPAppenderextendsAppenderSkeleton{privateStringto;privateStringcc;privateStringfrom;privateStringsubject;privateStringsmtpHost;privateintbufferSize=512;privatebooleanlocationInfo=false;privateStringuser;privateStringpassword;privateStringmustAuthentication;protectedCyclicBuffercb=newCyclicBuffer(bufferSize);// protected Message msg;privateMultiPartEmailemail=newMultiPartEmail();protectedTriggeringEventEvaluatorevaluator;/** * The default constructor will instantiate the appender with a * {@link TriggeringEventEvaluator} that will trigger on events with level * ERROR or higher. */publicSMTPAppender(){this(newDefaultEvaluator());}/** * Use <code>evaluator</code> passed as parameter as the {@link * TriggeringEventEvaluator} for this SMTPAppender. */publicSMTPAppender(TriggeringEventEvaluatorevaluator){this.evaluator=evaluator;}/** * Activate the specified options, such as the smtp host, the recipient, * from, etc. */publicvoidactivateOptions(){email.setCharset(Email.ISO_8859_1);if(smtpHost!=null){email.setHostName(smtpHost);}if(needAuthentication()){email.setAuthentication(user,password);}try{if(from!=null){email.setFrom(from);}else{email.setFrom("[email removido]");}email.addTo(to);if(cc!=null){email.addTo(cc);}if(subject!=null){email.setSubject(subject);}else{email.setSubject("Qual é o seu problema?");}}catch(EmailExceptione){LogLog.error("Could not activate SMTPAppender options.",e);}}privatebooleanneedAuthentication(){if(mustAuthentication!=null){StringupperCase=mustAuthentication.toUpperCase();returnupperCase.equals("TRUE");}returnfalse;}/** * Perform SMTPAppender specific appending actions, mainly adding the event * to a cyclic buffer and checking if the event triggers an e-mail to be * sent. */publicvoidappend(LoggingEventevent){if(!checkEntryConditions()){return;}event.getThreadName();event.getNDC();if(locationInfo){event.getLocationInformation();}cb.add(event);if(evaluator.isTriggeringEvent(event)){sendBuffer();}}/** * This method determines if there is a sense in attempting to append. * * <p> * It checks whether there is a set output target and also if there is a set * layout. If these checks fail, then the boolean value <code>false</code> * is returned. */protectedbooleancheckEntryConditions(){/* if (this.msg == null) { errorHandler.error("Message object not configured."); return false; }*/if(this.evaluator==null){errorHandler.error("No TriggeringEventEvaluator is set for appender ["+name+"].");returnfalse;}if(this.layout==null){errorHandler.error("No layout set for appender named ["+name+"].");returnfalse;}returntrue;}synchronizedpublicvoidclose(){this.closed=true;}/** * Returns value of the <b>To</b> option. */publicStringgetTo(){returnto;}/** * The <code>SMTPAppender</code> requires a {@link org.apache.log4j.Layout * layout}. */publicbooleanrequiresLayout(){returntrue;}/** * Send the contents of the cyclic buffer as an e-mail message. */protectedvoidsendBuffer(){try{StringBuffersbuf=newStringBuffer();Stringt=layout.getHeader();if(t!=null)sbuf.append(t);intlen=cb.length();for(inti=0;i<len;i++){// sbuf.append(MimeUtility.encodeText(layout.format(cb.get())));LoggingEventevent=cb.get();sbuf.append(layout.format(event));if(layout.ignoresThrowable()){String[]s=event.getThrowableStrRep();if(s!=null){for(intj=0;j<s.length;j++){sbuf.append(s[j]);}}}}t=layout.getFooter();if(t!=null)sbuf.append(t);email.setSentDate(newDate());email.setMsg(sbuf.toString());email.buildMimeMessage();//msg.setSentDate(new Date());email.send();}catch(Exceptione){LogLog.error("Error occured while sending e-mail notification.",e);}}/** * Returns value of the <b>EvaluatorClass</b> option. */publicStringgetEvaluatorClass(){returnevaluator==null?null:evaluator.getClass().getName();}/** * Returns value of the <b>From</b> option. */publicStringgetFrom(){returnfrom;}/** * Returns value of the <b>Subject</b> option. */publicStringgetSubject(){returnsubject;}/** * The <b>From</b> option takes a string value which should be a e-mail * address of the sender. */publicvoidsetFrom(Stringfrom){this.from=from;}/** * The <b>Subject</b> option takes a string value which should be a the * subject of the e-mail message. */publicvoidsetSubject(Stringsubject){this.subject=subject;}/** * The <b>BufferSize</b> option takes a positive integer representing the * maximum number of logging events to collect in a cyclic buffer. When the * <code>BufferSize</code> is reached, oldest events are deleted as new * events are added to the buffer. By default the size of the cyclic buffer * is 512 events. */publicvoidsetBufferSize(intbufferSize){this.bufferSize=bufferSize;cb.resize(bufferSize);}/** * The <b>SMTPHost</b> option takes a string value which should be a the * host name of the SMTP server that will send the e-mail message. */publicvoidsetSMTPHost(StringsmtpHost){this.smtpHost=smtpHost;}/** * Returns value of the <b>SMTPHost</b> option. */publicStringgetSMTPHost(){returnsmtpHost;}/** * The <b>To</b> option takes a string value which should be a comma * separated list of e-mail address of the recipients. */publicvoidsetTo(Stringto){this.to=to;}/** * Returns value of the <b>BufferSize</b> option. */publicintgetBufferSize(){returnbufferSize;}/** * The <b>EvaluatorClass</b> option takes a string value representing the * name of the class implementing the {@link TriggeringEventEvaluator} * interface. A corresponding object will be instantiated and assigned as * the triggering event evaluator for the SMTPAppender. */publicvoidsetEvaluatorClass(Stringvalue){evaluator=(TriggeringEventEvaluator)OptionConverter.instantiateByClassName(value,TriggeringEventEvaluator.class,evaluator);}/** * The <b>LocationInfo</b> option takes a boolean value. By default, it is * set to false which means there will be no effort to extract the location * information related to the event. As a result, the layout that formats * the events as they are sent out in an e-mail is likely to place the wrong * location information (if present in the format). * * <p> * Location information extraction is comparatively very slow and should be * avoided unless performance is not a concern. */publicvoidsetLocationInfo(booleanlocationInfo){this.locationInfo=locationInfo;}/** * Returns value of the <b>LocationInfo</b> option. */publicbooleangetLocationInfo(){returnlocationInfo;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicStringgetUser(){returnuser;}publicvoidsetUser(Stringuser){this.user=user;}publicStringgetMustAuthentication(){returnmustAuthentication;}publicvoidsetMustAuthentication(StringmustAuthentication){this.mustAuthentication=mustAuthentication;}publicStringgetCc(){returncc;}publicvoidsetCc(Stringcc){this.cc=cc;}}classDefaultEvaluatorimplementsTriggeringEventEvaluator{/** * Is this <code>event</code> the e-mail triggering event? * * <p> * This method returns <code>true</code>, if the event level has ERROR * level or higher. Otherwise it returns <code>false</code>. */publicbooleanisTriggeringEvent(LoggingEventevent){returnevent.getLevel().isGreaterOrEqual(Level.ERROR);}}