5

I've looked into other questions that are similar and have done some googling to find my answer but my question is still unanswered. I'm still unfamiliar with how some of this works, so bear with me.

Our maven pom.xml is using the slf4j dependency:

pom.xml importing slf4j from using maven

and our log4j2.xml file looks like this:

log4j2.xml file containing smtp appender

I only added this to the above log4j2.xml file <SMTP name="Mail" ...> </SMTP> and lower down in the file i added <logger name="com.path.class1" ...> <appender ...> </logger>

But for some reason, when I call log.error("error message"), my email isn't sent to me. I know the smtp host works because it is used in a .NET service. The smtp server does not require any credentials to use it. I know that my log.error call is in the correct directory and file path. I've tried setting the port, but that didn't make any difference. How do i go about getting the email notifications to work?

I even tried the mailAppender, but that didn't work either:enter image description here

j will
  • 3,747
  • 11
  • 41
  • 64

3 Answers3

1

So i finally was able to get SMTP notifications to work. This is the log4j2.xml file that made it work:

enter image description here

j will
  • 3,747
  • 11
  • 41
  • 64
0

In your log4j.xml

Try configuring it with Mail Appender

make sure you are able to send email using the SMTP of the server.

<?xml version="1.0" encoding="UTF-8"?>

<appender name="mailAppender" class="org.apache.log4j.net.SMTPAppender">
    <param name="BufferSize" value="50" />
    <param name="SMTPHost" value="smtp.mail.example.com" />
    <param name="SMTPPort" value="587" />
    <param name="SMTPUsername" value="myemail_id@example.com" />
    <param name="SMTPPassword" value="mypassword" />
    <param name="From" value="fromemail_id@example.com" />
    <param name="To" value="to_emailid@example.com" />
    <param name="Subject" value="Testing Log4j mail notification" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{ISO8601}]%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>

<root>
    <priority value="info" />
    <appender-ref ref="mailAppender" />
</root>

neo7
  • 654
  • 5
  • 14
  • I tried your suggestion and edited my question above explaining what i did. – j will Oct 01 '13 at 20:32
  • are you able to send emails using the smtp server provided in the configuration, through the server on which your slf4j is running? I also recommend this answer http://stackoverflow.com/questions/18945084/slf4j-logback-refer-to-appender-that-is-not-referenced-from-any-logger – neo7 Oct 02 '13 at 16:50
  • @Remko Popma's solution would give you exact slf4j error too. – neo7 Oct 02 '13 at 17:17
0

You cannot mix log4j2 and log4j-1.x appenders. The Log4J2 SMTP appender should work.

I suspect some error is occurring. Do you have the java mail jars in the classpath? Can you change the start of your log4j2.xml config so that it will print debug messages to the console?

<Configuration status="debug"...
Remko Popma
  • 35,130
  • 11
  • 92
  • 114