How to see log4J messages in console - java

I have an application in NetBeans where I am trying to use debug messages to log method calls, e.g.:
public class PersonService {
protected static Logger logger = Logger.getLogger(PersonService.class.getName());
public void add(Person person) {
logger.debug("PersonService.add called");
...
}
My log4j configuration file is in the WEB-INF folder of the project as below:
log4j.rootLogger=DEBUG,console
#Console Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%5p] [%t %d{hh:mm:ss}] (%F:%M:%L) %m%n
I understand that this should print debug messages to the console but so far I get nothing.
I am also receiving this in the output:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
Any ideas?

Put log4j.properties file into WEB-INF/classes, you should keep it in classpath.
Another (more correct way) is to keep all your .properties file in separate properties folder, which content automatically moved to WEB-INF/classes during web archive creation. In Eclipse this kind of folders called source folders on build path.

Related

Log4J Properties not found / no effect

I've got a Web application with Tomcat and would like to add Log4J2. Logging to the console by default works just fine. Although, I've added a log4j2.properties to add a File appender to my project and write the log to a dedicated location. Unfortunately, it seems that my properties-File has no effect.
Path to properties-File: /WEB-INF/log4j2.properties
Configs of log4j2.properties
name=Log4j2PropertiesConfig
appenders=file
appender.file.type=File
appender.file.name=FileLogger
appender.file.filename=logs/app.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=%d [%t] %-5p %c - %m%n
rootLogger.level=all
rootLogger.appenderRefs=file
rootLogger.appenderRef.file.ref=FileLogger
How I create the logger inside a class:
private val logger: Logger = LoggerFactory.getLogger(this.javaClass)
What am I doing wrong?
try to put the file log4j2.properties into src/main/resources. It should allocate in classpath
https://www.codejava.net/coding/how-to-initialize-log4j-for-java-web-application
The tutorial solves my problem as it defines a way on how to keep the properties in the WEB-INF.

hadoop container stdout is always empty

Why is stdout file of a job container in hadoop is always of size 0.
On java
import org.apache.log4j.Logger;
static final Logger MAPLOGGER= Logger.getLogger(MyMap.class.getName());
MAPLOGGER.warn("key is :"+key);
after running jar, 3 files are generated
stderr, stdout,syslog
stderr contains
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.impl.MetricsSystemImpl).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
where exactly MAPLOGGER.warn("key is :"+key); writes the log?.
It doesn't write it anywhere right now, because you have not set up log4j.
From the link in the WARN message:
"Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?
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."
So you need to set up the log4j engine, and give it to Java overall, or explicitly declare their location in the application.

restrict logs using log4j

I'm using log4j for logging, and it is the first time I'm working on it.
I want to log particular lines from the code, say for exmple
log.debug("this is my an example");
Only want these lines from my project in my log file. Is it possible using log4j?
Here is my log4j.properties
log4j.rootLogger=DEBUG, CA
log4j.appender.CA =org.apache.log4j.RollingFileAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.File=myLocation/logs.log
log4j.appender.CA.ImmediateFlush=true
log4j.appender.CA.Append=false
log4j.appender.CA.layout.ConversionPattern= %d{ABSOLUTE} %5p %c - %m%n
aslo can we route the logging to separate file for each run?
Set loglevel in your code as below and make it as a static block or something where it should load at the startup
private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);
log.setLevel(Level.Debug);
and for specific package restrictions of log, you can do like below
Suppose you have a package a.b.c
To specify logging level for this package as debug, add the below line in your log4j.properties
log4j.logger.a.b.c=debug

log4j conflicting?

I have a htmlunit application and I turned off its logging by the following lines since it was printing way too many messages in the console which I don't need.
LogFactory.getFactory().setAttribute(
"org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit")
.setLevel(Level.OFF);
java.util.logging.Logger.getLogger("org.apache.commons.httpclient")
.setLevel(Level.OFF);
I then added the latest log4j jar to my project and configured it so I can have my own custom logging messages. But now everything from htmlunit also goes to my log file. How can I prevent this? I only want my own logging messages to go to the log file.
One important thing to understand about log4j is its concept of a 'logger hierarchy', and how this affects its settings. To quote the log4j documentation,
A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger
is said to be a parent of a child logger if there are no ancestors
between itself and the descendant logger.
For example, the logger named "com.foo" is a parent of the logger
named "com.foo.Bar". Similarly, "java" is a parent of "java.util" and
an ancestor of "java.util.Vector". This naming scheme should be
familiar to most developers.
A logger inherits the settings of its parent, and the 'root logger' is always at the top of the hierarchy. Therefore to some extent you can treat the root logger as the 'default' logger settings, and you can then add to these settings for just the loggers contained in your code.
I don't know how to set this up in code, but here is what I would do using a log4j.properties file:
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/springapp.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{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger settings: by default only log messages at level "ERROR" and above
# will be logged. These messages will be written to console
log4j.rootLogger=error, console
# All loggers with names starting with "org.somecompany.someproject"
# will log messages at all levels "DEBUG" and above.
# They will be written to file as well as console; writing to console
# is enabled since that setting is inherited from the root logger.
log4j.logger.org.somecompany.someproject=debug, file
Then, assuming that the logger of each class is named as the fully qualified name of that class, then simply replace "org.somecompany.someproject" in the above config with the root package name for the packages in your project, and that should give you the behaviour you want.
For more information, have a look at the log4j documentation.

log4j warning issue - apache commons

I'm using apache commons library and log4j.
I have an xml configuration file and a log4j.properties files. I want to specify my log4j properties path inside my xml configuration file.
To load my settings i do:
//Loading my xml file
this.config = new XMLConfiguration(this.xmlFileName);
At this moment the following warnings are raised:
log4j:WARN No appenders could be found for logger (org.apache.commons.configuration.ConfigurationUtils).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
However i haven't yet called any log4j object. Once i have read the xml file i can successfully work with my log4j instance.
Is there any way to remove those warnings?
Check if the log4j.properties file is in the classpath
This link might be useful:
http://www.coderanch.com/t/63230/open-source/log-log-WARN-No-appenders
Log4J outputs this error when a logger is created, but no appender(s) is(are) defined.
In practice this error occurs when a logger is created before log4j is initialized.
You say you haven't called any log4j object. But in the error message you see that org.apache.commons.configuration.ConfigurationUtils creates a logger object (see line 66).
You could turn it off before initialization, see How to turn off log4j warnings?
Logger.getRootLogger().setLevel(Level.OFF);
There should be no need to turn it on again since the initialization sets normally the log level of the root logger.
You should at least set the appender and the logger level for the root logger in the loaded log4j configuration file. Otherwise , you will see this warning message.
Example of setting the appender and logger level for the root logger:
#Set root logger 's level and its appender to an appender called CONSOLE which is defined below.
log4j.rootLogger=DEBUG, CONSOLE
#Set the behavior of the CONSOLE appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
I resolve my issue with this workaround:
//Disable log level
Logger.getRootLogger().setLevel(Level.OFF);
Now i can read my xml configuration file without WARNINGS.
After set log level:
Logger.getRootLogger().setLevel(Level.INFO);

Categories