0

I am new to log4j and need help understanding things about the logger instance. I define a logger in my class as a static member

public static Logger myLogger = Logger.getLogger(MyClass.class.getName());

A static member gets loaded into the memory when the class is loaded in memory by the JVM. If I am deploying my application as web service I am assuming the class will stay in the memory till the application is running. So that every time a request comes in , the application can immediately process it.

If i am using FileAppender or a JDBCAppender , would the file or database resources also be open throughout the life of the application. Or does the log4j framework under the hood handle these resources differently/smartly.

If not , are there any guidelines / best practices for gracefully handling these resources when using log4j?

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
Chiseled
  • 2,280
  • 8
  • 33
  • 59

1 Answers1

1

I recommend to use org.apache.log4j.RollingFileAppender 1 instead org.apache.log4j.FileAppender. It is always easier to handle small files ... especially if an error occurs.

And I also recommend using org.apache.log4j.DBAppender 2 instead org.apache.log4j.jdbc.JDBCAppender.

See also:


Notes

  1. Here is a sample configuration.
  2. From Apache Extras for Apache log4j. You can download the file apache-log4j-extras-1.2.17.jar here. This appender uses a database schema (not customizable) and you can find it here. If you want to use other tables, you'll need to rewrite the appender. Here is a sample configuration.
Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Thanks for pointing this out . Whats the idea behind logging to 3 diff tables ? – Chiseled Mar 06 '15 at 17:15
  • On the java docs I see both DBAppender and JDBCAppender use connection pooling. Are there other advantages of using DBAppender over JDBCAppender ? – Chiseled Mar 06 '15 at 17:21
  • Nope. You need to extends `JDBCAppender` in order to use connection pooling. See "For use as a base class:" in the [*javadoc*](http://bit.ly/1Bfoace). – Paul Vargas Mar 06 '15 at 17:26
  • The three tables are required. See the [*code*](http://pastie.org/pastes/10005474/text?key=ggkgajjrvbubgv4eky26rw) for this class. – Paul Vargas Mar 06 '15 at 17:45