masiboo
masiboo

Reputation: 4719

Java Apache Log4j log4j.xml config multiple error

I think it might have been asked before. But it has some extra questions about how to use the property. Here is my log4j.xml. :-

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
   <Properties>
      <Property name="myinfo">INFO</Property>
      <Property name="mywarn" value="WARN"/>
  </Properties>

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="threshold" value="info" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level Line: %L  - %msg%n" />
    </layout>
</appender>


<appender name="LOGFILE" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="c:/temp/logfile.log" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="2MB" />
    <param name="MaxBackupIndex" value="2" />
    <priority value="WARN" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level Line: %L  - %msg%n" />
    </layout>
</appender>


<appender name="UNMAPFILE" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="c:/temp/unmapped.log" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="2MB" />
    <param name="MaxBackupIndex" value="2" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
               value="%d [%t] %-5p %c - %m%n" />
    </layout>
</appender>


<category name="com.ma.dev" additivity="false">
    <priority value="INFO" />  <!-- <priority value="${myinfo}" /> --> 
    <appender-ref ref="STDOUT" />
    <appender-ref ref="LOGFILE" />
</category>


<root>
    <priority value="WARN" />
    <appender-ref ref="UNMAPFILE" />
</root>

  </Configuration>

Here is my Java code:-

package com.ma.dev;

import org.apache.log4j.Logger;

public class App 
{
    public static Logger logger = Logger.getLogger(App.class.getName());
    public static void main( String[] args )
    {
        logger.info("test");
        System.out.println( "Hello World!" );
        logger.warn("Dummy warning");
    }
}

I got the following error/warning:-

log4j:WARN Continuable parsing error 2 and column 16
log4j:WARN Document root element "Configuration", must match DOCTYPE root "null".
log4j:WARN Continuable parsing error 2 and column 16
log4j:WARN Document is invalid: no grammar found.
log4j:ERROR DOM element is - not a <log4j:configuration> element.
log4j:WARN No appenders could be found for logger (com.ma.dev.App).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hello World!

Process finished with exit code 0

In the log4j.xml file I have this property:-

<Properties>
     <Property name="myinfo">INFO</Property>
     <Property name="mywarn" value="WARN"/>
</Properties>

I wanted to use this property to chnage the priority level as:-

 <priority value="${myinfo}" />

is it possible?

Upvotes: 0

Views: 1079

Answers (2)

SternK
SternK

Reputation: 13041

It looks like you tried to mix Log4j 1.x and 2.x configuration.

If you want to use Log4j 2.x, you should create log4j2.xml in your classpath. The simple example:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
       <Properties>
         <Property name="priorityLevel">warn</Property>
       </Properties>
       <Appenders>
          <Console name="myConsole" target="SYSTEM_OUT">
             <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
          </Console>
       </Appenders>
       <Loggers>
          <Root level="${priorityLevel}">
             <AppenderRef ref="myConsole"/>
          </Root>
       </Loggers>
    </Configuration> 

Also you should have log4j-api-2.12.1.jar and log4j-core-2.12.1.jar in your classpath and then you can use it.


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

public class App
{
   private static final Logger logger = LogManager.getLogger(App.class.getName());

   public static void main(String[] args)
   {
      logger.info("test");
      System.out.println( "Hello World!" );
      logger.warn("Dummy warning");
   }
}

More details you can find here.

Upvotes: 1

DylanRodgers98
DylanRodgers98

Reputation: 11

You’re getting those errors because you haven’t included the log4j namespace in a DOCTYPE element. The top of your file should look like this:

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    ...

As for your second question, I’m not quite sure if that’s possible.

Upvotes: 0

Related Questions