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){
...
}
Related
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
I hava jar with class my.package.Foo inside, containing main method.
What is more i have Log4j configurated as a logging system.
I want to print full stack trace while exception is catched, however i read this topic: log4j not printing the stacktrace for exceptions
and i think that i need to use a -XX:-OmitStackTraceInFastThrow flag.
So i'm trying to call my app with command line like this:
java -XX:-OmitStackTraceInFastThrow -cp %JAR_LOCATION:% my.package.Foo
However i'm still missing stacktrace, i get only short exception message.
Here is my log4j config:
log4j.rootLogger=INFO, CONSOLE, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=log.txt
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=false
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-4p %c{2}.%m%n
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} %-4p %c{2}.%m%n
What i'm missing?
Try it like this:
set JAR_LOCATION=c:\any folder with spaces\jar
java -XX:-OmitStackTraceInFastThrow -cp "%JAR_LOCATION:~%" my.package.Foo
You have to remove the ":" char from %JAR_LOCATION:%, or use %JAR_LOCATION:~% to de-quote it, and quote it directly at the JAVA command, because the path probably contains spaces.
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?
am using log4j-1.2.15.jar for enable logging .and its writing all logs to a file.
this is what in my log4j.properties.
log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = log.NewLogForEachRunFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c - %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = D:/log/logs.log
It was working fine when am trying to run this from my local server configured in eclipse.
But the same is not working when i had deployed that into the production development enviornment.This is what am getting in the console.
no output stream or file set for the appender named [fileout]
Can anyone give a solution.?
Your configuration looks ok. I assume the D:/log/logs.log is available in production environment.
You might want to try log4j configuration debugging by setting -Dlog4j.debug on the command line. It often points out useful configuration errors.
I have a log4j properties file which is creating a file inside my tomcat>bin folder but instead can it write the log file to my project's root dir? webapps>test>___?
Here is my log4j properties file contents.
#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
# now define the layout for the appender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%t %-5p %c{3} - %m%n
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=/test/a.log
log4j.appender.rollingFile.MaxFileSize=10MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
# now map our console appender as a root logger, means all log messages will go to this appender
#for console printing
#log4j.rootLogger = DEBUG, consoleAppender
#for file printing
log4j.rootLogger = DEBUG, rollingFile
Try replacing this:
log4j.appender.rollingFile.File=/test/a.log
with this:
log4j.appender.rollingFile.File=../webapps/test/a.log
Note (by Stephen C) - the "../" means this solution depends on whether or not the Tomcat launch mechanism you use makes $CATALINA_HOME the current directory before the JVM that hosts Tomcat. Some do, and some don't.
The log4j configurations understand "${catalina.home}", so ...
log4j.appender.rollingFile.File=${catalina.home}/webapps/test/a.log
However, I don't think it is a good idea to put logs into the webapps tree because they are liable to be blown away if your webapp is redeployed.
Put them in ${catalina.home}/logs instead.
Or better still, put them in the distro-specific conventional place to put application logfiles; e.g. "/var/spool/..." or "/var/log/...".
Putting logfiles in standard places means there are less places for someone else (e.g. the guy who is the backup sysadmin when you are on holiday) to investigate if the file system fills up with old logfiles.