?
Solved

log4j outputting to a file

Posted on 2013-06-16
7
Medium Priority
?
480 Views
Last Modified: 2013-06-17
Hi there;

I want to output my variable to a file, and I need to use log4j framework.

I wrote the following code but it seems it has no effect since the file is empty. (I created the file manually, not with the code).

   Logger logger = Logger.getLogger("com.foo");
        logger.setLevel(Level.INFO);
        logger.info(val);
        FileAppender fa = new FileAppender();
        fa.setFile("C:/log.txt");
        logger.addAppender(fa);

Open in new window


I also find an elegant way with,
http://www.mkyong.com/logging/log4j-log4j-properties-examples/

but I don't know where to put this properties code. Should I put it into my java source code or create a file and map it? (if so, how?), what should I do?

Regards.

P.S. it's not a web project but a simple java code with no main class to produce Jar (so I cannot output it to console).
0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39251533
Here is what another expert put together. log4j to file

That might help. But if you need further assistance on it, then more help might be needed. ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39251572
I wrote the following code but it seems it has no effect since the file is empty.
That's because you use the logger (line 3) before you add the FileAppender to it
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39251684
Hi CEHJ;

One question: Is it possible to do this without properties file?

Regards.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 39251737
One question: Is it possible to do this without properties file?
To do what?

As it happens, the best way to configure and use log4j is to use as little code as possible.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39252377
Hi;

I mean should I also include the .properties file in the project? or is it OK without it?

Regards.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39252418
ah, thanks I solved it.

 
		Logger logger = Logger.getLogger("com.foo");
        logger.setLevel(Level.INFO);
     
        FileAppender fa = new FileAppender();
        fa.setWriter(new OutputStreamWriter(new FileOutputStream(new File("C:\\log.txt"))));
        fa.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
        
        logger.addAppender(fa);
        logger.info("aaaa");

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39252520
:)

But why would you have all that code in your own? It's not necessary...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question