|
Logging System for Developing in Windchill
8.0
By Dmitry Tkach, isLaNd Inc.
In software development, logging is the process of
inserting statements into the program that provide some kind
of output information that is useful to the developer.
Examples of logging are trace statements, dumping of
structures and the familiar System.out.println or printf debug
statements.
Log4j is a popular logging package written in Java. One of
its distinctive features is the notion of inheritance. Using a
logger hierarchy, it is possible to control which log
statements are output at what granularity. This helps reduce
the volume of logged output and minimize the cost of
logging.
One of the advantages of the log4j API is its
manageability. Once the log statements are inserted into the
code, you can control them with configuration files. They can
be selectively enabled or disabled, and sent to multiple
output targets in different formats. The log4j package is
designed so that log statements can remain in shipped code
without reducing performance.
In the following example, we use log4j in Windchill to
eliminate the headache of maintaining thousands of
System.out.println statements. But first we need to do some
customization.
Setup
To use the tools we are about to install, you must set up
the operating environment so that the tools know where to find
what they need and the operating system knows where to find
the tools.
1. Download the log4j distribution from http://jakarta.apache.org/log4j/docs/download.html.
(During installation, Windchill 8.0 loj4j.jar is already
there.)
2. Extract the archived files to some suitable
directory.
The Logger
The Logger is the core component of the
logging process. In log4j, there are five normal levels of
logger available (not including custom levels). The following
descriptions are borrowed from the log4j API (http://jakarta.apache.org/log4j/docs/api/index.html).
· Static Level DEBUG.
Designates fine-grained informational events that are most
useful to debug an application.
· Static Level INFO.
Designates informational messages that highlight the progress
of the application at coarse-grained level.
· Static Level WARN.
Designates potentially harmful situations.
· Static Level ERROR.
Designates error events that might still allow the application
to continue running.
· Static Level FATAL.
Designates very severe error events that will presumably lead
the application to abort.
In addition, there are two special levels of logging
available. (Descriptions borrowed from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html.)
· Static Level ALL. Has
the lowest possible rank and is intended to turn on all
logging.
· Static Level OFF. Has
the highest possible rank and is intended to turn off
logging.
You can use any of seven levels: Level.DEBUG, Level.INFO,
Level.WARN, Level.ERROR, Level.FATAL, Level.ALL and
Level.OFF.
For our example here, we created the package in src/ext
folder and called – logging.
src/ext/logging/LoggingService.java
src/ext/logging/LoggingServiceFwd.java
src/ext/logging/LogginServiceHelper.java
src/ext/loggin/StandardLoggingService.java
src/ext/logging/LoggingUtils.java
To check the loading process using log files, we need to
configure Log4j for Windchill Server. Log4j is initialized via
a property file. Two configuration files exists. The primary
one is <i>PDM.lsf</i> in the codebase directory of
the Windchill server. For development purposes, a developer
may create a <i>PDMdev.lcf</i> exists.
<i>PDM.lsf</i> will not be used.
The Appender
The Appender controls how the logging is output. The
available appenders are as follows. (Descriptions borrowed
from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html.)
· ConsoleAppender:
Appends log events to System.out or System.err using a layout
specified by the user. The default target is System.out.
· FileAppender. Appends
log events to a file.
·
DailyRollingFileAppender. Extends FileAppender so that the
underlying file is rolled over at a user chosen frequency.
· RollingFileAppender.
Extends FileAppender to back up the log files when they reach
a certain size.
· WriterAppender.
Appends log events to a Writer or an OutputStream depending on
the user's choice.
· SMTPAppender. Sends
an email when a specific logging event occurs, typically on
errors or fatal errors.
· SocketAppender. Sends
LoggingEvent objects to a remote a log server, usually a
SocketNode.
· SocketHubAppender.
Sends LoggingEvent objects to a set of remote log servers,
usually SocketNodes.
· SyslogAppender Sends
messages to a remote syslog daemon.
· TelnetAppender.
Specializes in writing to a read-only socket.
You can also implement the Appender interface to
create your own ways of outputting log statements.
PDM-Wide Log4j Configuration
Here is a sample PDM.lsf configuration file that would go
in the Windchill logs folder.
Appenders
Stdout is set to be a ConsoleAppender that outputs to
System.out.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.threshold=WARN
The method server adds a timestamp to every line, so don't
do it via log4j too.
log4j.appender.stdout.layout.ConversionPattern=%-5p
%c{1} - %m%n
Send Deere messages to $WT_HOME/logs/test.log
log4j.appender.test=org.apache.log4j.RollingFileAppender
log4j.appender.test.File=${deere.logs.dir}/test.log
log4j.appender.test.MaxFileSize=6MB
log4j.appender.test.MaxBackupIndex=0
log4j.appender.test.layout=org.apache.log4j.PatternLayout
log4j.appender.test.layout.ConversionPattern=%d{DATE}
%-5p %c{1} - %m%n
Loggers
By default, everything goes to the stdout appender.
log4j.rooLogger=WARN, stdout
For third-party packages, we only want priority WARN or
above. Some of these packages use Jakarta Commons Logging
wrapper which, when log4j is found on the classpath, routes
the logs through log4.
log4j.logger.org.apache=WARN
log4j.logger.org.apache.commons.httpclient=WARN
log4j.logger.httpclient.wire=WARN
log4j.logger.ext.cf.loaders.Test=DEBUG,
deere
log4j.additivity.ext.cf.loaders.Test=false
Setup for Windchill
So now, to configure several log files at once for each
module, you need to set up log4j for Windchill Server.
1. Copy the log4j.jar archive to the Windchill codebase
directory.
2. Create the PDM.lcf (production servers) or PDMDev.lcf
(development servers)
3. Initialize the log4j in the new sources code files if
you need.
4. Restart the Tomcat and Method Server. 
Dmitry Tkach is software architect at isLaNd
Inc.(www.ittechstar.com).
He can be reached by email at dmitrytkach1@hotmail.com. |