Solved

log4j outputting to a file

Posted on 2013-06-16
7
469 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now