I'm trying to use log4j in my Java application (no web-app). Therefore I added the configuration file directly within my /src folder.
log4j.properties
log4j.rootLogger=DEBUG
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
My main class is in a package with a sub-namespace and within there I'm trying to log as follows:
final static Logger logger = Logger.getLogger(MainClass.class);
public static void main(String[] args) {
//BasicConfigurator.configure();
logger.warn("some warning");
}
But still I'm getting an error message and I don't know what to do. You see the commented line BasicConfigurator above. As far as I know, the log4j.properties should be read without calling that line, right?
log4j:WARN No appenders could be found for logger
(at.company.project.poi.MainClass). log4j:WARN Please initialize the
log4j system properly. log4j:WARN See
http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
According to documentation, file should be on classpath and named 'log4j2.properties'
You can also set directly the configuration by setting -Dlog4j.configurationFile="<your config filepath>"
Source: https://logging.apache.org/log4j/2.x/manual/configuration.html
Related
Always I try to use a formal and structurized log api is a nightmare (this explains why so many people just print to console)
I'm trying to use log4j in my project
to instantiate the logger I do:
private static final Logger log = LoggerFactory.getLogger(Instagram.class.getPackage().getName());
when I want to register an event I do:
log.info("some information");
I've a log4j.properties file in the src folder like:
log4j.logger.com.tomatechines.instagramapi.api = INFO, CONSOLE, FILE, ERROR
log4j.rootLogger = INFO, FILE, ERROR
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern = yyyy-MM-dd'.log'
log4j.appender.FILE.File = logs/log-
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
log4j.appender.ERROR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.DatePattern = yyyy-MM-dd'.log'
log4j.appender.ERROR.File = logs/errorlog-
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
log4j.appender.ERROR.Threshold=WARN
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} %-5p %C{1}:%L - %m%n
but when the code runs the only thing printed to console or file is:
log4j:WARN No appenders could be found for logger (com.tomatechines.instagramapi.api).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
why it doesn't find my file, I set 3 appenders and why can't it find just one
First of all, I can see that you are using SLF4J with log4j 1.2.x, because you are instantiating the logger with LoggerFactory.getLogger. If you want to use only log4j 1.2.x you should get the logger with Logger.getLogger. Making sure that you have imported the classes from org.apache.log4j package.
You must also make sure that you have put the log4j.properties file under the classpath. And in my opinion you should try first with a very simple configuration, to avoid configuration problems. Something like:
# 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
Probably your problem is about the location of log4j.properties file.
I have to perform basic logging on my application for which I am using Log4j. I have set up log.properties file inside resource folder with desired configuration but am getting the following warning when I try to log information
log4j:WARN No appenders could be found for logger (com.MyClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
This is my property file located under resource folder :
# Root logger option
log4j.rootLogger=DEBUG, ERROR, 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=./resources/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
This is what my project structure looks like :
src
-> main
-> java
....
-> resources
--> log.properties
Every folder is present in java build path of the project as well.
This is how am calling just to test:
public class Demo {
final static Logger logger = Logger.getLogger(Demo.class);
public static void main(String[] args) {
logger.debug("demo");
}
}
Can u try to rename the file to log4j.properties and also
log4j.rootLogger=DEBUG,ERROR stdout, file
line should have either debug or error not both
I'm using log4j for my final project. I tested it using single class before I used log4j in my project. That worked fine.
Then I added log4j to my project and I used it in many classes using:
final static Logger logger = Logger.getLogger(Supplier.class);
Then I got a warning:
log4j:WARN No appenders could be found for logger (classes.Supplier).
log4j:WARN Please initialize the log4j system properly.
This is my properties file:
# Root logger option
log4j.rootLogger=DEBUG, file
log4j.rootLogger=DEBUG, errorfile
# Redirect log messages to console
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.errorfile=org.apache.log4j.RollingFileAppender
# Redirect log messages to a log file, support file rolling.
log4j.appender.file.File=C:\\HardwareLog\\INFO.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
log4j.appender.errorfile=C:\\HardwareLog\\ERROR.log
log4j.appender.errorfile.Threshhold=ERROR
log4j.appender.errorfile.MaxFileSize=5MB
log4j.appender.errorfile.MaxBackupIndex=10
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Do I have to create appenders for each class to use it?
Can I use one properties file in different packages ?
I found a solution. I removed root logger and added two loggers for my two packages.
log4j.logger.package1=DEBUG,FIRST
log4j.logger.package2=DEBUG,SECOND
now its working fine.
you can go for root logger that will log for the entire application.
# Root logger option
log4j.rootLogger=INFO, file
plese go through this link
https://www.mkyong.com/logging/log4j-log4j-properties-examples/
Whenever I ran my webdriver script in eclipse, using java, I got following warning:
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
When I looked into Referenced Libraries I saw a 'log4j-1.2.14.jar'. This must have come along with default selenium download. What's its use??
Log4j configuration is missing. Log4j scans for configuration files log4j.xml or log4j.properties on the classpath and cannot find either.
For details, have a look at No appenders could be found for logger(log4j)? .
Example:
To setup a console appender, create file log4j.properties with the following contents and put it on the classpath:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p %c - %m%n
I am stuck. I have a configuration issue in setting up log4j with Spring MVC. Getting the below error.
log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Here is my controller code
public class HelloWorldController {
private static Logger logger = Logger.getLogger(HelloWorldController.class);
int first_no=10;
int second_no=10;
int summation;
#RequestMapping("/helloworld")
public String helloWord(ModelMap modelview){
summation=first_no+second_no;
System.out.println("Inside hello world");
logger.info("Hello, World!");
modelview.addAttribute("message", summation);
return "helloworld";
}
}
Here is my log4j.properties file
# 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{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=debug, file, stdout
I have kept the file under src/resources in eclipse. Can anyone help me out with this? I am stuck.
Probably log4j.properties file is not in the classpath.Move the log4j.properties in src folder in Eclipse and under WEB-INF/classes when deployed in the Container.