I have a Spring Boot application. I'm trying to send log to mail using SMTPAppender, but I don't get any mails. I've managed to use log4j for ConsoleAppender and RollingFileAppender, so I guess I have log4j added properly, and the file with log4j properties is visible.
Here is my log4j.properties file:
log4j.rootLogger = info, email, stdout, file
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=smtp.google.com
log4j.appender.email.SMTPUsername=xyz#gmail.com
log4j.appender.email.SMTPPassword=abc123
log4j.appender.email.From=xyz#gmail.com
log4j.appender.email.To=xyz#gmail.com
log4j.appender.email.Subject=Log
log4j.appender.email.BufferSize=1
log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m
#------------------------------------------------------------------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#------------------------------------------------------------------------------------------
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylog.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I found a important piece from the documentation about SMTPAppender.
By default, an email message will be sent when an ERROR or higher severity message is appended. The triggering criteria can be modified by setting the evaluatorClass property with the name of a class implementing TriggeringEventEvaluator, setting the evaluator property with an instance of TriggeringEventEvaluator or nesting a triggeringPolicy element where the specified class implements TriggeringEventEvaluator
Also, you can't use "smtp.google.com" - it should be smtp.gmail.com
You should use GmailSMTPAppender, the details are here -
http://www.tgerm.com/2010/05/log4j-smtpappender-gmail-custom.html
Related
I added Log4j2 to the project, and added config.
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to 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
Than, I log code like here:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
...
final static Logger logger = LogManager.getLogger(AbstractEditor.class);
...
logger.info("updated: " + entity);
logger.debug("==> debug");
logger.info("==> info");
logger.warn("==> warn");
logger.error("==> error");
logger.fatal("==> fatal");
logger.trace("==> trace");
As I understand, all logs with level higher than DEBUG must be written to console and file. But only this has been printed into console:
15:08:52.285 [http-nio-8080-exec-1] ERROR ru.example.AbstractEditor - ==> error
15:08:52.292 [http-nio-8080-exec-1] FATAL ru.example.AbstractEditor - ==> fatal
I see this strings not matches my config. And they are not witten into file. When I added this config, all logs disappeared from console, excluding this 2 strings.
Please help to write config to see all logs with level from DEBUG on console and file.
You are programmatically using Log4j 2 but using the configuration format of Log4j 1. Log4j 2 is ignoring your configuration and using the default. You have 2 choices.
Convert your configuration to use Log4j 2 syntax (recommended), or
Enable Log4j 2's experimental support for log4j 1 configuration files by setting the system property "log4j1.compatibility=true". This support was added in release 2.13.0 so you would have to be using that version. The property file would also have to be named log4j.properties, not log4j2.properties.
I'm actually on a project using slf4j/log4j.
For that, we use log4j.properties files to configure the logging, especially the DailyRollingFileAppender.
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.com.thales.ecosystem=DEBUG,stdout,file
log4j.additivity.com.thales.ecosystem=false
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
# this is set dynamically
log4j.appender.file.File=${log.basedir}/decoders/nm-flight-decoder.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} v${release.version} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true
# Direct log messages to 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.SSS} v${release.version} %-5p %c{1}:%L - %m%n
And in code, we use the loggerFactory :
private static final Logger LOGGER = LoggerFactory.getLogger(xxxxxxxx.class);
But we have now to deal with multiple (but limited in number) identical process.
So the question is : how can we have two process (with the same class)
with only one log4j.properties per class, writting on a file each, and keeping the DailyRollingFileAppender ?
Thank's !
Make the file name in the configuration a property, and start each process with a different value for that property.
log4j.appender.file.File=${log.basedir}/decoders/${proc}.log
Start one process with -Dproc=nm-flight-decoder, and the other one with a different value.
(Also, consider upgrading to Log4j2. Log4j 1.2 has been End of Life since summer 2015 (archived here) and is known to be broken on Java 9 (archived here).)
Currently my code is printing logs on the console, yet it's not creating a file with the logs. This is the code of log4j.properties:
log4j.rootLogger=file, stdout
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C:/logs/logging.log
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %L - %m%n
log4j.appender.file.Append=true
log4j.appender.file.Threshold=INFO
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} %m%n
It's in the resource folder of my spring application.
And this is where i use it:
public void getPreviousDay() {
PropertyConfigurator.configure("log4j.properties");
logger = Logger.getLogger(LocationScheduler.class);
logger.info("Test");
}
There are couple of changes you need to make to your log4j.properties
First of all log4j.rootLogger specifies Log Level and than the appender. so in your case it has to be
log4j.rootLogger= INFO, file, stdout
Instead of log4j.appender.file=org.apache.log4j.FileAppender use log4j.appender.file=org.apache.log4j.RollingFileAppender in order to get a rollover logfile.
Hope this helps.
EDIT
It seems your program is not able to reach your log4j.properties and the reason is the method you are using for loading it only takes a fully qualified path of the log4j.properties
If you want to load your log4j.properties from classpath use below version:
PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));
I want my Hibernate specific logs to go to a separate file using log4j.
I tried this:
# Root logger option--First parameter is logLevel, second says redirect to file , third says redirect to standard output alos
log4j.rootLogger=INFO, file, stdout
# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=INFO,hblog
# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=D:\\a2bnextLogs\\a2bnext.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct hibernate messages to a log file
log4j.appender.hblog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.hblog.File=D:\\a2bnextLogs\\hibernate.log
log4j.appender.hblog.layout=org.apache.log4j.PatternLayout
log4j.appender.hblog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
But this doesent seem to work.Could anyone suggest what changes I need to make in above configuration?
I have a problem and an uncertainty relating to using Log4j.
I can't write the log to a file. I only get a warning that says:
log4j:WARN No such property [file] in org.apache.log4j.ConsoleAppender.
Logging to console works fine.
I want to store the log file outside the context root. In this case, would I hard code the absolute path into the properties file? Sorry if that's a dumb question.
Log4j.properties
# Root logger option
log4j.rootCategory=INFO, file, stdout
# Direct log messages to stdout
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/Users/me/Documents/apache-tomcat-7.0.34/wtpwebapps/my_app/log.txt
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
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
LogInit.java
String root = Environment.getRootDirectory(getServletContext());
String propertiesUri = root + propertiesUriParameter;
File propertiesFile = new File(propertiesUri);
if (!propertiesFile.exists())
{
System.out.println("Initializing log4j with: " + propertiesUri);
PropertyConfigurator.configure(propertiesUri);
}
Change this from :
log4j.appender.file.File=Users/Me/Documents/apache-tomcat-7.0.34/wtpwebapps/my_app/log.txt
To
log4j.appender.file.File=/Users/Me/Documents/apache-tomcat-7.0.34/wtpwebapps/my_app/log.txt
And make sure below path exists
/Users/Me/Documents/apache-tomcat-7.0.34/wtpwebapps/my_app/
Regards,
Shiva Kumar SS