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"));
Related
Always I try to use a formal and structurized log api is a nightmare (this explains why so many people just print to console)
I'm trying to use log4j in my project
to instantiate the logger I do:
private static final Logger log = LoggerFactory.getLogger(Instagram.class.getPackage().getName());
when I want to register an event I do:
log.info("some information");
I've a log4j.properties file in the src folder like:
log4j.logger.com.tomatechines.instagramapi.api = INFO, CONSOLE, FILE, ERROR
log4j.rootLogger = INFO, FILE, ERROR
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern = yyyy-MM-dd'.log'
log4j.appender.FILE.File = logs/log-
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
log4j.appender.ERROR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.DatePattern = yyyy-MM-dd'.log'
log4j.appender.ERROR.File = logs/errorlog-
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
log4j.appender.ERROR.Threshold=WARN
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
but when the code runs the only thing printed to console or file is:
log4j:WARN No appenders could be found for logger (com.tomatechines.instagramapi.api).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
why it doesn't find my file, I set 3 appenders and why can't it find just one
First of all, I can see that you are using SLF4J with log4j 1.2.x, because you are instantiating the logger with LoggerFactory.getLogger. If you want to use only log4j 1.2.x you should get the logger with Logger.getLogger. Making sure that you have imported the classes from org.apache.log4j package.
You must also make sure that you have put the log4j.properties file under the classpath. And in my opinion you should try first with a very simple configuration, to avoid configuration problems. Something like:
# Root logger option
log4j.rootLogger=INFO, stdout
# 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
Probably your problem is about the location of log4j.properties file.
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).)
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
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