I have a requirement wherein I need to put log4j.properties file inside a folder named 'resources' under src directory.
log4j.properties
log = D:\\XYZ
log4j.rootLogger=INFO, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
resources.log4j.appender.FILE.File=${log}/logger.txt
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
but my when I navigate through my application nothing gets logged into logger.txt file..I have explicitly not given any configuration for log4j.properties file...
Please point my mistake and suggest solution..I am new to this concept..
Thanks
Notice that log4j is using a system property in the configuration, ${log} this needs to be defined when the VM is started. Essentially this is a variable piece of the configuration that is defined by a configuration parameter passed to the application.
java -Dlog=C:\SomeDirectory\ theApp
If your using Eclipse system properties can be set via the Run Configuration for your application.
Related
This is the log file for my log4j
log4j.rootLogger=DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.file=/Applications/apache-tomcat-8.0.24/logs/myLog.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.Encoding=UTF-8
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:%L - %m%n
As you see, i specify the location of the log file like:
Application/bla bla bla
That works good when I deploy on Mac, but sometimes, I deploy on windows, so I have to change that location to
C:/bla bla bla
is there any way to make the path relative? I would like to create my custom log file always in the logs folder of apache on the server regardless of the operating system
Thanks
When you deploy a web app into tomcat the servlet code runs in the JVM instance of tomcat. So all relative paths are relative to the current working directory (CWD) of the tomcat process.
The "standard" CWD is %CATALINA_HOME%\bin. To write into tomcat's log folder you logfile prefix should be ../logs/ like ../logs/myapp.
If you use maven, you can do:
resource filtering
I have a main class to test some of the functions, I am getting error:
WARN No appenders could be found for logger (Jena).
I searched online, and I found this answer to solve the problem
Configure Eclipse for Log4j
One of the answers says that I should create a file called log4j.properties in the bin folder. However, when I do that, I get this problem:
Error: Could not find or load main class com.blablabla.Main
If I remove that file from the bin folder, i go back to the first problem.
do likewise,
put your log4j.properties file into resource directory(if you are using Eclipse IDE then otherwise put it where it load to your Runtime Environment)
and into your log4j.properties file set appender properly,
For Example : log4j.properties will look like after completely configure,
log4j.rootLogger=DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.file=${path}/logFile2.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.Encoding=UTF-8
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:%L - %m%n
I'm trying to configure logging in my app where log4j.properties is put to /resources folder an has the following content:
#log level
log4j.rootLogger=ERROR, file
log4j
#
log4j.appender.file=org.apache.log4j.RollingFileAppender
#
log4j.appender.file.File=D:\agent\temp\panel log.log
#
log4j.appender.file.MaxFileSize=1MB
#
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
and on my app start I see the following in the console:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
what does this mean and how to fix it?
I had the same problem, I was using tomcat 7.
I solved the problem by adding conf path to the /bin/setenv.sh (if you are on linux, create the file if it does not exist) like this:
export JAVA_OPTS="$JAVA_OPTS -Dconfig.location=file -Dlog4j.configuration=file:///opt/apache-tomcat-7.0.61/lib/log4j.properties"
if you are on windows edit or create setenv.bat and write something like this (not sure about the path format):
set JAVA_OPTS=%JAVA_OPTS%-Dconfig.location=file -Dlog4j.configuration=file://c:/opt/apache-tomcat-7.0.61/lib/log4j.properties
Forgot to say: this will affect all the apps on the server
Try putting the log4j.properties in the root of your source folder. Or alternatively add resources to your classpath.
The root problem is log4j not seeing your config file, so you need to make it accessible.
Extract from log4j manual:
Default Initialization under Tomcat
The default log4j initialization is particularly useful in web-server
environments. Under Tomcat 3.x and 4.x, you should place the
log4j.properties under the WEB-INF/classes directory of your
web-applications. Log4j will find the properties file and initialize
itself. This is easy to do and it works.
You can also choose to set the system property log4j.configuration
before starting Tomcat. For Tomcat 3.x The TOMCAT_OPTS environment
variable is used to set command line options. For Tomcat 4.0, set the
CATALINA_OPTS environment variable instead of TOMCAT_OPTS.
i've changed log4j.properties to log4j.xml and put it to resources folder and now everything is correct. maybe my log4j version doesn't supprot .properties format
This question already has answers here:
Log4j Warning while initializing? [duplicate]
(5 answers)
Closed 8 years ago.
First up - yes, I have read the multiple questions & answers on this topic, and can't get any of the solutions within those to help me. I'm not running Tomcat or JBoss and I don't have a web.xml file to change. I'm using Java 6.0 and log4j-1.2.8.jar.
I am creating a runnable jar file with IDEA IntelliJ with jar libraries packaged separately and linked via the manifest. I am running my code on a Linux server thus:
me#server:/mydir> java -jar code/myjar.jar
log4j:WARN No appenders could be found for logger (FactoredEventsForTrna).
log4j:WARN Please initialize the log4j system properly.
My log4j configuration file (which I've placed both in mydir and mydir/code, just in case) is:
## Logger configure file for myproject
log.dir=log/
datestamp=yyyy-MM-dd/HH:mm:ss.SSS
log4j.rootLogger=TRACE, file, proappender, console
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=1GB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=log/mydebug.log
log4j.appender.file.threshold=TRACE
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{${datestamp}} %5p: %c{2} - %m%n
log4j.appender.proappender=org.apache.log4j.RollingFileAppender
log4j.appender.proappender.maxFileSize=5GB
log4j.appender.proappender.Threshold=INFO
log4j.appender.proappender.File=log/myinfo.log
log4j.appender.proappender.layout=org.apache.log4j.PatternLayout
log4j.appender.proappender.layout.ConversionPattern=%d{${datestamp}} %5p: %c{2} - %m%n
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{${datestamp}} %5p: %c{2} - %m%n
And I have created the log/ directory in mydir and mydir/code, again, just in case.
Any ideas?
There are many possible options for specifying your log4j configuration. One is for the file to be named exactly "log4j.properties" and be in your classpath. Another is to name it however you want and add a System property to the command line when you start Java, like this:
-Dlog4j.configuration=file:///path/to/your/log4j.properties
All of them are outlined here http://logging.apache.org/log4j/1.2/manual.html#defaultInit
Solution
Download log4j.jar file
Add the log4j.jar file to build path
Call logger by:
private static org.apache.log4j.Logger log
= Logger.getLogger(<class-where-this-is-used>.class);
if log4j properties does not exist, create new file log4j.properties file new file in bin directory:
/workspace/projectdirectory/bin/
Sample log4j.properties file
log4j.rootLogger=debug, 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=%t %-5p %c{2} - %m%n
I had moved my log4j.properties into the resources folder and it worked fine for me !
Man, I had the issue in one of my eclipse projects, amazingly the issue was the order of the jars in my .project file. believe it or not!
You will find de log4j.properties in solr/examples/resources.
If you don't find the file, i put it here:
# Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9
#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n
log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN
# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
Regards
Move Your log4j.properties file into the src folder.
Open Windows explorer, browse to the directory where your project resides in.
Browse to bin directory and delete all the folders with in it.
Now in eclipse click project---->clean---->OK
This would force it to build the project again,all the contents delete from bin directory will be recreated.
I took while to figure this out.
At times directly clicking clean doesn't work
put the folder which has the properties file for log in java build path source. You can add it by right clicking the project ----> build path -----> configure build path ------> add t
Maybe it looks silly to ask this but I am confused. I referred to Configuring Log4j property but it doesn't seem to help.
I have written a simple web service HelloWorld. And while running it I am getting the error something like this :
log4j:WARN No appenders could be found for logger (org.apache.axis.transport.http.AxisServlet).
log4j:WARN Please initialize the log4j system properly.
I am not sure why its happening.
I am generating the web-service using Eclipse and deployed in Tomcat 6.0. I check on Axis Developer's Guide and according to them
log4j.configuration=log4j.properties
Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties. A log4j.properties file is provided in axis.jar.
I didn't find log4j.properties in the axis.jar.
Any help on this?
Those messages are something tricky, enough so that people created this to make it clearer:
https://issues.apache.org/bugzilla/show_bug.cgi?id=25747
What's tricky about them is that the warnings are written if Log4j can't find its log4j.properties (or log4j.xml) file, but also if the file is fine and dandy but its content is not complete from a configuration point of view.
The following paragraph is taken from here:
http://svn.apache.org/repos/asf/logging/log4j/tags/v1_2_9/docs/TROUBLESHOOT.html
Logging output is written to a target by using an appender. If no appenders are attached to a category nor to any of its ancestors, you will get the following message when trying to log:
log4j: No appenders could be found for category (some.category.name).
log4j: Please initialize the log4j system properly.
Log4j does not have a default logging target. It is the user's responsibility to ensure that all categories can inherit an appender. This can be easily achieved by attaching an appender to the root category.
You can find info on how to configure the root logger (log4j.rootLogger) in the log4j documentation, basically adding something as simple as this at the beginning of the file:
log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
This should clear those WARN messages you get on startup (make sure you don't already have an appender named stdout; also be carefull of what level you give the root logger, debug will be very verbose and every library in your app will start writing stuff to the console).
As about the log4j.properties/log4j.xml, I suggest you place this file in /WEB-INF/classes as it is important to have it exposed for different tweaks (activating/deactivating logs, changing log levels etc). You can have it inside a JAR in the classpath also (as you specified in your comment), but it will be enclosed in the archive (hopefully in the right place inside the archive) and won't be as easy to handle as if it were in /WEB-INF/classes.
You have to create your own log4j.properties in the classpath folder.
Well, if you had already created the log4j.properties you would add its path to the classpath so it would be found during execution. Yes, the thingy will search for this file in the classpath.
Since you said you looked into axis and didnt find one, I am assuming you dont have a log4j.properties, so here's a crude but complete example.
Create it somewhere and add to your classpath. Put it for example, in c:/proj/resources/log4j.properties
In your classpath you simple add .......;c:/proj/resources
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=c:/project/resources/t-output/log4j-application.log
log4j.appender.file.MaxFileSize=5MB
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
If the below statment is present in your class then your log4j.properties should be in java source(src) folder , if it is jar executable it should be packed in jar not a seperate file.
static Logger log = Logger.getLogger(MyClass.class);
Thanks,
Warning No appenders could be found for logger means that you're using log4j logging system, but you haven't added any Appenders (such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, etc.) into your configuration file or the configuration file is missing.
There are three ways to configure log4j: with a properties file (log4j.properties), with an XML file and through Java code (rootLogger.addAppender(new NullAppender());).
If you've property file present (e.g. when installing Solr), you need to place this file within your classpath directory.
classpath
Here are some command suggestions how to determine your classpath value:
$ echo $CLASSPATH
$ ps wuax | grep -i classpath
$ grep -Ri classpath /etc/tomcat? /var/lib/tomcat?/conf
or from Java: System.getProperty("java.class.path").
Tomcat
If you're using Tomcat, you may place your log4j.properties into: /usr/share/tomcat?/lib/ or /var/lib/tomcat?/webapps/*/WEB-INF/lib/ folder.
Solr
For the reference, Solr log4j.properties looks like:
# Logging level
solr.log=logs/
log4j.rootLogger=INFO, file, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
#- size rotation with log cleanup.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=4MB
log4j.appender.file.MaxBackupIndex=9
#- File to log to and log format
log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n
log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN
# set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
If you are using Logger.getLogger(ClassName.class) then place your log4j.properties file in your class path:
yourproject/javaresoures/src/log4j.properties (Put inside src folder)
You can configure log4j.properties like above answers, or use org.apache.log4j.BasicConfigurator
public class FooImpl implements Foo {
private static final Logger LOGGER = Logger.getLogger(FooBar.class);
public Object createObject() {
BasicConfigurator.configure();
LOGGER.info("something");
return new Object();
}
}
So under the table, configure do:
configure() {
Logger root = Logger.getRootLogger();
root.addAppender(new ConsoleAppender(
new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
}