Techniques for logging
This article talks about several different ways to do some logging with log4j. It also describes how to configure your own custom logging.
- Use SystemOut actions in an Action list.
- Use Debug Tracing builder
- Use the com.bowstreet.appserver.logmanager package
- Create your own custom logging
Use SystemOut actions in an Action list
This technique is good for logging messages from an Action List builder. It's a quick way to check the value of a variable or to log a message to help trace where you are in the application flow. You can also turn off the logging for all SystemOut actions in your application by setting a single property. This is much better than having to got back and comment out or delet many calls to System.out.println() in your code.
The log entries are written to SystemOut.log and WEB-INF\logs\general.txt
In your action list, click the button to bring up the picker. Select Special > SystemOut
Enter a message or use the picker to select an indirect reference that you want to log a value for.
To diable the SystemOut actions in all the actions lists for your Web Application, edit the log4j.properties file and change the following line from
Use Debug Tracing builder
Use the Debug Tracing builder to trace actions or the values of indirect references. You can add as many Debug Tracing builders to the model as you like and trace as many actions or variable values as you like. The logging is controled by an entry in the log4j.properties file. So you can easily turn the logging on and off by setting a single property. Keep in mind when setting that property that it control the logging from Debug Tracing builders in all the models for that Web Application.
The logging is written to \WEB-INF\logs\debugTracing.txt
Add a Debug Tracing builder to the model.
Select to trace all actions.
Go to the "Additional Debug Output" input field. Select an indirect reference to log using the picker
Edit the log4j.properties file and change the following line from
Use the com.bowstreet.appserver.logmanager package
Use this package to log messages from your Portlet Factory Linked Java Objects. You can add calls to your code that use this package and log messages and variable values to SystemOut.log and \WEB-INF\logs\general.txt.
This logging is written to SystemOut.log and WEB-INF\logs\general.txt.
Add a call to such as the following to log a message:
LogEvent.writeLogEntry(Severity.ALL, Component.ALL,"message from logmanager");
This property in the log4j.properties file controls when your messages get
# this corresponds to the previous Event drivers
Whether or not a message is logged depends on the severity you use in the writeLogEntry() method and the severity set for the property in log4j.properties.
For example with the default setting of WARN, messages that have a severity
of ALL, SEVERE, ERROR and WARNING will be logged. Messages that have a
severity of DEBUG or INFORMATION will not be logged.
Create your own custom logging
The last technique involves creating your own logging class and appender information. The word "class" in this case does not refer to a Java class. The word class in this case is specific to logging. The advantage of creating your own logging classes is that you can make your logging more granular. You can have a logging class for each Java class you want to debug or you can have a logging class for each package or use your own design.
The output of your logging can be logged to any file you choose. This is configured in the log4j.properties file. Add the lines below and simply change the value of the property "log4j.appender.MyLogging.File".
Your custom appender sends its ouput to myLogging.txt
# Truncate the logging file if it aleady exists.
# Appender ModelActions uses the PatternLayout.
log4j.appender.MyLogging.layout.ConversionPattern=*-- TIME: [%d] --* %nCategory: %c%nPriority: %p%nThread: %t%nMsg: %m%n%n%n
In your code, define a logger.
static Logger logger = Logger.getLogger("com.ibm.test.LoggingTest");
Then call methods on the logger in order to write log entries. You can call debug(), info(), warn(), error() or fatal() to log messages of different severities.
logger.debug("In setValue and the value is "+value);
Lastly, you will need to define the logging class in the log4j.properties file. Since the name of the logging class above is "com.ibm.test.LoggingTest" and the appender is defined as "MyLogging", you will need to add the following line:
A sample Java Class and model are attached to demonstarte the custom logging. Add the lines described above to try the sample.