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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:11:51+00:00 2026-05-30T08:11:51+00:00

Is there any way to write error log or exception into a file in

  • 0

Is there any way to write error log or exception into a file in java. i have gone through Log4j. I googled about it but diidnt find a good solution. I have written a simple code

catch (Exception e) {
    PrintWriter pw = new PrintWriter(new FileOutputStream("Log"));     
    e.printStackTrace(pw);
} 

Is there any other way to log the errors or exception? can any body provide me wwith sample example of Log4j?

  • 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-30T08:11:53+00:00Added an answer on May 30, 2026 at 8:11 am

    First read log4j Manual, it’s easy to configure a rolling log file. You do not have to do any explicit file operations.

    #SET LEVEL of ROOT-LOGGER, you will like to have Debug in local, but in prod you may just want WARN and ABOVE. This setting is done here!
    log4j.rootLogger=debug, stdout, R
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    
    # Pattern to output the caller's file name and line number. (basically, format of log)
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    
    # THIS IS WHERE YOU WILL HAVE ALL THE LOG WRITTEN
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=/var/log/applogs/example.log
    
    # Maximum size of log file, usually we keep 10MB
    log4j.appender.R.MaxFileSize=100KB
    # Keep one backup file, usually we keep 10
    log4j.appender.R.MaxBackupIndex=1
    
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
    

    Second, whenever you catch an exception, do like this

    public class MyClass{
    
      private static Logger logger = Logger.getLogger(MyClass.class);
    
      public ReturnType myMethod(Param p, Param2 p2) {
        ....
        ....
        try {
          ..    
        } catch(MyException e) {
           logger.log("Exceptions happen!", e); //this will put all the details in log file configured earlier
        }
        ....
      }
    
      ....
    }
    

    It worth reading the manual. Even better read Complete log4j Manual

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

Sidebar

Related Questions

Is there any way to write a Visual Studio Project file easy or do
Is there any standard way of implementing some sort of a write-through buffer for
Is there any way to Write files to App_Data under medium trust hack? Im
Is there any way to write a valid header/link/image? Something like <a href=index.html><h1 id=logo>Company
Is there any way to write Shoes application using IDE (NetBeans for example)? I
Is there any way to write binary output to sys.stdout in Python 2.x? In
is there any way to write this foreach in linq or another better way,
Is there any way to write a MIDlet, but still use BlackBerry API classes
Calling all C macro gurus... Is there any way to write a C macro
Is there any other way to write javascript:false that is more pleasant? I'm building

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.