• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static object doesn't get collected. Why?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class JLog to log all the messages to a file. The JLog class is in a jx.war file which is deployed to the application server JBoss.

If the jx.war is deployed once, the messages printed to the file only show up once, which is fine.
But if the jx.war is deployed twice, the messages printed to the file show up twice.
If the jx.war is deployed three times, the messages show up three time and so forth.

My question is why the static object jLogger from the previous deployments doesn't get collected.
How can I solve this problem?


public class JLog {

private static int logLevel = getLogLevel();
private static long line = 0;
static Logger jLogger;
private static PatternLayout jLayout = null;
private static Appender jAppender = null;

public static void toLogger(String msg ) {
try {
if ( jLogger == null ) {
// System.out.println(" *** get a new logger *** ");
jLogger = Logger.getLogger(JLog.class.getName() );
jLogger.setAdditivity( false );
jLogger.setLevel( Level.ALL );
}
if ( jLayout == null ) {
// System.out.println(" * new patternLayout " );
String pattern = "%d{yyyy-MM-dd HH:mm:ss,SSS} - %m%n";
jLayout = new PatternLayout( pattern );
pattern = null;
}
if ( jAppender == null ) {
// System.out.println(" * new FileAppender " );
String filename = "jx.log";
String dataPattern = "'.'yyyy-MM-dd";
jAppender = new DailyRollingFileAppender( jLayout, filename, dataPattern );
filename = null;
}
jLogger.addAppender( jAppender );
jLogger.debug( msg );
} catch (Exception e) {
System.out.println("Exception! JLog.toLogger >>> " + e.getMessage() );
e.printStackTrace();
} // try
} // toLogger

:
:
}
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's this line:

No matter if you have created a jLogger/jAppender or not, you're adding the appender to the logger.
 
Caly LeeAnn
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't add any Appender object to a Logger object, the Logger object doesn't know where output the message. And you endup have no output even the logging methods are called.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, but you only want to do that once. You have the same Logger until you recycle the JVM because it's static, and you keep adding appenders to the same Logger. Can you either force a new Logger every time you redeploy or only add the Appender the first time you create the Logger?

I could be wrong about this if redeploying creates a new ClassLoader to load everything up, but that's how it looks so far.
 
Caly LeeAnn
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the Log4J AIP, public void addAppender(Appender newAppender), if newAppender is already in the list of appenders, then it won't be added again.

Also, I moved jLogger.addAppender( jAppender ) to if( jAppender == null ) block, which means jAppender is only added once after it's instantiated. And I still have the same problem.

The question is how I can refer to the very first Logger object that I create.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want all that setup to be done only once, when the class is loaded -- and you do want that -- then put it all in a static initializer like this:(I tossed out your error handling but it needed to be redone anyway because you were catching Exception instead of specific exceptions that might be thrown.

You could also do all of this in a log4j.properties file instead of hard-coding it here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic