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 8365519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:40:50+00:00 2026-06-09T12:40:50+00:00

I want to use Log4J for logging my java projects. I created a log4j.properties

  • 0

I want to use Log4J for logging my java projects.
I created a log4j.properties file in the src directory with the following content:

# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.DEFAULT_LOGGER=INFO,file2

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n



# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n


log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=file2.log
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

For example I want to use the “DEFAULT_LOGGER” only in my main method. So I wrote:

static Logger log = Logger.getLogger("DEFAULT_LOGGER");
log.fatal("Process Logger");

But when I execute the main method I prints the message “Process Logger” to all Appenders (stdout, file and file2) But I only want to print it out to file2. How can I do that or better ro say what do I do wrong?

The second point is, when I execute the main method the second time, it does not overwrite file and file2, it justs adds a line inside the textfile. How can I avoid that?

  • 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-06-09T12:40:52+00:00Added an answer on June 9, 2026 at 12:40 pm

    Log4j has something called additivity. by default it is set to true and it means that every log you write will not only be logged by the specific logger but also by its ancestors (the root logger in this case).

    To set it to false try this:

    log4j.additivity.DEFAULT_LOGGER = false
    

    Learn more about it here.

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

Sidebar

Related Questions

I use log4j for logging and i want to print all logger.debug statements in
I want to use the log4j logging framework in a jsp file. The typical
I have a java main application, and I want to use log4j. I don't
We use Log4j (and Commons Logging) to log our error messages. Now we want
i am a Java beginner and i want to use log4j , but i
I am using java.util logging classes to create a rolling file appender. I want
I am using Jacorb 3.0 and I want to use log4j as the logging
I want to use log4j in my jsp s and servlets. I read apache
I want use a single php file to handle all of my voting requests.
Here is my log4j.properties file # Set loggers' levels log4j.rootLogger=warn, trace_file # Appender log4j.appender.trace_file=org.apache.log4j.RollingFileAppender

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.