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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:23:43+00:00 2026-06-06T20:23:43+00:00

I have developed a spring application, I want to configure it with apache log4j,

  • 0

I have developed a spring application, I want to configure it with apache log4j, have downloaded it and put the jar in project’s class path. Below is my log4j.Properties file.

# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C\:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

And below is my main spring application class.

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class DrawingClass {

    public static void main(String args[])
    {
        //without dependency injection 
        /*Triangle t1 = new Triangle();
        t1.draw();*/


        //with dependency injection     
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("Spring.xml"));
        Triangle t1 =(Triangle) factory.getBean("triangle");
        t1.draw();
    }
}

Please advise if I want to put the log.info in my above main class what modifications I need to do in my main class and also please advise what modifications I need to done to call log4j in my main class?

come up with this solution and it works ..the edited log4j.properties file is

### direct messages to file or.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C:/logs/s.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n
log4j.appender.file.append=true

### set log levels - for more verbose logging change 'info' to 'debug' ##
log4j.rootCategory=ALL, file
log4j.logger.Demo=\=debug
log4j.logger.org.eclipse=debug

and the way to callit from main class is

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class DrawingClass {
     /* Get actual class name to be printed on */
    static final Logger log = Logger.getLogger(DrawingClass.class);
    public static void main(String args[])
    {PropertyConfigurator.configure("log4j.properties");
        //without dependency injection 
        /*Triangle t1 = new Triangle();
        t1.draw();*/

         log.info("Before execution");
        //with dependency injection     
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("Spring.xml"));
        Triangle t1 =(Triangle) factory.getBean("triangle");
        log.info("Hello this is an info message");
        t1.draw();
         log.info("after object execution");
    }
}

If there is any other better way then please advise .

  • 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-06T20:23:45+00:00Added an answer on June 6, 2026 at 8:23 pm

    Can you try adding this line to your class –

    public class DrawingClass {
    
       static final Logger log = Logger.getLogger(DrawingClass.class);
    
    public static void main(String args[])
    {
        //without dependency injection 
        /*Triangle t1 = new Triangle();
        t1.draw();*/
    
        log.info("Before execution");
        //with dependency injection     
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("Spring.xml"));
        Triangle t1 =(Triangle) factory.getBean("triangle");
        t1.draw();
    }
     }
    

    Now let me know if anything gets added to the log.

    This link might help you – http://www.dzone.com/tutorials/java/log4j/sample-log4j-properties-file-configuration-1.html

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

Sidebar

Related Questions

I have a web application developed with Spring 2.5. The application has numerous controllers,
I have used spring framework 3 for my application. Everything is ok while developed
I have a web application developed with Struts2, JSP, JPA, Spring and MySql. I
I have developed an utility class for spring which is a singleton which will
I have developed an asp.net application that has a Gridview control. I want it
I have developed an application with Spring MVC that deals with bulk data insert/update.
I have developed one application which has 15 screens. Now I want to display
I have developed sample api as jar file. This jar file contains the code
I have developed a website using Razor (Microsoft WebMatrix) and now I want to
I have an existing, fully functional Spring web application based on Spring 2.5.6 -

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.