jrauschenbusch
I'm new here

Module-specific log4j.properties

Hi community,

Is it possible to define a module-specific log4j.properties file, which will be evaluated by the logging system? In my case i need a mechanism to provide an additional custom log4j appender implementation. But this should be defined by the module itself and not in the fs-logging.conf file. The reason is, that the custom appender is defined in the module and should only be used by the module. It should be cicumvented that the appender is still defined in the fs-logging.conf even if the module is not installed anymore. The custom appender is used to create a log file in the log-directory of the FirstSpirit installation directory to create daily rolling log files. My problem is, that this custom implementation isn't currently instantiated/invoked by the logging system despite a log4j.properties file is defined in the root of the fsm structure.

Thank you in advance for your help!

0 Kudos
3 Replies
andre
I'm new here

> Is it possible to define a module-specific log4j.properties file, which will be evaluated by the logging >system?

unfortunately no but

maybe by this way?

org.apache.log4j.Logger#getRootLogger#addAppender(YourAppender extends  org.apache.log4j.AppenderSkeleton | RollingFileAppender)

--

andre

I have already created such a mechanism, but the way described above would be much more cleaner. Too bad that this is not possible. I think this is should be a feature request for the future of FS.

Here is my solution:

public static void initDefaultLogging(ServerEnvironment environment, String filename, Class clazz) {
    Logger moduleLogger = Logger.getLogger(clazz);
    moduleLogger.setAdditivity(false);

    try {

      OutputStream out = environment.getLogDir().obtain(filename).getOutputStream();

      PatternLayout layout = new PatternLayout("%d{ISO8601} %-5p [%t] %c: %m%n");

      WriterAppender writeAppender = new WriterAppender(layout, out);

      moduleLogger.addAppender(writeAppender);
    } catch (IOException ex) {
       logger.error("Module logger could not be initialized.", ex);
    }
}

public static void initDailyRollingFileLogging(String directory, String filename, Class clazz) {
    Logger moduleLogger = Logger.getLogger(clazz);
    moduleLogger.setAdditivity(false);

    try {
       Path logPath = Paths.get(CMSServer.getRootDir(), CMSManager.MODULES_LOG_DIR);
       String logFile = logPath.resolve(directory).resolve(filename).toString();

       PatternLayout layout = new PatternLayout("%d{ISO8601} %-5p [%t] %c: %m%n");

       DailyRollingFileAppender appender = new DailyRollingFileAppender(layout, logFile, "yyyy-MM-dd");

       moduleLogger.addAppender(appender);
    } catch (IOException ex) {
       logger.error("Daily rolling file logger could not be initialized.", ex);
    }
}

--

Jochen

0 Kudos

Hello Jochen,

am I right in thinking that this posting is answered? Or is there still an open question?

You mentioned a feature request. If you did not create it already, you could do it here: Click

Best regards

Michaela

0 Kudos