0

I am developing a mobile application and im using log4j to display information. I want to use log4j to display information on the console which it is doing correctly but i am having trouble when I try adding a SMTPAppender to send a email when ever an error occurs. It is not displaying an error but the tomcat does not start correctly when I start it with the SMTPAppender is added.

I am using Apache Tomcat 7.0.23

This is my log4j.properties file

log4j.rootCategory=TRACE, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} [application] %-5p %m%n


log4j.rootCategory=ERROR, mail

log4j.appender.mail=org.apache.log4j.net.SMTPAppender
#defines how often emails are send
log4j.appender.mail.BufferSize=1
log4j.appender.mail.SMTPHost=smtp.gmail.com
log4j.appender.mail.SMTPUsername=*******   #not the username im using
log4j.appender.mail.SMTPPassword=*******   #not the password im using
log4j.appender.mail.From=info@gmail.com
log4j.appender.mail.To=noc@gmail.com
log4j.appender.mail.Subject="Application.log error occurred"
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d{ISO8601} [application] %-5p %m%n

This is my console

Found binding in [jar:file:/C:/Documents%20and%20Settings/noconnor/.m2/repository/org/slf4j/slf4j-simple/1.6.1/slf4j-simple-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Documents%20and%20Settings/noconnor/.m2/repository/org/slf4j/slf4j-log4j12/1.6.2/slf4j-log4j12-1.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

is this the correct way to add a second appender or am i making a mistake somewhere?

newSpringer
  • 1,018
  • 10
  • 28
  • 44
  • 1
    Btw, also see this for how to solve it in using properties syntax: http://stackoverflow.com/a/728351/47190 (note the `Threshold` parameter). – Peter Štibraný May 04 '12 at 14:25

1 Answers1

2

You can only have one log4j.rootCategory property.

You can have multiple appenders:

log4j.rootCategory=TRACE, stdout, mail

But you probably don't want all messages go to mail. For this, you need to add priority filter to your mail appender, but I think that's only possible to do using XML syntax instead of log4j.properties.

Here is an example for using both appenders, but "mail" appender is only activated for ERROR and FATAL messages.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 
    <param name="Target" value="System.out"/> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%d{ISO8601} [application] %-5p %m%n"/> 
    </layout> 
  </appender> 

  <appender name="mail" class="org.apache.log4j.net.SMTPAppender">
    <!-- mail configuration here... -->

    <layout class="org.apache.log4j.PatternLayout"> 
        <param name="ConversionPattern" value="%d{ISO8601} [application] %-5p %m%n"/> 
    </layout> 

    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="levelMin" value="ERROR" />
        <param name="levelMax" value="FATAL" />
    </filter>
  </appender>

  <root> 
    <priority value="trace" /> 
    <appender-ref ref="stdout" /> 
    <appender-ref ref="mail" />
  </root>

</log4j:configuration>

You need to add more mail configuration options to match your original properties file. This file must be called "log4j.xml" and must be used instead of "log4j.properties" (remove this one).

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
  • This sorted the problem :) as i want it to email me whenever there is an email would it be better to change TRACE to ERROR? – newSpringer May 04 '12 at 14:13
  • If you change "log4j.rootCategory=ERROR, stdout, mail", you will only get ERROR messages in both appenders, console and mail. I guess you want more messages on the console. I'll try to put an example together for you that will solve the problem. – Peter Štibraný May 04 '12 at 14:15
  • would you please if its not too much bother... because ive only started learning this today so am not fully sure how it all works yet – newSpringer May 04 '12 at 14:16
  • @newSpringer: check out the example I've added. You need to fill in the mail configuration (``, etc.), but the main idea is to attach a filter to the appender. – Peter Štibraný May 04 '12 at 14:20