log4j etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
log4j etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

21 Aralık 2011 Çarşamba

log4j NDC

Here is a link about logging best practices:  http://c2.com/cgi/wiki?LoggingBestPractices
The last rule of thumb:
If you're logging the output of a web service or something like that, make sure you log an item which is unique to the session in each log message. With log4j, you can do this using a NDC. (We didn't use the session id, thats too long, we grabbed a unique integer and put it into the session). This way you can trace whats happening to a single session very easily. You can also log the request parameters as well if you're concerned about that sort of thing


for code sample:

public ReturnType method( InputType input) throws ExceptionType{

      NDC.push("identifyme");
      try {
         // do something
      } catch(ExceptionType e) {
         throw e;
      } finally {
         NDC.pop();
      }
}

14 Aralık 2011 Çarşamba

delete log files automatically with log4j

With RollingFileAppender, there is a maxBackupIndex property which specifies the number of log files that will be kept.. Unfortunately, DailyRollingFileAppender does not support such property. Therefore, to use maxBackupIndex with DailyRollingFileAppenderhttp://www.codeproject.com/KB/java/CustomDailyRollingFileApp.aspx (and http://wiki.apache.org/logging-log4j/DailyRollingFileAppender) provides a custom DailyRollingFileAppender.

I included the file http://wiki.apache.org/logging-log4j/DailyRollingFileAppender in my project.. By the way, since some classes used (such as org.apache.log4j.RollingCalendar) is not visible outside the package org.apache.log4j, do not try to put the file in another package..

13 Eylül 2011 Salı

logging the stacktrace

logging the stacktrace with log4j:


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

// ......
static Logger logger = Logger.getLogger(MyClass.class);

// ......
public static void debugException(Exception ex) {
    //ex.printStackTrace();
    logger.error("An error occurred in MyClass.", ex);
}

28 Temmuz 2011 Perşembe

a simple log4j example

the properties file is something like that:

log4j.rootLogger=DEBUG


log4j.appender.SampleAppender =org.apache.log4j.FileAppender
log4j.appender.SampleAppender .File=Sample.log
log4j.appender.SampleAppender .layout=org.apache.log4j.PatternLayout
log4j.appender.SampleAppender .layout.ConversionPattern= [%t] %-5p %c{2} %d %x - %m%n


log4j.logger.org.sample=,SampleAppender 





import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class Sample {


    static Logger logger = Logger.getLogger(Sample.class);



    public static void debug(String message, boolean debugModeOn) {
        if (debugModeOn) {
            PropertyConfigurator.configure("log4j.properties");
            logger.debug( message);
        }
    }




log4j.properties file must be in classpath..
for log format: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html