Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Maintaining Application Debug Information using Log4J and Log4net

Application developers rely on application logging to gather usage data, track issues and debug applications of all types across the software industry.  Whilst a massively valuable tool, logging every use can lead to consequences such as increased disk space usage and continual disk I/O leading to detrimental application performance as experienced by the end user. The challenge for developers is how to manage logging information without impacting on performance or maintenance requirements.

Apache offers a logging tool for both Java (Log4J) and .NET (Log4net) developers. This is a framework for logging which helps manage the issues described above.  Log4* is available for download or inclusion from your preferred package manager.

SAS Mid-tier applications utilise the Log4J framework for extensive and configurable logging functions by default.  This is highly valuable to platform administrators for troubleshooting as well as from an audit perspective.  The paradox is that, over time, log files can become ever-larger to the point that a detrimental effect on performance is seen.

Some administrators take an ad-hoc approach to handling the issues of performance by manually archiving files when an issue becomes apparent. This approach has two issues:

  1. Downtime is needed to ensure log files are archived, affecting application users.

  2. Clearing log files becomes part of a diagnostic process whenever a performance issue is seen on the Mid-tier, potentially acting as a red-herring when a different issue is present.

Fortunately, there is a better way. Log4J can be configured to automatically roll-over to a new file per given time-period, meaning file locks don’t exist on archived files enabling maintenance without downtime. Full details of the Log4J configuration options are beyond the scope of this paper - we will only focus on the critical elements to achieve the required log archiving.

In SAS, Log4J configuration files are found in the following location:

<sasconfig>\lev<n>\web\Common\LogConfig\

The example below shows the default log configuration for SAS Studio. There are two key elements (tags) in the XML; Appender and Root.

  • Appender: Contains instructions on how to write a log 
  • Root: Activates one or more Appenders

The code block below shows the default XML configuration file for SAS Studio 3.6:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender class="org.apache.log4j.ConsoleAppender" name="SAS_CONSOLE">
    <layout class="com.sas.svcs.logging.CustomPatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p [%u] %c - %m%n"/>
    </layout>
  </appender>
 
  <appender class="org.apache.log4j.FileAppender" name="SAS_FILE">
    <param name="append" value="true"/>
    <param name="file" value="${config.lev.web.appserver.logs.dir}/SASStudioMidTier3.6.log"/>
    <layout class="com.sas.svcs.logging.CustomPatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p [%u] %c - %m%n"/>
    </layout>
  </appender>
  <!-- A time/date based rolling appender
    <appender class="org.apache.log4j.DailyRollingFileAppender" name="SAS_FILE">
        <param name="datePattern" value="'.'yyyy-MM-dd"/>
        <param name="append" value="true"/>
        <param name="file" value="${config.lev.web.appserver.logs.dir}/SASStudioMidTier3.6.log"/>
        <layout class="com.sas.svcs.logging.CustomPatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p [%u] %c - %m%n"/>
        </layout>
    </appender>
    -->
  <category additivity="false" name="com.sas">
    <priority value="WARN"/>
    <appender-ref ref="SAS_CONSOLE"/>
    <appender-ref ref="SAS_FILE"/>
  </category>
  <root>
    <priority value="WARN"/>
    <appender-ref ref="SAS_CONSOLE"/>
    <appender-ref ref="SAS_FILE"/>
  </root>
</log4j:configuration>

Notice that there are TWO <appender> tags with the name attribute set to “SAS_FILE”.  However, the second such <appender> tag is commented out.  In brief, we need to switch the commented and uncommented tags, but let’s interrogate the XML first.

<appender class=”…”

The value in the class attribute indicates the type of log to be produced, this could be console output, log files, or even writing to a database such as SQL Server.  The default uses the FileAppender class, which does exactly as it says on the tin; it writes to a file.  The second (commented) tag specifies the DailyRollingFileAppender. This class inherits and extends the FileAppender class with additional functionality.

Crucial to the latter DailyRollingFileAppender class is the DatePattern attribute.  This indicates not only the name given to the rolling file but how frequently this occurs. Take the following (default) DatePattern.

<param name="datePattern" value="'.'yyyy-MM-dd"/>

The value attribute indicates the file will be appended with a new extension relating to the current date, this also doubles as frequency indicator, that archives will occur at midnight each day.  Archiving can occur hourly, minutely or even every second(1).

The downside to the default date-pattern is that each file has a unique file extension, referring to the date of the file. It’s possible to append a fixed extension, e.g.: .log, .logarchive, .txt etc. by adding the required text in single quotes, inside the value attribute as per the example below

<param name="datePattern" value="'.'yyyy-MM-dd'.logarchive'"/>

Applying these modifications to the SAS Studio log configuration file will result in the example below:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender class="org.apache.log4j.ConsoleAppender" name="SAS_CONSOLE">
    <layout class="com.sas.svcs.logging.CustomPatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p [%u] %c - %m%n"/>
    </layout>
  </appender>
 
   <!--A time/date based rolling appender-->
    <appender class="org.apache.log4j.DailyRollingFileAppender" name="SAS_FILE">
        <param name="datePattern" value="'.'yyyy-MM-dd'.txt'"/>
        <param name="append" value="true"/>
        <param name="file" value="${config.lev.web.appserver.logs.dir}/SASStudioMidTier3.6.log"/>
        <layout class="com.sas.svcs.logging.CustomPatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p [%u] %c - %m%n"/>
        </layout>
    </appender>
   
  <category additivity="false" name="com.sas">
    <priority value="WARN"/>
    <appender-ref ref="SAS_CONSOLE"/>
    <appender-ref ref="SAS_FILE"/>
  </category>
  <root>
    <priority value="WARN"/>
    <appender-ref ref="SAS_CONSOLE"/>
    <appender-ref ref="SAS_FILE"/>
  </root>
</log4j:configuration>

 Finally, you must restart the relevant SAS services before changes to the log configuration takes effect.

 

 

References:

  1. Apache Log4j 1.2.17 API https://logging.apache.org/log4j/1.2/apidocs/overview-summary.html, visited on 10 April 2018.