Logging a specific package in log4j programmatically - java

I have to deploy a web app and the the log4j.properties file is created by the client so I dont have control over it.
Their properties file is like this:
log4j.rootCategory= FILE
!-----------FILE--------------!
log4j.category.FILE=DEBUG
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${catalina.base}/logs/rcweb.log
log4j.appender.FILE.MaxFileSize=1024KB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-2d{dd/MM/yyyy HH:mm:ss} [%t] %5p %c:%L - %m%n
And in my classes I do something like this:
private static final Logger LOG = Logger.getLogger(MaterialController.class);
LOG.info("my log");
But the log file has never been created.
I did the test and changed the log4j.properties file and deployed it in my computer adding the following line:
log4j.logger.br.com.golive.requisicaoCompras=DEBUG
This works, but I can't use the file like this.
Are there any ideas?

Problem solved. My client's log4j.properties file was wrong, the first line should be:
log4j.rootCategory=DEBUG, FILE

Related

Set log4j output files for a web project: issue with destination folder

I am trying to set up log4j and I would like output files to be placed in my project, specifically in the src/main/resources/logs folder, as I am in the development phase and I prefer having it immediately visible in my IDE.
My issue is that I don't want to use absolute paths nor variables to pass as arguments when running the program (system or VM).
I have 2 questions:
1) How to set up log4j via properties to place the log files in the said folder?
2) Where to do you generally your log files?
My current log4j configuration:
log4j.debug=true
log4j.rootLogger=debug, stdout, FILE
log4j.appender.FILE = org.apache.log4j.FileAppender
log4j.appender.FILE.File = ${classpath}:\\logs\\log_1.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=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=application.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=3
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
1) src/main/resources will not exist after your project is built to a war. Files located in src/main/resource are packaged (by maven) into the WEB-INF/classes directory of your war file. src/main/resources is meant for project resource files (e.g., log4j.properties, ini config files) that need to be in the application class path. The application class path is generally not accessible through your operating system's file system.
2) Log files generally go into the log directory of your servlet container. For example, in tomcat they go in $CATALINA_HOME/logs. The next choice is the operating system log directory. For example in linux, /var/log/tomcat7

Log4j printing to console every time

I have placed following log4j.properties file in src folder of my eclipse dynamic web project. But everytime logs are getting printed on console though I am writing code to print the logs in file only. If I am deleting log4j.properties file then also it is getting printed on console.Don't know what went wrong.
###############################################################################
log4j.rootLogger=INFO, file
###############################################################################
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/apache-tomcat-7.0.47/webapps/LogFile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p %c - %m%n
###############################################################################
I am using following code to print the logs.
import org.apache.log4j.Logger;
Logger log = Logger.getLogger(Client.class.getName());
log.info("First log");
Try to override the log4j configuration in your class like this:
import org.apache.log4j.PropertyConfigurator;;
static{
PropertyConfigurator.configure("src/main/resources/log4j.properties");
}
This assumes you have a file called log4j.properties under src/main/resources
Thanks Nikhil
I have used follwing code by looking your code and it worked fine.
import org.apache.log4j.PropertyConfigurator;
PropertyConfigurator.configure(getClass().getClassLoader().getResourceAsStream("log4j.properties"));

Log4j logfile error FileNotFoundException

I am trying to write a log4j logfile using Netbeans Java. I succeded writing the log file to C:\tmp\log.txt , but for some reason i have no premission to write it at the root of my project : log.txt.
This is my log4j.properties:
log4j.rootLogger = all, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=log.txt <- failes
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%5p](%F:%L) %m%n
log4j.appender.FILE.Append=false
Have you try this :
log4j.appender.FILE.File=./log.txt
. represents the current folder (usually the project root folder). Maybe the log file that you set are placed somewhere in root of your drive.
This reference may useful for you :
Where Are Your Log4j Log Files?

Change the properties file

I have properties file as below
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.logger.com.foo=WARN
I want to change the log level in this file so i write below line in java file
logger.setLevel(Level.FATAL);
this will only change my log level at run time what if i want to change the log level permanently in properties file?
Use the java.util.Properties class. The class allows to load properties from a file, change it, then save to a file.

Need to create separate log file for jar file?

I am creating a jar file that i need to provide to client, but my client is asking for separate logger for the jar file as this is doing some integration work.
anyone can suggest how can i create logger for only one jar file, can i put log4j.properties in same jar file.
I am using web-logic server. we will not deploy this jar file instead we will just keep it in domain lib folder.
Thanks
If the properties file is in the jar, then you could do something like this:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/log4j.properties"));
PropertyConfigurator.configure(props);
The above assumes that the log4j.properties is in the root folder of the jar file.
If this does not work for your needs in this case then you can always use:
-Dlog4j.configuration=log4j_for_some_jar.properties
If the other application is using Log4j as well. The easier method would be to just config your log4j file to send anything from your classes in your jar to a new log file like so:
log4j.rootLogger=ERROR, logfile
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.datePattern='-'dd'.log'
log4j.appender.logfile.File=log/radius-prod.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n
log4j.logger.foo.bar.Baz=DEBUG, myappender
log4j.additivity.foo.bar.Baz=false
log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.datePattern='-'dd'.log'
log4j.appender.myappender.File=log/access-ext-dmz-prod.log
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n

Categories