Jar file ran by cron doesn't write his own log - java

I have a simple java program (Java version 1.7), which is an email sender, and I want to log some events during its running. So I decided to use org.apache.log4j.Logger for this. I created the log4j.properties file which contains the following:
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=Autoemail.log
log4j.appender.file.MaxFileSize=2MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
log4j.appender.file.encoding=UTF-8
This is how I load the configuration:
PropertyConfigurator.configure(cl.getResource("resources/log4j.properties"));
logger = Logger.getLogger("");
logger.info("Auto email sending started...");
where cl is the classloader. When I run this from Eclipse on my localhost, everything works as desired, the log file has been created if didn't exist, and on next run it is appended.
Then I wanted to run this email sender program automatically every day at 7 am. I created the scheduled cron job, which executes the following:
java -jar /var/projects/Autoemail/Autoemail.jar 2>> /var/projects/Autoemail/Autoemail.err
On first run it worked fine, the log file had been created and info or error had been inserted, but since then the program doesn't write any event into it. The jar file runs, because I can see the sent emails. When I started searching for the reason, and ran the command, it showed me a Permission denied error. I checked the rights of the log file, and it was: rw-rw-r--. So I modified, at last to rwxrwxrwx, but still doesn't write the log, and I can't see any error message in the syslog file. The owner doesn't need sudo right, I think. BTW the server is Ubuntu 13/04.
Does anyone have any idea of what can be wrong, what is still needed, or what do I have to do?

Related

How to define log4j default file location

I'm setting up "log4j" logging framework to my java project, and I want to define a default path to the log4j.appender.X.File=${file.name}configuration property, if nothing to the ${file.name} argument has been specified by the comand line.
I need the solution to be dynamic:
- when I run the .jar file using -Dfile.name=C:\log\directory\path\log4j-application.log JVM argument on the comand line --> the framework should write to the path that I've specified on the argument;
- when I run the .jar file without using the JVM argument on the comand line --> the framework should write on the .jar file path;
Where should I give the default path to make my project understand where it should write the file on?
This is a part of my actual log4j.properties file:
# Root logger option, use one of the following log levels: INFO, WARN, ERROR, DEBUG, TRACE
log4j.rootLogger=DEBUG, stdout, file
[...]
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${file.name}
log4j.appender.file.MaxFileSize=100MB
log4j.appender.file.MaxBackupIndex=3
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout=com.webratio.rtx.log.SessionAwarePattern
log4j.appender.file.layout.ConversionPattern= %d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true
log4j.appender.file.Threshold=TRACE
With the attached settings, the app is working partly:
1. java -Dfile.name=C:\log\directory\path\log4j-application.log -jar C:\Users\martin\Desktop\Projects\Log4j\log4j_example.jar: including the JVM argument, the .jar correctly created the log file inside the already defined JVM argument path;
2. java -jar C:\Users\martin\Desktop\Projects\Log4j\log4j_example.jar: excluding the JVM argument, the .jar is not able to understand where it should write on, therefore no file has been created;
I was expecting that, on the second attempt, the app could write on the same .jar file folder, since the ${file.name} argument was not defined on the comand line.That didn't work because it seems that something is missing to assign that "default" path to write on.
I also tried setting log4j.appender.file.File=${file.name}/log4j.log on the log4j.properties file instead, giving java -Dfile.name=C:\log\directory\path\ -jar C:\Users\martin\Desktop\Projects\Log4j\log4j_example.jaras a comand and excluding the log4j.log name to the path.
Niether this attempt is working for me, since system is raising the following error:
[...]
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: \log4j.log (Access denied)
[...]
This time I was expecting that the framework could write on the same .jar file folder, since the ${file.name} argument was not defined on the comand line and atuomatically get the root of the .jar file (expecting that's the default writing folder).
I'm using the 1.2.17 log4j version.
#in properties file put like this
log4j.appender.file.File=./history.log

How to enable/setup log4j for oozi java workflows?

I'm running an Oozie Java workflow (the jar file is in HDFS), and I'd like to add logging functionality to my application. Does anybody know how to do it? Where should I put my "log4j.properties" file? How can I make log4j to output the log to a location in HDFS?
Looking in this documentation, you can try adding oozie-log4j.properties in your oozie directory (where workflow.xml is).
Here are the default settings:
log4j.appender.oozie=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.oozie.RollingPolicy=org.apache.oozie.util.OozieRollingPolicy
log4j.appender.oozie.File=${oozie.log.dir}/oozie.log
log4j.appender.oozie.Append=true
log4j.appender.oozie.layout=org.apache.log4j.PatternLayout
log4j.appender.oozie.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
log4j.appender.oozie.RollingPolicy.FileNamePattern=${log4j.appender.oozie.File}-%d{yyyy-MM-dd-HH}
log4j.appender.oozie.RollingPolicy.MaxHistory=720
It also outlines the following restrictions:
In order for Oozie logging to work 100% correctly, the following restrictions must be observed (described below and in the oozie-log4j.properties file):
The appender that Oozie uses must be named "oozie" (i.e. log4j.appender.oozie )
log4j.appender.oozie.RollingPolicy.FileNamePattern must end with "-%d{yyyy-MM-dd-HH}.gz" or "-%d{yyyy-MM-dd-HH}". If it ends with ".gz" the old logs will be compressed when rolled
log4j.appender.oozie.RollingPolicy.FileNamePattern must start with the value of log4j.appender.oozie.File

Issue with log4j.properties on the command propmt

I'm trying to create a batch file for Windows (*.bat) for executing a Java application. I've set up everything but the logger. I have made the call to BasicConfigurator.configure() and put the file log4j.properties on the root of the project which works fine in the IDE (Eclipse) but when I try to do the same through a batch file, it doesn't work.
I've already tried to move the file to other path but it didn't work and I don't know what else to try.
eInvoice.bat (CLASSPATH is suppose to be defined previously)
java -Xmx500m -cp %CLASSPATH% com.mycompany.einvoice.InvoiceSender -Dlog4j.configuration=log4j.properties
log4j.properties
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=AplFacturae.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'm not sure if this is useful for someone, but I found the source of the problem and It was related with the order of the parameters. The main class has to be the last parameter unless it has parameters. So I just swapped the last two parametes and it started to work
java -Xmx500m -cp %CLASSPATH% -Dlog4j.configuration=log4j.properties com.mycompany.einvoice.InvoiceSender
In the moment I fix the problem I realized the reason. The main class can have paramentes, so, every parameter I use before of the main class, belongs to the main class instead of Java.
public static void main (String[] args){
...
}

Location change for Logs in play framework

I have been newly working on Play framework and the version is 1.2.5. The logs for my application are stored at location where the entire application is installed. I have installed my application package in C:\app location and running it through Java wrapper.
I am confused as my log.properties file do not say anything about the stored location of log, as shown below, but still it is logging in default log folder of play.
Below is the exactly copied log.properties file for my application which is working fine. Few lines are commented but that is how I have received it.
#properties file used for log4j
log4j.rootLogger=ERROR, Console
log4j.logger.play=DEBUG
# Rolling files
#log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
#log4j.appender.Rolling.File=logs/application.log
#log4j.appender.Rolling.MaxFileSize=1MB
#log4j.appender.Rolling.MaxBackupIndex=100
#log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
#log4j.appender.Rolling.layout.ConversionPattern=%d{DATE} %-5p ~ %m%n
# Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n
All I want to know is how Play is storing it there, Is there any class of Play that has this location stored, and How can I configure to change the location of building logs to other drive say E:\logs?
It is responsible for the location of the log folder,
#log4j.appender.Rolling.File=logs/application.log
will generate logs in the application path with a folder logs
you can change it to the desired location E:\logs in the above line to generate logs in the E:
You will find the default settings of logging,
# Logger
# ~~~~~
section in application.conf inside your /conf directory of your play app.

configuring log4j to display errors

I've configured my log4j.properties so that it puts all the info and debug messages in a separate log file in tomcat. However, when the application fails, say bad sql command then the errors don't show up in that file but instead show up in the regular localhost log file.
How can I configure the properties such that the errors show up in that file as well?
log4j.rootCategory=INFO, logfile
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile.File=${catalina.home}/logs/myapplog.log
log4j.category.org.springframework.web=INFO
log4j.category.com.package.app=DEBUG
log4j.category.org.springframework.samples.mvc31=INFO
log4j.logger.org.springframework.jdbc.core=DEBUG
The type of errors I want to see in my log file are like these:
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name...
If you are catching the exceptions/errors(Throwable) and putting log.error statements in your catch blocks then only you will be able to see the exception messages in your log file. For all the exceptions that are not caught in your code will be basically pushed to the console logger of tomcat and hence you are seeing the exception traces in tomcat out log file.
So try to catch all the possible exceptions in your code and log them properly so that they appear in your log files.

Categories