0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. But I got this error: - ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. Looks like the config file is missing. But it is maven project and I add this file in the resource folder. To make sure, I made a package and checked the content of the jar . i can see log4j inside the jar. So what did I misused?
As I said, the log4j2.xml should be in your classpath. You can see the simple example here
0

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.

6 Comments

is it just ... ? Pls give full name what will be the namespace?
Sorry it formatted weird for some reason, I’ve just edited the post
What is this log4j.dtd
It is the Document Type Definition file for log4j. A DTD defines the structure and the legal elements and attributes of an XML document (w3schools.com/xml/xml_dtd.asp).
thx, this it worked. So I guess this log4j.dtd comes from the system meaning log4j library. Is it so? Perhaps, I have to ask another question for this property or wait for a while to if anybody can answer this property question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.