EzDevInfo.com

log4j interview questions

Top log4j frequently asked interview questions

Setting log level of message at runtime in slf4j

When using log4j, the Logger.log(Priority p, Object message) method is available and can be used to log a message at a log level determined at runtime. We're using this fact and this tip to redirect stderr to a logger at a specific log level.

slf4j doesn't have a generic log() method that I can find. Does that mean there's no way to implement the above?


Source: (StackOverflow)

Set System Property With Spring Configuration File

Configuration:
Spring 2.5, Junit 4, Log4j
The log4j file location is specified from a system property

${log.location}

At runtime, system property set with -D java option. All is well.

Problem / What I Need:
At unit test time, system property not set, and file location not resolved.
App uses Spring, would like to simply configure Spring to set the system property.

More Info:
Requirement is for configuration only. Can't introduce new Java code, or entries into IDE. Ideally, one of Spring's property configuration implementations could handle this--I just haven't been able to find the right combination.

This idea is close, but needs to add Java code:
Spring SystemPropertyInitializingBean

Any help out there? Any ideas are appreciated.


Source: (StackOverflow)

Advertisements

log4j: Log output of a specific class to a specific appender

I use log4j and would like to route the output of certain Loggers to specific files.

I already have multiple appenders in place. Now, to make debugging easier, I want to tell log4j that the output generated by a specific class (e.g. foo.bar.Baz) should be written to a specific log file.

Can this be done?


Source: (StackOverflow)

Java Logging vs Log4J [closed]

Is it still worth to add the log4j library to a Java 5 project just to log let's say some exceptions to a file with some nice rollover settings. Or will the standard util.logging facility do the job as well?

What do you think?


Source: (StackOverflow)

Log4J: Strategies for creating Logger instances

I decided to use Log4J logging framework for a new Java project. I am wondering what strategy should I use for creating/managing Logger instances and why?

  • one instance of Logger per class e.g.

    class Foo {
        private static final Logger log = Logger.getLogger(Foo.class);
    }
    
  • one instance of Logger per thread
  • one instance of Logger per application
  • horizontal slicing : one instance of Logger in each layer of an application (e.g. the view layer, the controller layer and the persistence layer)
  • vertical slicing : one instance of Logger within functional partitions of the application

Note: This issue is already considered to some extent in these articles:

The ultimate way to gettting [sic] Log4J loggers

Whats the overhead of creating a Log4j Logger


Source: (StackOverflow)

log4j:WARN No appenders could be found for logger in web.xml

I already put the log4jConfigLocation in web.xml, but I still get the following warning:

log4j:WARN No appenders could be found for logger ⤦
    ⤥ (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.

What did I miss?

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.properties</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>suara2</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>suara2</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Source: (StackOverflow)

java convert milliseconds to time format

I am trying to convert a long value (The number of milliseconds elapsed from 1/1/1970) to a time of format h:m:s:ms
The long value I use as timestamp, I get from the field timestamp of logging event from log4j.
How do I do the conversion?
For example to get the minutes I tried the following and all fail:
logEvent.timeStamp/ (1000*60*60) and
TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)
but I get garbage:
I get

1289375173771 for logEvent.timeStamp
358159  for logEvent.timeStamp/ (1000*60*60) 
21489586 for TimeUnit.MILLISECONDS.toMinutes(logEvent.timeStamp)

How can I convert this?

Thanks


Source: (StackOverflow)

Where do I configure log4j in a JUnit test class?

Looking at the last JUnit test case I wrote, I called log4j's BasicConfigurator.configure() method inside the class constructor. That worked fine for running just that single class from Eclipse's "run as JUnit test case" command. But I realize it's incorrect: I'm pretty sure our main test suite runs all of these classes from one process, and therefore log4j configuration should be happening higher up somewhere.

But I still need to run a test case by itself some times, in which case I want log4j configured. Where should I put the configuration call so that it gets run when the test case runs standalone, but not when the test case is run as part of a larger suite?


Source: (StackOverflow)

Java Log Viewer

Unfortunately, sometimes the only way to debug a program is by going through its long log files.

I searched for a decent log viewer for a while now, and haven't found a real solution. The only program that seemed to be most appropriate was Chainsaw with its Socket connector but after a few short uses the program proved to be buggy and unresponsive at best.

For my purposes, a log viewer should at least be able to mark log levels (for example with different colors) and perform easy filtering based on packages and free-text.

Is there any other (free) log viewer? I'm looking for anything that could work well with log4j.

Thanks in advance!


Source: (StackOverflow)

Setting a log file name to include current date in Log4j

I would like to set the log file name for a log4j and log4net appender to have the current date. We are doing Daily rollovers but the current log file does not have a date. The log file name format would be

logname.2008-10-10.log

Anyone know the best way for me to do this?

edit: I forgot to mention that we would want to do this in log4net as well. Plus any solution would need to be usable in JBoss.


Source: (StackOverflow)

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.

Source: (StackOverflow)

Configuring Hibernate logging using Log4j XML config file?

I haven't been able to find any documentation on how to configure Hibernate's logging using the XML style configuration file for Log4j.

Is this even possible or do I have use a properties style configuration file to control Hibernate's logging?

If anyone has any information or links to documentation it would appreciated.

EDIT:
Just to clarify, I am looking for an example of the actual XML syntax to control Hibernate.

EDIT2:
Here is what I have in my XML config file.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="info"/>
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>
        </layout>
    </appender>
    <appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="Program-Name.log"/>
        <param name="MaxFileSize" value="1000KB"/>
    <!-- Keep one backup file -->
        <param name="MaxBackupIndex" value="4"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
        </layout>
    </appender>

    <root>
        <priority value ="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="rolling-file" />
    </root>
</log4j:configuration>

Logging works fine but I am looking for a way to step down and control the hibernate logging in way that separate from my application level logging, as it currently is flooding my logs. I have found examples of using the preference file to do this, I was just wondering how I can do this in a XML file.


Source: (StackOverflow)

log4j redirect stdout to DailyRollingFileAppender

I have a java app that uses log4j.

Config:

log4j.rootLogger=info, file

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${user.home}/logs/app.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %c %p %m%n

So all the log statements are correctly appended to the file, but i am losing stdout and stderr. How do i redirect exception stack traces and sysouts to the daily rolled file ?


Source: (StackOverflow)

Disabling Log4J Output in Java

How can one quickly turn off all Log4J output using a log4j.properties file?


Source: (StackOverflow)

Dynamically Changing log4j log level

What are the different approaches for changing the log4j log level dynamically, so that I will not have to redeploy the application. Will the changes be permanent in those cases?


Source: (StackOverflow)