|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
The Logger interface available in the Logging Framework, decides which messages are to be logged based on the LogLevel specified. It provides various convenient methods for the Logging mechanism. It passes the output and error messages to the LogWriter in accordance with the specified LogLevel.
Through the methods available in Logger, the Logger Properties such as LogLevel, DisplayName and Class Name can be modified at runtime.
Writing Your Own Logger |
Reference Implementation |
The following code snippet explains how implementation can be given
for writing your own logger. Keeping this as reference, you may give your
own implementation. Detailed explanation has been provided alongside the
code snippet itself for ease of reference.
package test;
import java.util.*; import java.text.*; import com.adventnet.afp.log.LogWriter; import com.adventnet.afp.log.LoggerProperties; import com.adventnet.afp.log.AbstractLogger; // An example implementation class of the Logger interface explaining how you can have your own custom properties and handle them in accordance with your needs. public class ExampleLogger extends AbstractLogger {
// The default logging level
of the logger.
// The Writer used by the Logger.
// Some custom properties for
a Logger (specified in the logging.xml file).
// Initializing the various
parameters of the Logger. All default parameters and the custom properties
will be set by this method. The respective Writer is mentioned for every
Logger properties.
// Setting the LogLevel.
// Setting the writer.
// Setting the custom properties
// To get the custom property(needTimeStamp).
If it is true, the message will have a timestamp.
} } // To get the instanceName
of the Logger.
// Method to write the output
message. The message will be logged only if its level is less than or equal
to the level of the Logger. The message will have TimeStamp prepended if
needTimeStamp is true.
{
// Method to log an error message. The message will be logged only if its level is less than or equal to the level of the Logger. The message will have TimeStamp prepended if needTimeStamp is true. Since there is no exception trace, it is forwarded to err(string,exception,level) with exception as null. public void err(String message, int level) {
// To log an error message.
The message will be logged only if its level is less than or equal to the
level of the Logger. The message will have TimeStamp prepended if needTimeStamp
is true.
}
// To set the LogLevel. public void setLoggingLevel(int level) { this.logLevel = level; }
// To get the LogLevel.
// To format the messages in the required format. // Here, the custom property needTimeStamp is used, based on which formatting is done. public String formatMessage(String message, int level) { StringBuffer str = new StringBuffer();
// If time stamp is needed, it can be added to the message. if (needTimeStamp) { SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm aaa"); str.append(dateFormat.format(new
Date()));
// To append the InstanceName
in any format. (In Lowercase in this example).
// Finally, append the string
and return it.
|
Integrating Your Logger |
The above code snippet explained you how to create your own Logger. You need to follow the steps given below for integrating it with the Logging Mechanism. You can add the Loggers either in the configuration details or at Runtime programmatically.
Adding in Configuration details
Step 1
Write a class implementing the AbstractLogger. You can give your own implementation for the methods available in the interface. You may add new methods, if need be.
Step 2
Add a new Logger in the source from where LogConfigReader would read the configuration details. In this framework, by default, the LogConfigReader reads the details from a file. The tags which you find below, can be added to the logging.xml, (as Logger), if you wish to use the default setup. This Logger's InstanceName is given as "SERVEROUT". It will be associated to the Writer which has the same InstanceName (i.e) SERVEROUT.
<LOGGER>
<ClassName>test.ExampleLogger</ClassName>
<InstanceName>TestLogger</InstanceName>
<WriterInstanceName>SERVEROUT</WriterInstanceName>
<LogLevel>3</LogLevel>
<Properties>
<Property>
<Key>----</Key>
//Specify the Custom property key
<Value>---</Value> // property value
</Property>
.
//Enter all the custom properties here.
</Properties>
</LOGGER>
Step 3
The following steps are to be followed to implement Logging through the Logger created by you
Get the instances of Configuration reader and Configuration writer.
Get the instance of the LogFactory and initialize it.
Get the Logger( added by you) .
Creating Your Logger at Runtime
Step 1
Method Summary | |
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
This method is used to add the PropertyChangeListener to the Logger instance. |
void |
err(java.lang.String message,
int level)
The err method is used to display error messages. |
void |
err(java.lang.String message,
java.lang.Throwable exception,
int level)
The err method is used to display error messages. |
java.lang.String |
getInstanceName()
The getLoggerInstanceName method is used to get the LoggerInstanceName of the Logger. |
int |
getLogLevel()
The getLogLevel method is used to get the current LogLevel of the particular
Logger. |
void |
init(LoggerProperties loggerProperties,
LogWriter logWriter)
The init method is used to initialize a Logger.The properties of the
Logger and the LogWriter are passed to this method. |
void |
out(java.lang.String message,
int level)
The out method is used to display some output messages or debugging messages. |
boolean |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
This method is used to remove the PropertyChangeListener from the Logger instance. |
void |
setLogLevel(int level)
The setLogLevel method is used to set the LogLevel of a Logger at runtime. |
Method Detail |
public void init(LoggerProperties loggerProperties, LogWriter logWriter) throws LogException
init
method is used to initialize a Logger.The properties of the
Logger and the LogWriter are passed to this method. The Logger properties include
Logging level, Log Instance name , Display name and Log Class nameloggerProperties
- a LoggerProperties
having the properties of the LoggerlogWriter
- a LogWriter
valueLogException
- if an error occurs during initializationpublic void out(java.lang.String message, int level)
out
method is used to display some output messages or debugging messages.
The message will be logged if and only if the
message's logging level is less than or equal to the logging level of the Logger specified in the
LoggerProperties if the logging level is not set at runtime.message
- a String
having the message that has to be loggedlevel
- an int
specifying the level of the log messagepublic void err(java.lang.String message, int level)
err
method is used to display error messages. The message will be logged only
when the message's logging level is less than or equal to the logging level of the Logger mentioned
in the Logger Properties if the level is not set at runtime.message
- a String
valuelevel
- an int
valuepublic void err(java.lang.String message, java.lang.Throwable exception, int level)
err
method is used to display error messages. The message and the exception
will be logged if and only if the logging for that Logger is enabled and the message's logging
level is less than or equal to the logging level of the Logger mentioned in the Logger Properties
if the level is not set at runtime. The stack trace is passed along
with the error messages.message
- a String
valueexception
- a Throwable
valuelevel
- an int
valuepublic void setLogLevel(int level)
setLogLevel
method is used to set the LogLevel of a Logger at runtime.
If this method is not invoked at runtime, the logging level is taken from the Logger propertieslevel
- an int
value specifying the new LogLevel.public int getLogLevel()
getLogLevel
method is used to get the current LogLevel of the particular
Logger. If the setLogLevel
is not invoked at runtime, then logging level is
obtained from the Logger properties. If the method is invoked , then the
level which is set using the method setLogLevel
is retreived .int
value denoting the current LogLevel.public java.lang.String getInstanceName()
getLoggerInstanceName
method is used to get the LoggerInstanceName of the Logger.
The key name for the Logger for example MISCOUT,MISCERR etc will be retreived from this method.String
valuepublic void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- PropertyChangeListener to listen for any property change in this Loggerpublic boolean removePropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- PropertyChangeListener which was added to the listener
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |