KUNJESH KUMAR MISHRA
KUNJESH KUMAR MISHRA

Reputation: 41

Unable to send error mail using Apache log4j2/slf4j

I am not able to send error mail using log4j2. Also, it is not showing any type of exception or something. Here's my log4j2.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <RollingRandomAccessFile name="LogToRollingRandomAccessFile" fileName="logs/app.log" 
        filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingRandomAccessFile>

        <SMTP name="LogToMail" subject="Capacity Platform: Error mail notification"
              from="[email protected]"
              to="[email protected]"
              smtpHost="<mySMTP>"
              smtpPort="<smtpPort>"
              smtpUsername="<username>"
              smtpPassword="<password>"
              bufferSize="100">
              <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </SMTP>

    </Appenders>
    <Loggers>
        <Logger name="com" level="info">
            <AppenderRef ref="LogToConsole"/>
            <AppenderRef ref="LogToRollingRandomAccessFile"/>
        </Logger>
        <Root level="error" additivity="false">
            <AppenderRef ref="LogToMail"/>
        </Root>
    </Loggers>
</Configuration>

I am able to create log file (app.log) and able to print the error on console using this log4j2.xml, but, I am not able to send error mail.

Here's the java code:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ErrorClassSample {

static final Logger logger= LoggerFactory.getLogger(ErrorClassSample.class);

public void errorMethod() {
try {
            System.out.println(getData());
        } catch (IllegalArgumentException e) {
            logger.error("{}", e);
        }

}

private int getData() throws IllegalArgumentException {
        throw new IllegalArgumentException("Sorry IllegalArgumentException!");
    }
}

I am using it in Spring 4.0.2.

Upvotes: 0

Views: 427

Answers (1)

KUNJESH KUMAR MISHRA
KUNJESH KUMAR MISHRA

Reputation: 41

Due to my smtpPort , I was not able to receive any mails. After changing it to right one, I am able to get the error mails.

Thanks.

Upvotes: 0

Related Questions