Log4j: Unable to send mail using Log4j SMTP Appenders - java

I need to send a email when any Exception occurs in log file .This is my log4j.properties file
log4j.rootLogger=Info, EMAIL
# EMAIL
log4j.appender.EMAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.EMAIL.SMTPHost=smtp.gmail.com
log4j.appender.EMAIL.SMTPProtocol=smtp
log4j.appender.EMAIL.SMTPPort=25
log4j.appender.EMAIL.StartTls=true
log4j.appender.EMAIL.From=krn1231#gmail.com
log4j.appender.EMAIL.To=krn1231#gmail.com
log4j.appender.EMAIL.SMTPUsername=krn1231
log4j.appender.EMAIL.SMTPPassword=mypassword
log4j.appender.EMAIL.Subject=Test Gmail Smtp
log4j.appender.EMAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.EMAIL.layout.ConversionPattern=[%d] [%t] %-5p %c %x - %m%n
log4j.appender.EMAIL.Threshold=ERROR
log4j.appender.EMAIL.BufferSize=1
log4j.appender.EMAIL.SMTPDebug=true
This is my sample Program for Testing this
import org.apache.log4j.Logger;
public class Test {
private static Logger loggers = Logger.getLogger(Test.class);
public static void main(String args[]) {
try {
loggers.info("Hisas");
throw new NullPointerException();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I tried all ways of trail and Error , but of no use
This is the output given inside Eclipse Console , when i tried
log4j:WARN No such property [startTls] in org.apache.log4j.net.SMTPAppender.
DEBUG: setDebug: JavaMail version 1.4ea
java.lang.NullPointerException
at Test.main(Test.java:11)
When i executed this on command prompt
telnet smtp.gmail.com 25
It responded well with
220 mx.google.com ESMTP f8sm802040pbe.42

pass the vm arguement first i.e -Dmail.smtp.starttls.enable=true
log4j.rootLogger=DEBUG, sendMail
log4j.appender.sendMail=org.apache.log4j.net.SMTPAppender
log4j.appender.sendMail.SMTPProtocol=smtps
log4j.appender.sendMail.Threshold=ERROR
log4j.appender.sendMail.SMTPPort=465
log4j.appender.sendMail.SMTPUsername=xxxx#gmail.com
log4j.appender.sendMail.From=xxxx#gmail.com
log4j.appender.sendMail.SMTPPassword=.........
log4j.appender.sendMail.To=yyyyy#gmail.com
log4j.appender.sendMail.SMTPHost=smtp.gmail.com
log4j.appender.sendMail.Subject=Error Alert
log4j.appender.sendMail.layout=org.apache.log4j.PatternLayout
log4j.appender.sendMail.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss.SSS} [%p] %t %c - %m%n
log4j.appender.sendMail.smtp.starttls.enable=true
log4j.appender.sendMail.smtp.auth=true
log4j.appender.sendMail.BufferSize=1

Try the smtps protocol as Gmail requires SSL.

Related

Need information related to sshj library. How logging works?

I'm trying to transfer a file from one server to other server using sftp protocol. So, I'm trying to use sshj library for this. Now before transferring the file I'm performing several activities such as zipping and after transferring unzipping . So, I'm logging this details to successLog and if ay error to FailureLog.
here is my log4j properties:-
# Define the root logger
log4j.rootLogger = DEBUG, toConsole
# Define the console appender
log4j.appender.toConsole=org.apache.log4j.ConsoleAppender
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.toConsoleAppender.layout.ConversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
# Define the file appender
log4j.appender.success=org.apache.log4j.FileAppender
log4j.appender.success.File=logs/success.log
log4j.appender.success.Append=false
log4j.appender.success.layout=org.apache.log4j.PatternLayout
log4j.appender.success.layout.conversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
# Define the file appender
log4j.appender.failure=org.apache.log4j.FileAppender
log4j.appender.failure.File=logs/failure.log
log4j.appender.failure.Append=false
log4j.appender.failure.layout=org.apache.log4j.PatternLayout
log4j.appender.failure.layout.conversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
log4j.category.successLogger=DEBUG, success
log4j.additivity.successLogger=true
log4j.category.failureLogger=WARN, failure
log4j.additivity.failureLogger=false
and in java code I'm using this as :-
static final Logger successLog = Logger.getLogger("successLogger");
static final Logger failureLog = Logger.getLogger("failureLogger");
and example failureLog.error("exception occured due to so an so reason",e);
now I would like to store sshj library log in some file called sftp.log. How can I do that??so far I was manually logging into success and failure logs but sshj log is by default printing on console which I want to write it to some file.
Here's my example of writing the SFTP-session FINE-level log to file:
private void enableFineLogging() {
try {
fileHandler = new FileHandler(
"./logs/fine_sshj.log",
10000000, 1000, true);
fileHandler.setLevel(Level.FINER);
fileHandler.setFormatter(new SimpleFormatter());
final Logger app = Logger.getLogger("net.schmizz");
app.setLevel(Level.FINER);
app.addHandler(fileHandler);
app.setUseParentHandlers(false);
app.info(
"######################################################################## "
+ "START LOGGING NEW SSHJ SESSION "
+ "########################################################################");
} catch (Exception e) {
// Do something...
}
}
Though I'm not sure if are able to separate the log levels so that successLogger would have only the success cases... I think there will be also WARNING and SEVERE level messages.

Log4J API not printing messages in the java program

I am trying to implement Log4J API in my java project.
To do that, I have created a properties as shown in the image below (highlighted in yellow):
Project Structure Image
These are the properties I set in the file:
# TRACE < DEBUG < INFO < WARN < FATAL
log4j.rootLogger=TRACE, DEBUG, INFO, file
# Console
# log4j.appender.toConsole=org.apache.log4j.ConsoleAppender
# log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
# log4j.appender.toConsole.layout.ConversionPatter=%d{HH:mm:ss} %5p [%t] - $c.%M - %m%n
# Redirecting log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I declared the LOGGER object in my class as following:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Credits{
Logger LOGGER = null;
public Credits(String sources) {
LOGGER = Logger.getLogger(ChunkRecon.class.getName());
LOGGER.setLevel(Level.DEBUG);
String creditNames = "select tablename, creditNumbers, credit_type from schema.table where credit_type in ("sources")";
LOGGER.debug("This is a debug message");
system.out.println("This message is from println");
}
}
In the output, I see the message from sysout: This message is from println but not the debug message.
Could anyone let me know what is the mistake I am doing here ?
Try to rename your loggerproperties to log4j.properties and check it is in classpath. Another problem with rootLogger, see explanation here
Also Logger is usually used as static variable. For example:
public class Credits {
private static final Logger logger = Logger.getLogger(Credits.class);
public Credits(String sources) {
logger.setLevel(Level.DEBUG);
logger.debug("This is a debug message");
System.out.println("This message is from println");
}
}
log4j.properties
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Your log4j.rootLogger declaration seems incorrect. Try as -
log4j.rootLogger=TRACE,stdout,file
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
If you only want the logs on console then remove file logging fully.
log4j.rootLogger=TRACE,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Refer section Configuring loggers from JavaDoc.
The syntax for configuring the root logger is:
log4j.rootLogger=[level], appenderName, appenderName, ...

Using log4j JMSAppender with ActiveMQ - wire format timeout in debug level (client side)

I'm trying to make a POC using log4j JMSappender and ActiveMQ. I successfully follow this example -> http://activemq.apache.org/how-do-i-use-log4j-jms-appender-with-activemq.html
But this is for INFO level. I made some modification to use it in DEBUG level and my program and activeMQ can't exchange their wire format. Here are my log4j (client side)
log4j.rootLogger=DEBUG, stdout, jms
log4j.logger.org.apache.activemq=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n
log4j.appender.stdout.Threshold=INFO
log4j.appender.jms=org.apache.log4j.net.JMSAppender
log4j.appender.jms.InitialContextFactoryName=org.apache.activemq.jndi.ActiveMQInitialContextFactory
log4j.appender.jms.ProviderURL=tcp://localhost:61616
log4j.appender.jms.TopicBindingName=logTopic
log4j.appender.jms.TopicConnectionFactoryBindingName=ConnectionFactory
log4j.appender.jms.Threshold=DEBUG
When running my client, I have the wire format error.
Producer program:
import org.apache.log4j.Logger;
public class ProducerDebug {
public static void main( String[] args ) throws InterruptedException
{
new ProducerDebug();
}
private ProducerDebug() throws InterruptedException {
Logger logger = Logger.getLogger(ProducerInfo.class);
while (true)
{
logger.debug("push DEBUG");
logger.info("push INFO2");
Thread.sleep(10000);
}
}
}
And the error message:
log4j:ERROR Error while activating options for appender named [jms].
javax.jms.JMSException: Wire format negotiation timeout: peer did not
send his wire format. at
org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:72)
at
org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1412)
at
org.apache.activemq.ActiveMQConnection.ensureConnectionInfoSent(ActiveMQConnection.java:1501)
at
org.apache.activemq.ActiveMQConnection.createSession(ActiveMQConnection.java:323)
at
org.apache.activemq.ActiveMQConnection.createTopicSession(ActiveMQConnection.java:1134)
at
org.apache.log4j.net.JMSAppender.activateOptions(JMSAppender.java:218)
at
org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:307)
at
org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:172)
at
org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:104)
at
org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:842)
at
org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:768)
at
org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:648)
at
org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:514)
at
org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:580)
at
org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:526)
at org.apache.log4j.LogManager.(LogManager.java:127) at
org.apache.log4j.Logger.getLogger(Logger.java:117) at
awg.adb.producer.ProducerDebug.(ProducerDebug.java:13) at
awg.adb.producer.ProducerDebug.main(ProducerDebug.java:9) Caused by:
java.io.IOException: Wire format negotiation timeout: peer did not
send his wire format. at
org.apache.activemq.transport.WireFormatNegotiator.oneway(WireFormatNegotiator.java:98)
at
org.apache.activemq.transport.MutexTransport.oneway(MutexTransport.java:68)
at
org.apache.activemq.transport.ResponseCorrelator.asyncRequest(ResponseCorrelator.java:81)
at
org.apache.activemq.transport.ResponseCorrelator.request(ResponseCorrelator.java:86)
at
org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1387)
... 17 more
I tried many things and I cannot understand why I have this error.
For information: I'm using
- ActiveMQ 5.12.1
- Log4j 1.2.17
yes it is due to AMQ logs if level is debug, one solution is to do the opposite like this :
log4j.rootLogger=INFO, stdout
## Be sure that ActiveMQ messages are not logged to 'jms' appender
log4j.logger.org.apache=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n
## Configure 'jms' appender. You'll also need jndi.properties file in order to make it work
log4j.appender.jms=org.apache.log4j.net.JMSAppender
log4j.appender.jms.InitialContextFactoryName=org.apache.activemq.jndi.ActiveMQInitialContextFactory
log4j.appender.jms.ProviderURL=tcp://localhost:61616
log4j.appender.jms.TopicBindingName=logTopic
log4j.appender.jms.TopicConnectionFactoryBindingName=ConnectionFactory
log4j.appender.jms.Threshold=DEBUG
log4j.logger.your.package=jms

log4j / slf4j not able to read from properties file want working solution please

here below i used a sample app.ication to show the eraror, it never creates a log file
package com.example;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class logtester {
static final Logger logger = Logger.getLogger(logtester.class);
#Value("${log4j.appender.R.MaxFileSize}")
public static void main(String[] args) {
PropertyConfigurator.configure("logProperties.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
// System.out.write("a2nd method");
PropertyConfigurator.configure("application.properties");
//System.out.write("weocme to usa");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
// System.out.write("a2nd method");
}
}
the above is my main file and now i will post my prpoperties files
i used two to check if any of them works but nbuth did not
application.properties file
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=TVK.log
log4j.appender.FILE.MaxFileSize=512KB
log4j.appender.FILE.MaxBackupIndex=3
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%og4j.appender.NotConsole=org.apache.log4j.RollingFileAppender
log4j.appender.NotConsole.fileName=/some/path/to/a/fileName.log
log4j.appender.NotConsole.maxFileSize=20MB
another properties file : log4jproperties.properties
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=TVK.log log4j.appender.FILE.MaxFileSize=512KB
log4j.appender.FILE.MaxBackupIndex=3
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%
log4j.appender.NotConsole=org.apache.log4j.RollingFileAppender
log4j.appender.NotConsole.fileName=/some/path/to/a/fileName.log
log4j.appender.NotConsole.maxFileSize=20MB
log4j.appender.MAIN_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.MAIN_LOG.File=${catalina.base}/logs/webtop.log
log4j.appender.MAIN_LOG.layout=com.j256.ormlite.Log4JSimpleLayout
log4j.appender.MAIN_LOG.MaxFileSize=10MB
log4j.appender.MAIN_LOG.MaxBackupIndex=5
log4j.appender.MAIN_LOG.append=true
log4j.rootCategory=ALL, MAIN_LOG
There are multiple PropertyConfigurator.configure() methods (see https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html).
The one you use looks for a file 'logProperties.properties' in the file system from the directory where you start the application.
If that file is packaged with the application, you should use the configure method taking the input stream:
PropertyConfigurator.configure(
logtester.class.getResourceAsStream("/logProperties.properties");
Anyway, if log4j can't find the properties file, it should display some error:
log4j:ERROR Could not read configuration file [application.properties].
java.io.FileNotFoundException: application.properties (The system cannot find the file specified)
Do you have something similar?

Kafka Log4j appender not sending messages

i am quite new ot apache Kafka and log4j. i am trying to send my log messages into Kafka. Here is my log4j properties file
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %% %m%n
log4j.appender.KAFKA=kafka.producer.KafkaLog4jAppender
log4j.appender.KAFKA.BrokerList=localhost:9092
log4j.appender.KAFKA.Topic=kfkLogs
log4j.appender.KAFKA.SerializerClass=kafka.producer.DefaultStringEncoder
log4j.appender.KAFKA.layout=org.apache.log4j.PatternLayout
log4j.appender.KAFKA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %% - %m%n
log4j.logger.logGen=DEBUG, KAFKA
however, i am not able to receive any messages in my consumer. I have tested the consumer with some other producer code and it works fine.
also, i get this warning
log4j:WARN No such property [serializerClass] in kafka.producer.KafkaLog4jAppender.
Edit
and here is the code that generates my log messages
package logGen;
import org.apache.log4j.Logger;
public class TestLog4j {
static Logger log = Logger.getLogger(TestLog4j.class.getName());
public static void main(String[] args) {
log.debug("Debug message");
log.info("Info message");
log.error("Error Message");
log.fatal("Fatal Message");
log.warn("Warn Message");
log.trace("Trace Message");
}
}
Also, if i write the log messages to a file by using something like
log4j.appender.KAFKA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.KAFKA.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.KAFKA.File=logs/server.log
i can see the log messages in the server.log file
thanks for the suggestions everyone. i think the weird behavior that i see might be related to my kafka set up. here are the contents of my server.properties file which i use to start up my kafka server. can you see anything odd about it ?
broker.id=0
port=9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/Users/xyz/kafka/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
log.cleaner.enable=false
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=6000
delete.topic.enable=true
I have took a look at the source code of KafkaLog4jAppender.scala and here are the valid and exhaustive properties for Kafka log4j appender as of version 0.8.2.1 : topic, brokerList, compressionType, requiredNumAcks, syncSend.
The log4j.properties that worked for me is below :
log4j.rootLogger=ERROR, stdout
log4j.logger.logGen=DEBUG, KAFKA
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %% %m%n
log4j.appender.KAFKA=kafka.producer.KafkaLog4jAppender
log4j.appender.KAFKA.topic=LOG
log4j.appender.KAFKA.brokerList=localhost:9092
log4j.appender.KAFKA.compressionType=none
log4j.appender.KAFKA.requiredNumAcks=0
log4j.appender.KAFKA.syncSend=true
log4j.appender.KAFKA.layout=org.apache.log4j.PatternLayout
log4j.appender.KAFKA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %% - %m%n
You need to add KAFKA to your log4j.rootLogger like this:
log4j.rootLogger=INFO, stdout, KAFKA
This will add the KAFKA appender to your rootLogger.
i had to specify
log4j.appender.KAFKA.producer.type=async
log4j.logger.logGen.TestLog4j=TRACE, KAFKA
and that seems to work. however i experience a delay of 10-30 seconds. Specifically, if i publish right now and can see the messages in teh consumer, then the next time i publish has to be about 30 secs later else i dont see anything in my consumer. any ideas on why this might be happening ? maybe its an eclispe issue ?
log4j.appender.KAFKA.SerializerClass=kafka.producer.DefaultStringEncoder
could you try
log4j.appender.KAFKA.Serializer=kafka.producer.DefaultStringEncoder
Instead?
I believe the async mode of sending msgs does so by batching, hence the delay, have you you tried sending using sync?

Categories

Resources