Why am i receiving the errors that I am getting? - java

I have this code below for a Log4j logger. I have to say that a lot of people have given me help on this on a different question. But I thought that since it was getting to a new question I would open one up.
My question is why am I getting these errors? Do I have everything configured correctly? How do I add an appender. I think that I am missing something somewhere but i am not certain as to what it is.
Code:
package TestMenu;
import org.apache.log4j.Logger;
import javax.swing.*;
public class TestLogs {
private static Logger logger = Logger.getLogger(TestLogs.class.getName());
public TestLogs() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
}
private void LogTester() {
logger.info("It works");
}
public static void main(String[] args) {
TestLogs tls = new TestLogs();
tls.LogTester();
}
}
Error:
log4j:WARN No appenders could be found for logger (TestMenu.TestLogs).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Properties File:
# Root logger
log4j.rootLogger=INFO, file, stdout
# Write to file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File="C:\Users\itpr13266\Desktop\test.log"
log4j.appender.file.MaxFileSize=50MB
log4j.appender.file.MaxBackupIndex=1
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
# Write 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

Log4j doesn't seem to find your configuration file. Have you tried running your application with a parameter like:
-Dlog4j.configuration=file:config/log4j.properties
So that Log4j finds your configuration file ("config/log4j.properties" using System Property log4j.configuration):
Alternatively, you can also put this configuration file in your classpath so that Log4j finds it, like in your source directory for example:
Else, here is what Log4j documentation explains or this error message:
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments. Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?

Related

Eclipse 2018-12 (4.10.0) Version not showing Spring Framework default logs in console?

I am using Eclipse IDE 2018-12 on Mac OS Mojave for project development, unfortunately unable to see default spring framework console logs in eclipse console, surprisingly i am able to see the other System.out.println statements used in project, Have anybody came across this issue or it's a bug with eclipse IDE 2018-12 ?
As per the above discussion, I provide below the way to configure your spring project. It is not maven or gradle type project. First of all you have to create a log4j.properties file and keep this file inside your resources folder. I provide below the sample log4j.properties file.
# 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
log4j.appender.file=org.apache.log4j.RollingFileAppender
#outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/myapp.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
Make Right click on the resource folder in eclipse and click Build Path > Use as Source Folder
See the image below.
Finally, I have modified your HelloClient.java
Use the following line for Log4j logger.
private static final Logger log = Logger.getLogger(HelloClient.class);
instead of the following line
private static Log log = LogFactory.getLog(HelloClient.class);
Also, import the following line.
import org.apache.log4j.Logger;
Comment out the following lines in the code.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Use this following line
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
in place of the following line
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("resources/applicationContext.xml");
Now run the class HelloClient.java in eclipse, you can see the number logs lines in console.

Unable to Set Logging Level and Output in Log4j

I am unable to set Root Logger Option through properties file in Log4j. Below is how my properties file looks -
I have saved this file as log4j2.properties in the root of src folder.
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to 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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I tried setting the rootLogger to ALL but, it doesn't work. However; if I set the Level to ALL through the method in my program it works. Example -
package com.nanwani;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class LoggerTest {
public static Logger log = Logger.getLogger(LoggerTest.class);
public static void main(String[] args) {
log.setLevel(Level.ALL);
}
}
In addition, it seems it is not referring to my properties file at all however, if I delete this file then it does show ERROR message that - using default properties file as log4j2 file not found.
I have found the answer to my question. Log4j2 does not support configuration properties file as opposed to Log4j v1. New formats are XML, JSON, and YAML, see the documentation
The file log4j2.properties must be named log4j.properties (if you are using Log4j version 1) . And you have to log something using log.debug("message"). What about considering moving the file to the src/main/resources folder?

Log4J: Warn No Appenders could be found for Logger error

Below is my log4j.properties file content, file is placed with the src folder in eclipse.
#Application Logs
log4j.rootlogger=INFO, logg
log4j.appender.logg=org.apache.log4j.RollingFileAppender
log4j.appender.logg.File=D:\\SandhyaFiles\\SeleniumWorkspace\\InterviewProject\\Logs\\Testlogs.log
log4j.appender.logg.layout=org.apache.log4j.PatternLayout
log4j.appender.logg.layout.ConversionPattern=%d -%c -%p - %m%n
log4j.appender.logg.maxFileSize=5MB
log4j.appender.logg.maxBackupIndex=3
Inside Library package i have initialised and used logj as below:
public class Library
{
public static final Logger Log = Logger.getLogger(Library.class);
public void initialized(){
Log.info("Inside initialise")
}}
calling initialize from testcase throws log4j warning:
log4j:WARN No appenders could be found for logger (library.Library).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Please Help me fix this.
It seems like log4j is not able to find the log4j properties file.
One could specify the location of the log4j.properties file explicitly via the log4j.configuration system property.
-Dlog4j.configuration=file:mylogging.properties
In case the system property log4j.configuration is not defined, then the resource is set to its default value log4j.properties and looked for in the classpath of the project (at root).
After so many trials, weirdly re arranging the properties file to this everything is going no warning and could get the logs.
# Root logger option
log4j.rootLogger=INFO, logg
log4j.appender.logg=org.apache.log4j.RollingFileAppender
log4j.appender.logg.File=.\\Logs\\Testlogs.log
log4j.appender.logg.MaxFileSize=5MB
log4j.appender.logg.MaxBackupIndex=3
log4j.appender.logg.layout=org.apache.log4j.PatternLayout
log4j.appender.logg.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I wonder why this is so? if somebody can explain it will be great help.
Thanks

Please initialize the log4j system properly. While running web service

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)));
}

How to configure log4j.properties for SpringJUnit4ClassRunner?

Suddenly this keeps happening during a JUnit test. Everything was working, I wrote some new tests and this error occured. If I revert it, it won't go away. Why is that?
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
The new tests you wrote (directly or indirectly) use classes that log using Log4j.
Log4J needs to be configured for this logging to work properly.
Put a log4j.properties (or log4j.xml) file in the root of your test classpath.
It should have some basic configuration such as
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# An alternative logging format:
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
An appender outputs to the console by default, but you can also explicitly set the target like this:
log4j.appender.A1.Target=System.out
This will redirect all output in a nice format to the console. More info can be found here in the Log4J manual,
Log4J Logging will then be properly configured and this warning will disappear.
If you don't want to bother with a file, you can do something like this in your code:
static
{
Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.INFO);
rootLogger.addAppender(new ConsoleAppender(
new PatternLayout("%-6r [%p] %c - %m%n")));
}
Add a log4j.properties(log4j.xml) file with at least one appender in root of your classpath.
The contents of the file(log4j.properties) can be as simple as
log4j.rootLogger=WARN,A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c %x - %m%n
This will enable log4j logging with default log level as WARN and use the java console to log the messages.
I have the log4j.properties configured properly. That's not the problem. After a while I discovered that the problem was in Eclipse IDE which had an old build in "cache" and didn't create a new one (Maven dependecy problem). I had to build the project manually and now it works.
I was using Maven in eclipse and I did not want to have an additional copy of the properties file in the root folder. You can do the following in eclipse:
Open run dialog (click the little arrow next to the play button and go to run configurations)
Go to the "classpath" tab
Select the "User Entries" and click the "Advanced" button on the right side.
Now select the "Add External folder" radio button.
Select the resources folder
I know this is old, but I was having trouble too. For Spring 3 using Maven and Eclipse, I needed to put the log4j.xml in src/test/resources for the Unit test to log properly. Placing in in the root of the test did not work for me. Hopefully this helps others.
Because I don't like to have duplicate files (log4j.properties in test and main), and I have quite many test classes, they each runwith SpringJUnit4ClassRunner class, so I have to customize it. This is what I useļ¼š
import java.io.FileNotFoundException;
import org.junit.runners.model.InitializationError;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Log4jConfigurer;
public class MySpringJUnit4ClassRunner extends SpringJUnit4ClassRunner {
static {
String log4jLocation = "classpath:log4j-oops.properties";
try {
Log4jConfigurer.initLogging(log4jLocation);
} catch (FileNotFoundException ex) {
System.err.println("Cannot Initialize log4j at location: " + log4jLocation);
}
}
public MySpringJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
}
When you use it, replace SpringJUnit4ClassRunner with MySpringJUnit4ClassRunner
#RunWith(MySpringJUnit4ClassRunner.class)
#ContextConfiguration("classpath:conf/applicationContext.xml")
public class TestOrderController {
private Logger LOG = LoggerFactory.getLogger(this.getClass());
private MockMvc mockMvc;
...
}

Categories

Resources