Custom module logging to a separate file (on top of usual logging)

Hi all,

What is the proper/recommended way to have a custom module to do logging to a separate file (while still also being logged as usual at the same time, in openmrs.log for instance)?

2 Likes

I looked here https://wiki.openmrs.org/display/docs/Java+Conventions#JavaConventions-loggingLogging but did not see any recommendation for such. What do you have in mind? :slight_smile:

1 Like

I “guess” I’m getting closer, I did this in my custom module:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="INITIALIZER_LOGFILE_APPENDER" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="false"/>
    <param name="file" value="initializer.log"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%p - %C{1}.%M(%L) |%d{ISO8601}| %m%n" />
    </layout>
  </appender>

  <logger name="org.openmrs.module.initializer" additivity="false">
    <level value="INFO"/>
    <appender-ref ref="INITIALIZER_LOGFILE_APPENDER"/>
  </logger>

  <root>
    <appender-ref ref="INITIALIZER_LOGFILE_APPENDER"/>
  </root>

</log4j:configuration>

Things seem to work fine (based on what I observe within tests). However at runtime the log file is nowhere to be found… I was taking the inspiration from this line:

<param name="file" value="openmrs.log"/>

Of course I could provide a path such as /opt/openmrs/initializer.log but this would make me bump into access rights issues. What would be the way to specify a path inside the app data dir?

1 Like

am not sure if log4j2 supports multiple configuration files but my search seems not to suggest that it does, the quickest way would be adding your appender in the openmrs log4js configuration file, i think there was a way to generate appenders dynamically depending on a runtime property availability

something like;

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
   <appender name="${sys:openmrs.module.id}_DEBUGGING_FILE_APPENDER" class="org.apache.log4j.RollingFileAppender">
     <param name="append" value="false"/>
     <param name="file" value="${sys:openmrs.module.id}.log"/>
     <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
     </layout>
  </appender>

   <logger name="org.openmrs.module.${sys:openmrs.module.id}" additivity="false">
   	<level value="INFO" />
   	<appender-ref ref="${sys:openmrs.module.id}_DEBUGGING_FILE_APPENDER"/>
   </logger>
   
   <root>
   	<appender-ref ref="${sys:openmrs.module.id}_DEBUGGING_FILE_APPENDER" />
   </root>


that can now work but u would want OpenMRS to evaluate the id  basing on some mechanism so that the same definition can work for more than one module
1 Like

hi @mksd, wondering if you got some work around for this?