Log4j Integration With Novell exteNd Application Server

marlinlineInternet and Web Development

Oct 31, 2013 (3 years and 10 months ago)

77 views

Log4j Integration With Novell exteNd Application Server

By Bill Massie


Log4J is the Jakarta project for managing logging in java applications. It is
becoming increasingly popular with enterprise application developers because of its
ease of use, its flex
ible configurations and the general popularity of the Jakarta
projects among Java developers.


You can use Log4j in exteNd both for
Application Logging
, the log messages that
the programmer puts into the application code for debugging and error handling, a
nd
Server Logging
, the messages that the Application Server itself generates to track
server events. It is easy to confuse these two types of logging, but it is important to
understand the distinction because they come from different sources and are
handl
ed differently by the server and Log4j.


Log4j is typically used for Application Logging. However, the exteNd Application
Server allows you to use Log4j for server logging as well. This gives you powerful
control over your Server Logging. This can be
particularly useful in a clustered
environment.


This article will focus on Server Logging. It will touch on Application Logging briefly,
to describe the interrelationships with Server Logging. This article also discusses a
custom Log4j appender that can

be used send Lo4j log messages to the exteNd
server console. The article assumes some familiarity with Log4j. If you are new to
Log4j, you should refer to the Log4j home page on the Jakarta website:
http://jakarta.apache.org/log4j/docs/index.ht ml.

exteN
d Server Logging using Log4j

The extendlog4j.jar file, available on Novell.com, makes it easy to do Server Logging
with Log4j on the exteNd Application Server. The exteNd Application Server
generates three separate logs: HTTP, Error and Trace. By default

extendlog4j.jar will
send these logs to Log4j’s RollingFileAppender. This will create log files that are
backed up when they reach a certain size. The Error and Trace logs are sent to
standard text files. The HTTP log is sent to a text file in either N
CSA Common or
Combined format. This allows for web traffic analysis with standard tools such as
WebTrends (
http://www.webtrends.com
) and AWStats
(
http://awstats.sourc
eforge.net
).

Installing extendlog4j.jar

In the SMC you have the option of sending server logs to a database, a file or to
handle them with a user
-
defined java class. You will use the user
-
defined option
integrate with Log4j.


In the SMC, click on the Co
nfiguration icon, then the General tab. The lower half of
the screen contains the logging settings. Under Server Logging select the User
Defined radio button. In the entry box for the java class of your logger select one of
the two logging classes that c
ome in extendlog4j.jar.



com.sssw.srv.logging.log4j.http.CommonNCSALogger
1

Generates standard
error and trace logs and generates the HTTP log in NCSA Common format.



com.sssw.srv.logging.log4j.http.CombinedNCSALogger
1

Generates standard
error and trace logs
and generates the HTTP log in NCSA Combined format.


SMC settings for adding the Log4j logger:




Select the checkboxes for each type of log that you want. The fields for the log file
names are ignored. The file names are confi
gured in the log4j.properties file
discussed below.


Click Update. You will be told that you need to restart your Application Server.
Before you restart, make sure that the extendlog4j.jar and the Log4j library (log4j
-
1.2.7.jar as of this writing) are in

your AGCLASSPATH. The server will not start if it
cannot find the class you have specified for logging
2
.


After you restart your server, the log messages will be going to Log4j. Depending on
which logging options you selected, you will see files named

Extend_Error_Log.log,



1

See
http://www
-
3.ibm.com/software/webservers/siteanalyzer/doc/Infocenter/web/en/guide/c
-
logs.html

for
a description of the NCSA file formats.


2

If you have problems, for instanc
e, if you mistyped the class, you will have to manually change the
logging options in your httpd.props file in the [appserver]
\
Resources directory.

Extend_HTTP_Log.log and Extend_Trace_Log.log. These files will be in your
[exteNdAppServer]
\
bin
3

directory. The log messages are sent to rolling files that will
roll at 10 megabytes and keep 10 backup files. All these options, inc
luding the file
names, are easily configurable.

Options for Server Logging

Log4j options are set in the log4j.properties file. Every aspect of the logging is
configurable. Log4j has tools for sending your logs to text files, databases, GUI log
viewers an
d JMS topics among others. extendlog4j.jar includes a log4j.properties
file that sets the default logging properties. To change the defaults, delete the
log4j.properties file from extendlog4j.jar and create your own log4j.properties file.
This file must

be in your AGCLASSPATH.


Log4j has three main components: loggers, appenders and layouts. Loggers are
named entities that establish a category of log message. Appenders determine
where a particular category of log message is sent, to a file, to a JMS
topic etc. and
layouts determine how the message is formatted.


extendlog4j.jar uses three different named loggers, one for each category of log
message generated by the Application Server.




com.sssw.srv.http.BaseLog4JLogger.HTTP is the logger for HTTP mes
sages.



com.sssw.srv.http.BaseLog4JLogger.Error is the logger for Error messages.



com.sssw.srv.http.BaseLog4JLogger.Trace is the logger for Trace messages.


Using the log4j.properties file you can send these log messages to any destination
with a supported

appender and format them anyway you please using a layout.


Example: Sending HTTP log messages to a rolling log file.

log4j.properties

# Set the http logger level to DEBUG

# set appender for rolling file appender

log4j.logger.com.sssw.srv.http.BaseLog4JL
ogger.HTTP=DEBUG, R_HTTP

# Configure the R_HTTP appender

log4j.appender.R_HTTP=org.apache.log4j.RollingFileAppender

log4j.appender.R_HTTP.File=Ext end_HTTP_Log.log

log4j.appender.R_HTTP.MaxFileSize=10000KB

# Keep 10 backup files

log4j.appender.R_HTTP.MaxBac
kupIndex=10

# Set the layout used for the R_HTTP appender

#Only thing we want is the message, it will contain everything

log4j.appender.R_HTTP.layout=org.apache.log4j.PatternLayout

log4j.appender.R_HTTP.layout.ConversionPattern=%m%n


Lines that begin with
# are comments. The first non
-
comment line sets the log level
for the
com.sssw.srv.http.BaseLog4JLogger.HTTP logger to DEBUG and tells Log4j to
use an appender named R_HTTP. Keep in mind that R_HTTP is an arbitrary name, it
could have been anything.





3

If you are running the application server as a service, the log files will be in the service’s root direct
ory.
Generally this is WINNT
\
system32

The

log level determines the minimum severity that a message must have to get
logged. This is more important for Application Logging. All Server Logging
messages are logged.
4



The remainder of the file configures the R_HTTP appender. The next non
-
comment

line specifies that R_HTTP should be a RollingFileAppender. A RollingFileAppender
backs up the existing log file and starts a new one when it reaches a certain size.
This is very useful for web server log files which can get ungainly otherwise. See the

Log4j javadoc for a complete description of the options available to you for the
RollingFileAppender. The next line says to use the name “Extend_HTTP_Log.log” for
the base file. The next line says to roll the file at 10 megabytes. The next line says
to

keep ten backup files. The last two lines deal with the layout. The HTTP
messages will already be in NCSA log format so we don’t want to add any additional
formatting. You may want to format the messages from the trace and error loggers.
Refer to the
Log4j javadoc for the PatternLayout for all of the layout options.

exteNd Application Logging using Log4j

You don’t have to do anything special to do Application Logging with Log4j in exteNd.
All you have to do is ensure that the log4j library (log4j
-
1.2.
7.jar as of this writing)
is in your AGCLASSPATH.


In your code, you simply create a new instance of the Logger class, giving it a name
that corresponds to the category of logging. The name you give your logger is
typically the fully qualified name of the

enclosing class. This allows you to control
your logging at the package and class level. The Logger class has all the logging
methods, debug(), info(), warn(), error() and fatal().


Example: Servlet using Log4j:


// log4j imports

import org.apache.log4j
.Logger;


public class LogExtractorServlet extends HttpServlet

{


// Create the logger, giving it the fully qualified name of this class


static Logger _logger = Logger.getLogger ( LogExtractorServlet.class );



/** Processes requests for both
HTTP GET and POST methods.


* @param request servlet request


* @param response servlet response


*/


protected void processRequest ( HttpServletRequest request, HttpServletResponse
response )


throws ServletException, java.io.IOException



{


_logger.debug ( "in" );




String action = request.getParameter ( "action" );


_logger.info ( "action = " + action );




4

Log4j supports five levels of logging. From lowest to highest they are DEBUG, INFO, WARN, ERROR
and FATAL. extendlog4.jar logs trace and http messages at the INFO level and error messages at the
ERROR level. By s
etting the log level to DEBUG you tell Log4j to log all of the messages.




try


{


if ( action == null )


displayForm ( reques
t, response );


else if ( action.equals ( "generate" ) )


handleGenerateReport ( request, response );


else if ( action.equals ( "download" ) )


handleDownl oadReport ( request, response );


els
e


throw new ServletException ( "Unknown action: " + action );


}


catch ( Exception e )


{


_logger.error ( e );


throw new ServletException ( e.getMessage() );


}


}


//


// Remain
der of the servlet code


//

}

Potential Conflict

As you can see, both Application Logging and Server Logging are straightforward in
exteNd. However, there is a potential conflict. You can only have one
log4j.properties file. The extendlog4j.jar file
includes a log4j.properties file. This
makes it easy to do Server Logging, but it can cause problems if you are doing both
Application and Server Logging. In this case, you will need to remove the
log4j.properties file from extendlog4j.jar and merge it w
ith your log4j.properties file
for your Application Logging. Place the combined log4j.properties file in your
AGCLASSPATH.

Log4j integration with the exteNd Server Console

The exteNd Server Console can be a useful GUI tool for viewing your log messages.
extendlog4j.jar includes a custom Log4j appender for logging to the exteNd Server
Console. The fully qualified name of the appender is:


com.sssw.srv.logging.log4j.appenders.ConsoleAppender


ConsoleAppender extends from the standard Log4j WriterAppender
and supports all
of WriterAppender’s options. Below is a sample log4j.properties file that uses the
ConsoleAppender. Note, you can use multiple appenders, so you could send your
output both to the ServerConsole using the ConsoleAppender and to a rolling
log file
using the RollingFileAppender.


Sample log4j.properties file using the ConsoleAppender:

# Set the http logger level to DEBUG

# set appender for console

log4j.logger.com.sssw.srv.logging.l og4j.BaseLog4JLogger.HTTP=DEBUG, Console

# Configure the Co
nsole Appender

log4j.appender.Console=com.sssw.srv.logging.l og4j.appenders.Consol eAppender

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=%d %
-
5p %c %x
-

%m%n