I am a pretty new user of Log4J v. 1.2.15, as a friend of mine convinced me of the advantages over console or other forms of logging. However, as i was doing some tests, i've encountered a test case that got me thinking. Here it is what i did :
- I've configured the properties, added 2 appenders, a
ConsoleAppender
and aRollingFileAppender
. - I've created a new log instance, using my main class :
Logger mainLogger = Logger.getLogger(Main.class);
I have this collection of general-purpose, hand-made, java utils, in a library, called
MyUtils.jar
, added to the classpath of my main app. In the main app, i've called a static method fromMyUtils.jar
. This method has a try-catch{} block and the exception was handled there, printing the stack trace, using System.err. Now, using my IDE and theConsoleAppender
, I was able to spot the problem, however, the event was NOT logged in my file log. There are 2 problems that need answers here: a. I am currently using mymainLogger
to log events from all classes of my app. Is that a good practice? Or should i use X logger instances for X classes? b. What can I do to be able to log errors already caught in my imports? It might sounds trivial, but I've usedObject foo = MyLibrary.composeObjectFoo()
, and inside the method looks similar to this example :public static Object composeObjectFoo() { try { .....statements..... . . // something stupid here int a = 100/0; //an AritmethicException will be thrown } catch Exception(e) { e.printStackTrace(); } }
Thanks for your answers and please excuse the length of this...