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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:01:02+00:00 2026-06-01T06:01:02+00:00

I have created a JAVA application. Sometimes, users do invalid operations on it or

  • 0

I have created a JAVA application. Sometimes, users do invalid operations on it or there are some exceptions that the application encounters when it outputs the errors. However, these outputs are not visible unless I run the application from command line using
java -jar myapp.jar

I wish to record all of these to a file in the form of a log but I am unable to find a function or object which is responsible for outputting these errors etc.

To simplify my explanation, assume that my application outputs number from 1 to 10 using a for loop and a Sytem.out command. How can I record everything that is output to the System?

Thanks

  • 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-01T06:01:03+00:00Added an answer on June 1, 2026 at 6:01 am

    Agreed with above, you should use a logging framework. I prefer to use abstractions such as SLF4J.

    In this example you can use log4j underneath. Here is an example log4j.properties file you can put in the classpath of your java application.

    #Rootlogger logs to console and logfile
    log4j.rootLogger=INFO,stdout,logfile
    
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
    
    log4j.appender.logfile=org.apache.log4j.RollingFileAppender
    log4j.appender.logfile.File=/tmp/apname.log
    log4j.appender.logfile.MaxFileSize=1024KB
    # Keep three backup files.
    log4j.appender.logfile.MaxBackupIndex=3
    # Pattern to output: date [thread] priority [category] - message
    log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
    log4j.appender.logfile.layout.ConversionPattern=%d [%t] %p [%c] - %m%n
    
    #=============== CREATE A SEPARATE HANDLER FOR LOGGING SPECIFIC PACKAGES
    log4j.appender.MYPACKAGEHANDLER=org.apache.log4j.RollingFileAppender
    log4j.appender.MYPACKAGEHANDLER.File=/tmp/mypackage.log
    # 1/2 GB
    log4j.appender.MYPACKAGEHANDLER.MaxFileSize=512MB
    # Keep three backup files.
    log4j.appender.MYPACKAGEHANDLER.MaxBackupIndex=3
    # Pattern to output: message only
    log4j.appender.MYPACKAGEHANDLER.layout=org.apache.log4j.PatternLayout
    log4j.appender.MYPACKAGEHANDLER.layout.ConversionPattern=%m%n
    
    log4j.additivity.com.techtrip.mypackage=false
    log4j.logger.com.techtrip.mypackage=DEBUG, MYPACKAGEHANDLER
    

    Using this config will create two log files with a rolling appender, printing all debug log output of any class that logs in the com.techtrip.mypackage to /tmp/mypackage.log.

    Suppose a simple example class with a Logger and a Formatter:

    package com.techtrip.mypackage;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class Foo {
    
        private Logger logger = LoggerFactory.getLogger(Foo.class);
    
        private String someString;
    
        private Foo() {
            super();
        }
        public void setSomeString(String someString) {
            if (logger.isDebugEnabled()){
                logger.debug(String.format("Setting someString %s", someString));
            }
    
            this.someString = someString;
        }
    }
    

    This will log the setter output to the log file. Turn it off by simply changing the property file. Very simple.

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

Sidebar

Related Questions

There is a Java Struts application running on Tomcat, that have some memory errors.
I have created a small Java application in which there is some code that
I have created a Java application that loads some configurations from a file conf.properties
I have created a Java Swing application. Some users can't connect to the internet
I have created a java application for Debian Linux. Now I want that that
Hi I have created a java application. I found there is an application menu
I have created one application in flex that is accessing the Java webservice using
I have created a java web application using spring. I want the users of
I have created a java SE application that uses the mysql database using mysql
I have created a java application which depends upon some external jars. Now I

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.