Asked  6 Months ago    Answers:  5   Viewed   43 times

After adding log4j to my application I get the following output every time I execute my application:

log4j:WARN No appenders could be found for logger (slideselector.facedata.FaceDataParser).
log4j:WARN Please initialize the log4j system properly.

It seems this means a configuration file is missing. Where should this config file be located and what is a good start content?

I'm using plain java for developing a desktop application. So no webserver etc...



Log4j by default looks for a file called or log4j.xml on the classpath.

You can control which file it uses to initialize itself by setting system properties as described here (Look for the "Default Initialization Procedure" section).

For example:

java -Dlog4j.configuration=customName ....

Will cause log4j to look for a file called customName on the classpath.

If you are having problems I find it helpful to turn on the log4j.debug:


It will print to System.out lots of helpful information about which file it used to initialize itself, which loggers / appenders got configured and how etc.

The configuration file can be a java properties file or an xml file. Here is a sample of the properties file format taken from the log4j intro documentation page:

log4j.rootLogger=debug, stdout, R


# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n


# Keep one backup file

log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
Tuesday, June 1, 2021
answered 6 Months ago

An alternative to using the built-in lookups is to create a custom lookup. This can be accomplished in a few lines of code with a log4j2 plugin. Your custom lookup then provides the exact value you want to show in the file header at each roll over.

The plugin code would look something like this:

package com.mycompany;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.AbstractLookup;
import org.apache.logging.log4j.core.lookup.StrLookup;

 * Looks up keys from a class SomeClass which has access to all
 * information you want to provide in the log file header at rollover.
@Plugin(name = "setu", category = StrLookup.CATEGORY)
public class SetuLookup extends AbstractLookup {

     * Looks up the value of the specified key by invoking a
     * static method on SomeClass.
     * @param event The current LogEvent (ignored by this StrLookup).
     * @param key  the key to be looked up, may be null
     * @return The value of the specified key.
    public String lookup(final LogEvent event, final String key) {
        return com.mycompany.SomeClass.getValue(key);

Then, in your configuration you can use the header of the pattern layout to output this at every rollover:

<RollingFile name="RollingFile" fileName="logs/app.log"

  <!-- use custom lookups to access arbitrary internal system info -->
  <PatternLayout header="${setu:key1} ${setu:key2}">
    <Pattern>%d %m%n</Pattern>
    <TimeBasedTriggeringPolicy />

The log4j2 manual has details on building/deploying custom plugins. Brief summary:

The easiest way is to build your jar with Maven; this will cause the log4j2 annotation processor to produce a binary index file in the jar so your plugin can be found quickly by log4j2.

The alternative is to specify the package name of your plugin class in your log4j2.xml configuration's packages attribute:

<Configuration status="warn" packages="com.mycompany">

UPDATE: Note that in your lookup implementation you can get as creative as necessary. For example:

package com.mycompany;

public class SomeClass {
    private static AtomicLong count = new AtomicLong(0);

    public static String getValue(final String key) {
        if (count.getAndIncrement() == 0) { // is this the first call?
            return ""; // don't output a value at system startup
        if ("FULL".equals(key)) {
            // returns info to shown on rollover, nicely formatted
            return fullyFormattedHeader();
        return singleValue(key);
Friday, June 11, 2021
answered 6 Months ago

Edit: for the newest versions of log4j2, see instead.

I get the impression they don't want you doing this, but this works for me:

if (arg[0].equals("log") ) {
  org.apache.logging.log4j.Logger logger
    = org.apache.logging.log4j.LogManager.getLogger("loggerNameFromXMLConfig");
  org.apache.logging.log4j.core.Logger coreLogger
    = (org.apache.logging.log4j.core.Logger)logger;
  org.apache.logging.log4j.core.LoggerContext context
    = (org.apache.logging.log4j.core.LoggerContext)coreLogger.getContext();
  org.apache.logging.log4j.core.config.BaseConfiguration configuration
    = (org.apache.logging.log4j.core.config.BaseConfiguration)context.getConfiguration();

} else {
Wednesday, July 14, 2021
answered 5 Months ago

Thanks to @Remko Popma answer I figure it out. Here is the solution example:

package com.bondarenko.tmp;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;

public class TestRouting {

private final static Logger log = LogManager.getLogger(TestRouting.class);

public static void main(String[] args) {
    ThreadContext.put("logFileName", "David");"Error happened");

    ThreadContext.put("logFileName", "Mark");"Something is broken");


And log4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">

    <Console name="consoleAppender" target="SYSTEM_OUT">
        <!--SHORT PATTERN-->
        <PatternLayout pattern="%d{ABSOLUTE} %level{length=5} [%thread] %logger{1} - %msg%n"/>
        <!--ROBUST PATTERN
        <PatternLayout pattern="%d{ISO8601} %level{length=5} [%thread] %logger - %msg%n"/>-->

    <Routing name="RoutingAppender">
        <Routes pattern="${ctx:logFileName}">
                <RollingFile name="Rolling-${ctx:logFileName}"
                    <PatternLayout pattern="%d{ABSOLUTE} %level{length=5} [%thread] %logger{1} - %msg%n"/>
                    <SizeBasedTriggeringPolicy size="512" />

            <!-- By having this set to ${ctx:logFileName} it will match when filename
                 is not set in the context -->
            <Route ref="consoleAppender" key="${ctx:logFileName}"/>


    <Logger name="com.bondarenko.tmp" level="info" additivity="false">
        <AppenderRef ref="RoutingAppender"/>

Wednesday, July 28, 2021
answered 4 Months ago

As stated in my comments above both


don't reconfigure already existing Logger instances, so that they still use your old EmailAppender. In order to make changes to take effect you should recreate loggers. If it's not possible (your loggers are static final fields for example), you can create a simple Logger wrapper, which will register itself with some listener, that will notify on configuration change, so that wrapper can create fresh logger instance

Sunday, November 28, 2021
answered 2 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :