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

  • Home
  • SEARCH
  • 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 6640369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:39:51+00:00 2026-05-25T23:39:51+00:00

I’m currently writing a big project in java, with numerous classes, some classes are

  • 0

I’m currently writing a big project in java, with numerous classes, some classes are quiet small, that simply represent objects with only few methods.
I have a logger set in my main class, and it works fine.
I want to be able to use only one logger (with one console appender) with all the classes.
I tried to pass a reference to the logger to the different classes, but it doesn’t look right.
besides, sometimes I’m running tests on the classes without running main, and thus the logger is not initialized for the other classes.

What’s the best approach to accomplish that, I mean, how to log from different classes to one log, with no hard dependency between the classes and with the ability to use the log independently with each class ?

  • 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-25T23:39:52+00:00Added an answer on May 25, 2026 at 11:39 pm

    If I understand correctly, what you have at the minute is:

    public class Main {
        public static final Logger LOGGER = Logger.getLogger(Main.class);
    }
    
    public class AnotherClass {
        public void doSomething() {
            Main.LOGGER.debug("value=" + value);
        }
    }
    

    or, you pass references to a logger into the constructors of the class.

    Firstly, you can use one global logger by simply using the same value passed to Logger.getLogger, like:

    public class Main {
        private static final Logger LOGGER = Logger.getLogger("GLOBAL");
    }
    
    public class AnotherClass {
        private final Logger LOGGER = Logger.getLogger("GLOBAL");
    
        public void doSomething() {
            LOGGER.debug("value=" + value);
        }
    }
    

    This uses exactly the same logger, Logger.getLogger returns the same object in both calls. You no longer have a dependency between the classes, and this will work.

    The other thing I gather from your comments is that you are configuring by hand (using BasicConfigurator.configure. Most of the time this isn’t necessary, and you should be doing your configuration by simply adding a log4j.properties or log4j.xml to your classpath. In Eclipse this is done by adding it to src/ (or src/main/resources if you’re using maven). If you’re using junit, then add it to the test/ source directory (or src/test/resources with maven). This is a much better long term way of configuring log4j, because you don’t have to pass information between classes.

    Also, the recommended way to use loggers is to pass the class to the Logger.getLogger(). In this way you can filter your output based upon the class name, which is usually much more useful than just having one global logger:

    public class Main {
        private static final Logger LOGGER = Logger.getLogger(Main.class);
        public static final main(String[] args) {
            LOGGER.debug("started");
        }
    }
    
    public class AnotherClass {
        private final Logger LOGGER = Logger.getLogger(this.getClass());
    
        public void doSomething() {
            LOGGER.debug("value=" + value);
        }
    }
    

    Then in the log4j.properties, you can configure a single appender to one file.

    # Set root logger level to DEBUG and its only appender to A1.
    log4j.rootLogger=DEBUG, A1
    
    # A1 is set to be a ConsoleAppender.
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    
    # A1 uses PatternLayout.
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    

    Finally, it is not necessary to declare all of your loggers as static. This only makes a noticeable difference if you are doing lots[*] of object creation. Declaring your loggers as non-static fields allows you to use Logger.getLogger(this.getClass()); in which case adding a logger to a class becomes a cut and paste of a single line. This slf4j page contains a good explanation of the pros and cons. So use non-static fields unless you have a very good reason not to.

    Cameron is right when he say that you should try and use slf4j if possible, it has one killer feature, you can use multiple logging frameworks with it.

    [*] and I mean lots.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.