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

The Archive Base Latest Questions

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

I am developing a demo app using Servlet/JSP and using logging JAR by apache

  • 0

I am developing a demo app using Servlet/JSP and using logging JAR by apache called log4j from http://logging.apache.org/log4j/1.2/

My question is related to where to use logging. Any best practices.?

1 – Should log error messages in try/catch and also throw exception. See example below?

2 – Should use it where we need to use System.out.println(“message”) for debugging or printing info?

3 – Should use it in production or only in development?

How do you use it in your application?

Am I doing it right or generating useless messages?

try{
            con = ConnectionManager.getConnection();
            ps = prepareStatement(con, SQL_DELETE, false, values);
            int affectedRows = ps.executeUpdate();
            if(affectedRows == 0){
                log.error("delete: Deleting user failed, no rows affected");
                throw new DAOException("Deleting user failed, no rows affected.");
            }else{
                user.setId(null);
            }
        }catch(SQLException e){
            log.error("delete: " + e.getMessage());
            throw new DAOException(e);
        }finally{
            close(con, ps);
        }


try{
            Class.forName(DRIVER);

            try{
                Class.forName(DRIVER);
                log.info("Connecting database...");
                con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                log.info("Database connected!");

            }catch(SQLException ex){
                throw new RuntimeException("Cannot connect the database!", ex);
            }
        }catch(ClassNotFoundException e){
            log.error("Closing ResultSet failed: " + e.getMessage());
            System.exit(-1);
        }
  • 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-09T07:09:01+00:00Added an answer on June 9, 2026 at 7:09 am

    Loggers are basically used to capture debug statements, info or error messages in log files.

    Ideally you should not use System.out.println(“message”) anywhere in your web app, only use loggers, like log4j.

    1. All error messages caught in try-catch block should be logged as

      if(log.isErrorEnabled()) {
      log.error(“error message”);
      }

    2. Statements which you use to debug your app or to print some development information in logs can be written as:

      if(log.isDebugEnabled()) {
      log.debug(“debug message”);
      }

    or

    if(log.isInfoEnabled()) {
       log.info("debug message");
    }
    

    Once you have added loggers in above format in your code,
    You can enable-disable them from being printed in logs by setting logging level in Log4j configuration (properties or XML file), without recompiling source code.

    For Example,

    if logging leve is DEBUG -> debug, info and error messages will be logged in log file.

    if logging leve is INFO -> only info and error messages will be logged in log file (no debug messages).

    if logging leve is ERROR -> only error messages will be logged in log file (no deebug or info messages).

    Ideally, in production we set logger level to ERROR and in development logger level is set to DEBUG or INFO.

    Log4j is much more powerful and useful than what i have written above.
    For more details refer to Log4j manual.

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

Sidebar

Related Questions

I am developing a web app on JBOSS AS 7 using Richaces 4, SEAM
I am developing a demo application using Last.FM API which will get list of
i am developing a site which demo URL is http://www.abc.com now my client wants
I am developing a Windows Azure Demo Site which uses HTTPS instead of HTTP.
I am developing an app in Android/Phonegap using Javascript. I have tried the insert
Developing for Android 2.3, I have a question regarding layouts. I use a vertival
While developing an application using gwt in ecliplse crashed. Now the server is running
For developing an devices monitor system, I am using a InetAdress isReachable method to
Im developing an app for iPhone, in wich one of the functionalities is an
iam developing one application.In that i need to get the music files from the

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.