11 Replies Latest reply on Jul 18, 2006 3:35 PM by alrubinger

    log4j email appender not working with jboss

    krustyfur

      I have configured log4j to write app logs using the RepositorySelector found in the Wiki here, and it is working just fine. I have tried to add an email appender to the configuration to email me the error & fatal messages and they are not getting sent. The log file contains the errors, but no email is ever generated (I checked the mail server logs, and nothing is ever being received).

      My configuration is as follows:

      <?xml version="1.0" encoding="UTF-8"?>
      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
       <appender name="dailyLogFile" class="org.apache.log4j.DailyRollingFileAppender">
       <param name="File" value="${log4j.log.dir}/myapp.log"/>
       <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c{1}] - %m%n"/>
       </layout>
       </appender>
      
       <appender name="email" class="org.apache.log4j.net.SMTPAppender">
       <param name="From" value="someemailaddress" />
       <param name="To" value="myemailaddress" />
       <param name="SMTPHost" value="mymailhost" />
       <param name="Subject" value="webapp error" />
       <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="[%d]%n%n%-5p%n%n%c%n%n%m%n%n" />
       </layout>
       <filter class="org.apache.log4j.varia.LevelRangeFilter">
       <param name="LevelMin" value="ERROR"/>
       <param name="LevelMax" value="FATAL"/>
       </filter>
       </appender>
      
       <category name="mycategoryname">
       <priority value="DEBUG"/>
       <appender-ref ref="dailyLogFile"/>
       <appender-ref ref="email" />
       </category>
      
       <category name="anothercategoryname">
       <priority value="DEBUG" />
       <appender-ref ref="dailyLogFile" />
       </category>
      
       <root>
       <priority value="WARN"/>
       <appender-ref ref="STDOUT"/>
       </root>
      </log4j:configuration>
      


      Any suggestions?

      Thanks

      Chris

        • 1. Re: log4j email appender not working with jboss

          You haven't added the email appender to the root logger.

          at the end change



          <appender-ref ref="STDOUT"/>



          to



          <appender-ref ref="STDOUT"/>
          <appender-ref ref="email"/>


          I would also take out the priority on their and set it per project/path.

          J

          • 2. Re: log4j email appender not working with jboss
            krustyfur

             

            "jelevy01" wrote:
            You haven't added the email appender to the root logger.


            I would also take out the priority on their and set it per project/path.

            J


            Why? I am not using the root logger. I could just as easily take it out. Everything gets caught by other two categories. The log messages that go to the dailyLogFile appender are the only ones I want emailed, if they are of ERROR or FATAL level.

            Also, this is not in the jboss log4j.xml, this is all in my own log4j.xml contained in the WEB-INF of the application.



            • 3. Re: log4j email appender not working with jboss
              krustyfur

              As a followup to this, I just wrote a simple command line app to test out the SMTPAppender and it works fine, using both the log4j.jar from JBoss (4.0.3) and v1.2.13 from Apache, so it appears it is something to do with the scope of running as a web app. Why is my logfile appender getting called successfully, but not the smtp appender?

              Chris

              • 4. Re: log4j email appender not working with jboss

                ;) saw you message on the Log4j Mailing list.

                Anyways, are you sure your app is loading it's configuration from this log4j.xml?

                Try turning debug on in just this log4j.xml to confirm.

                Jeremy

                • 5. Re: log4j email appender not working with jboss
                  krustyfur

                   

                  "jelevy01" wrote:
                  ;) saw you message on the Log4j Mailing list.

                  Anyways, are you sure your app is loading it's configuration from this log4j.xml?

                  Try turning debug on in just this log4j.xml to confirm.

                  Jeremy


                  I figured I'd try both places, just in case.

                  Yes I am positive my configuration is being loaded. The daily log file appender writes the logs to a directory way outside of the jboss server logs (specified by an init parameter to a listener via web.xml and then set in the log4j.log.dir system parameter). I also changed the pattern layout slightly to trim all leading package info off the class name in the log and that was reflected.

                  Also, the debug flag is already set to true, but JBoss doesn't log the debugging info that log4j emits that I saw when I wrote a simple test app. Where does this normally go? It's not in server.log.

                  Thanks

                  Chris

                  • 6. Re: log4j email appender not working with jboss
                    krustyfur

                    Can anyone help me on this please?

                    • 7. Re: log4j email appender not working with jboss
                      alrubinger

                      I'm seeing the same issue here, and I've also noticed the following:

                      * If I do not deploy my application into JBoss, I can receive ERROR log emails. I'm triggering the error by requesting the JMX Console to show me an MBean that doesn't exist.

                      * As soon as I deploy my application (not changing the Log4J configs), I no longer receive emails.

                      Anyone else seen this? Was there a resolution by the previous poster?

                      S,
                      ALR

                      • 8. Re: log4j email appender not working with jboss
                        jaikiran

                         

                        I'm seeing the same issue here, and I've also noticed the following


                        How is your application packaged? Are using your own log4j file in your webapp or are you using the log4j.xml present in %JBOSS_HOME%/server/conf directory. Post the contents of the log4j.xml file that you are using.

                        • 9. Re: log4j email appender not working with jboss
                          alrubinger

                          Hiya. My application is packaged as a JAR which in turn contains EJB3 JARs and other library JARs. I'm Using JBoss 4.0.4-GA with the log4j.xml in ${JBOSS_HOME}/server/[instancename]/conf:

                          <?xml version="1.0" encoding="UTF-8"?>
                          <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
                          <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
                           debug="true">
                          
                           <!-- ============================== -->
                           <!-- Append messages to the console -->
                           <!-- ============================== -->
                          
                           <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
                           <!-- <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/> -->
                           <param name="Target" value="System.out" />
                           <layout class="org.apache.log4j.PatternLayout">
                           <!-- The default pattern: Date Priority [Category] Message\n -->
                           <param name="ConversionPattern"
                           value="%d{ABSOLUTE} %-5p [%c{1}] %m%n" />
                           </layout>
                           </appender>
                          
                           <!-- -->
                           <!-- Declare the SMTPAppender -->
                           <!-- -->
                           <appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
                           <errorHandler
                           class="org.jboss.logging.util.OnlyOnceErrorHandler" />
                           <param name="Threshold" value="DEBUG" />
                           <param name="SMTPHost" value="myhost" />
                           <param name="To" value="mytoaddress" />
                           <param name="From" value="myfromaddress" />
                           <param name="BufferSize" value="40" />
                           <param name="Subject" value="[SYSTEM ERROR] Application Error - ${jboss.server.name}" />
                           <layout class="org.apache.log4j.HTMLLayout" />
                           </appender>
                          
                           <!-- ====================== -->
                           <!-- More Appender examples -->
                           <!-- ====================== -->
                          
                           <!-- Buffer events and log them asynchronously -->
                           <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
                           <errorHandler
                           class="org.jboss.logging.util.OnlyOnceErrorHandler" />
                           <appender-ref ref="SMTP" />
                           </appender>
                          
                           <!-- ======================= -->
                           <!-- Setup the Root category -->
                           <!-- ======================= -->
                          
                          
                          
                           <!-- ================ -->
                           <!-- Limit categories -->
                           <!-- ================ -->
                          
                           <!-- Increase MyPackage Logging Visibility -->
                           <category name="com.mypackage">
                           <priority value="ALL" />
                           </category>
                          
                           <!-- Show SQL Logs for Hibernate Schema Export -->
                           <category name="org.hibernate.tool.hbm2ddl.SchemaExport">
                           <priority value="DEBUG" />
                           </category>
                          
                           <root>
                           <priority value="INFO" />
                           <appender-ref ref="CONSOLE" />
                           <appender-ref ref="ASYNC" />
                           </root>
                          
                          </log4j:configuration>
                          


                          • 10. Re: log4j email appender not working with jboss
                            alrubinger

                            Upon setting "Log4jQuietMode" to "false" in the JMX Console for the Log4J Service, reloading the configuration and manually throwing an exception to trigger an ERROR, I receive the following stacktrace, noting the SMTPAppender attempting to contact port 465 (SMTP over SSL). Why is Log4J attemping port 465?

                            Before I deploy my application, I receive emailed errors properly.

                            13:04:46,171 ERROR [STDERR] javax.mail.MessagingException: Could not connect to SMTP host: myhost.com, port: 465;
                             nested exception is:
                             java.net.ConnectException: Connection refused: connect
                            13:04:46,171 ERROR [STDERR] at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
                            13:04:46,171 ERROR [STDERR] at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
                            13:04:46,171 ERROR [STDERR] at javax.mail.Service.connect(Service.java:275)
                            13:04:46,171 ERROR [STDERR] at javax.mail.Service.connect(Service.java:156)
                            13:04:46,171 ERROR [STDERR] at javax.mail.Service.connect(Service.java:105)
                            13:04:46,171 ERROR [STDERR] at javax.mail.Transport.send0(Transport.java:168)
                            13:04:46,171 ERROR [STDERR] at javax.mail.Transport.send(Transport.java:98)
                            13:04:46,171 ERROR [STDERR] at org.apache.log4j.net.SMTPAppender.sendBuffer(Unknown Source)
                            13:04:46,171 ERROR [STDERR] at org.apache.log4j.net.SMTPAppender.append(Unknown Source)
                            13:04:46,171 ERROR [STDERR] at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221)
                            13:04:46,171 ERROR [STDERR] at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57)
                            13:04:46,171 ERROR [STDERR] at org.apache.log4j.Dispatcher.run(AsyncAppender.java:310)
                            13:04:46,171 ERROR [STDERR] Caused by: java.net.ConnectException: Connection refused: connect
                            13:04:46,171 ERROR [STDERR] at java.net.PlainSocketImpl.socketConnect(Native Method)
                            13:04:46,171 ERROR [STDERR] at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
                            13:04:46,171 ERROR [STDERR] at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
                            13:04:46,171 ERROR [STDERR] at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
                            13:04:46,171 ERROR [STDERR] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
                            13:04:46,171 ERROR [STDERR] at java.net.Socket.connect(Socket.java:516)
                            13:04:46,171 ERROR [STDERR] at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:549)
                            13:04:46,171 ERROR [STDERR] at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
                            13:04:46,171 ERROR [STDERR] at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
                            13:04:46,171 ERROR [STDERR] at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
                            13:04:46,171 ERROR [STDERR] at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
                            13:04:46,171 ERROR [STDERR] ... 11 more
                            13:04:47,265 ERROR [STDERR] log4j:ERROR Error occured while sending e-mail notification.


                            • 11. Re: log4j email appender not working with jboss
                              alrubinger

                              Part of my application includes a mail service stateless session bean. When this mail service is loaded and the following code is executed, Log4J looks for SMTP on Port 465 as opposed to the settings defined in conf/log4j.xml. Seems Log4J is picking up a shared mail session?

                              Properties props = System.getProperties();
                               props.put(MailServiceBean.PROPERTY_KEY_SMTP_PROTOCOL,MailServiceBean.DEFAULT_VALUE_SMTP_PROTOCOL);
                               props.put(MailServiceBean.PROPERTY_KEY_SMTP_AUTH,true);
                               props.put(MailServiceBean.PROPERTY_KEY_SMTP_HOST,this.getSmtpHost());
                               props.put(MailServiceBean.PROPERTY_KEY_SMTP_USER,this.getSmtpUsername());
                               props.put(MailServiceBean.PROPERTY_KEY_SMTP_PASSWORD,this.getSmtpPassword());
                               props.put(MailServiceBean.PROPERTY_KEY_SMTP_PORT,new Integer(this.getSmtpPort()).toString());
                               props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                               props.put("mail.smtp.socketFactory.fallback", "false");
                               props.put("mail.smtp.socketFactory.port",new Integer(this.getSmtpPort()).toString());
                               SmtpAuthenticator auth = new SmtpAuthenticator();
                               auth.setUsername(this.getSmtpUsername());
                               auth.setPassword(this.getSmtpPassword());
                               mailSession = Session.getInstance(props,auth);
                              
                              


                              S,
                              ALR