How to define log4j default file location - java

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

Related

How to change the path for logging.file in (logback.xml) for UAT server?

I am trying to run a project on a UAT (Linux) server, but there is a problem, I need the logs to be written on the test server in a different path.
The project resources contain logback.xml, which specifies where to write logs:
<property name = "logging.path" value = "/logs"/>
<property name = "logging.file" value = "app-logger"/>
I cannot change this file, because in this case everything is correct, but for UAT I created another logback.xml and put it not in the resources but in another directory, and when I run the jar file I want to specify that I need to take this one logback.xml
On UAT I execute this command
java -Xmx512M -jar /home/user/app/program/program-0.1.jar --logging.config = app/program/config/logback.xml
I got this error:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender [RollingFile] - openFile (/logs/app-logger.log,true) call failed. java.io.FileNotFoundException: /logs/app-logger.log (No such file or directory)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration (LogbackLoggingSystem.java:169)
So the second file logback.xml which I created is ignored and the one that lies in the resources is used. What am I doing wrong? How to put another logback.xml into Spring?
You can add in your required log directory to your environment specific properties file,
should be something like application.uat.properties. So you would add in something like LOG_DIR=path/to/directory
Once that is done add in '<property resource="application.${env}.properties"/>' to your logback.xml
And finally in your file appender , add '<file>${LOG_DIR}/serive.log</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){
...
}

Running java application from batch with flags

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.

Error: "setFile(null,false) call failed" when using log4j

I have added log4j.properties file in source folder of project but I am still getting a log4j:error.
Here is my Log4j.properties file:
.rootCategory=DEBUG, R, O
# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender
log4j.appender.O=log44j.log
# File
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
# Define the root logger with appender file
logDir = ../logs
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=logs/${file.name}
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
Here is the Java exception that I am getting:
log4j:ERROR setFile(null,false) call failed.
java.io.FileNotFoundException: logs (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
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:809)
at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:547)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
at org.apache.log4j.Logger.getLogger(Logger.java:104)
at lib.Dashboard.Reports.<init>(Reports.java:34)
at testcases.AmazonDashboard.TC_DB17.main(TC_DB17.java:54)
AmazonDashboardTC_DB17Exception in thread "main" java.lang.NullPointerException
at testcases.AmazonDashboard.TC_DB17.main(TC_DB17.java:131)
Please let me know, how to resolve this exception, as I have tried placing my properties file in root folder and now I have placed in source folder but in both cases I got the above exception.
I suspect that the variable ${file.name} is not substituted correctly. As a result, the value of log4j.appender.FILE.File becomes logs/. As such, Java tries to create a log file named logs/, but probably it is an existing directory, so you get the exception.
As a quick remedy, change the log4j.appender.FILE.File setting to point to file by absolute path, for example /tmp/mytest.log. You should not get an exception.
After that you can proceed to debugging why ${file.name} is not replaced correctly in your runtime environment.
I had the exact same problem. Here is the solution that worked for me: simply put your properties file path in the cmd line this way :
-Dlog4j.configuration=<FILE_PATH> (ex: log4j.properties)
Hope this will help you
i just add write permission to "logs" folder and it works for me
this error is coming because of appender file location you have provided is not reachable with current user access.
Quick Solution, change the log4j.appender.FILE.File setting to point to file using absolute path which location is reachable to the current user you have logged in, for example /tmp/myapp.log. Now You should not get an error.
if it is window7(like mine), without administrative rights cannot write a file at C: drive
just give another folder in log4j.properties file
Set the name of the file
log4j.appender.FILE.File=C:\server\log.out you can see with notepad++
This is your config :
log4j.appender.FILE.File=logs/${file.name}
And this error happened :
java.io.FileNotFoundException: logs (Access is denied)
So it seems that the variable file.name is not set, and java tries to write to the directory logs.
You can force the value of your variable ${file.name} calling maven with this option -D :
mvn clean test -Dfile.name=logfile.log
Have a look at the error -
'log4j:ERROR setFile(null,false) call failed.
java.io.FileNotFoundException: logs (Access is denied)'
It seems there's a log file named as 'logs' to which access is denied i.e it is not having sufficient permissions to write logs. Try by giving write permissions to the 'logs' log file. Hope it helps.
Please changes your log file location to another drive. it will work.
this happen's the permission of creating log file.
To prevent this isue I changed all values in log4j.properties file with directory ${kafka.logs.dir} to my own directory. For example D:/temp/...
Try executing your command with sudo(Super User), this worked for me :)
Run : $ sudo your_command
After than enter the super user password. Thats All..

Log4j messages containing (?:method:?) instead of class name

My log4j messages are not including the class names:
[INFO] 22:41 (?: decodeDirectory :?)
Any idea why that would be so?
log4j.properties reads like so:
log4j.rootLogger=INFO, logfile, console
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=ExifImageRA.log
log4j.appender.logfile.MaxFileSize=1MB
log4j.appender.logfile.MaxBackupIndex=1
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n
It looks like what is actually missing from your log messages are the source file name (%F) and the line number (%L).
According to the javac Ant task: the debug attribute "[i]ndicates whether source should be compiled with debug information; defaults to off. If set to off, -g:none will be passed on the command line ... ".
According to the javac manual, the source filename and line numbers are omitted from the .class file if you compile with javac -g:none.
Hence, your log message lossage is a direct consequence of removing debug="true" from the javac task in your Ant build file.

Categories