Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6734489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:53:41+00:00 2026-05-26T10:53:41+00:00

I am starting on java and log4j. I need help with my test program.

  • 0

I am starting on java and log4j. I need help with my test program.

$pwd

/home/test/log4j

$ls

Log4jTest.class Log4jTest.java log_file logger.xml run.sh

$cat Log4jTest.java 
  import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jTest
{
public static void main(String args[])
{
Logger _log;
String loggerConfig = System.getenv ("logger_config");
 if (loggerConfig != null && !loggerConfig.trim ().equals (""))
  {   
    DOMConfigurator.configureAndWatch (loggerConfig);
  }   
else
{

System.out.println("no logger_config found");
    System.exit (-1);
}
 System.out.println("Logger is " + Log4jTest.class.getName ());
   _log = Logger.getLogger (Log4jTest.class.getName ());

 _log.info("logging to logger. Good!");
}
}

$ cat logger.xml

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

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/log4j.dtd">

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

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out" />
            <param name="threshold" value="OFF" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <appender name="log4j_test" class="org.apache.log4j.RollingFileAppender">
            <param name="File" value="/home/test/log4j/log_file" />
            <param name="append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <logger name="Log4jTest"
            additivity="false">
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </logger>

    <root>
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </root>

$export logger_config=/home/test/log4j/log_file

$javac -cp path to log4j__log4j.jar Log4jTest.java

$java -cp path to log4j__log4j.jar:. Log4jTest

Logger is Log4jTest

$ cat log_file

Question:

(1) Is it necessary to specify console appender?
(2) what goes in root?
(3) What does level_value in root as well as loggers do?
(4) I was expecting to see:
logging to logger. Good!

in log_file

What is going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:53:41+00:00Added an answer on May 26, 2026 at 10:53 am

    At least one appender is needed. Otherwise Log4J does not know where to print your log messages. There are various appenders: Console, file, JMS, JDBC. Each one knows to send messages to different destinations.

    You cannot see log messages on console because console appender is off:

    <param name="threshold" value="OFF" />

    Change OFF to INFO and I hope you will see your log.

    Log4J has hierarchical architecture of loggers. Each layer in hierarchy can block your message. Additional filtering is done on appenders layer. You can read more here: http://logging.apache.org/log4j/1.2/manual.html

    But pay attention: log4j is deprecated by its creator. Take a look on logback and slf4j.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting Java ... again. I just made a simple program class first
I need to prevent users from starting my Java application (WebStart Swing app) multiple
I'm starting a Java project and need a way to completely lock down a
Im a beginner in java and starting to do test driven development. I have
I am writing a bouncing ball class (I'm just starting to use Java) and
I am just starting Java RMI and have some problems with when to use
I'm starting a Java EE project that needs to be strongly scalable. So far,
I am starting a normal Java SE project and i am planning to use
We are starting a new Java EE project and am looking for suggestions regarding
I am starting a new Java web project that will be worked on by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.