|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
The LogWriter takes care of deciding where to
log the messages - the storage destination. It redirects all
the log out and log error messages to the storage destination specified
in the configuration details. The destination
can be a file, console, database, socket etc.,
If your storage destination is a file, then you can use the default
implementation FileLogWriter available in this framework. In case, you
desire to direct the log messages to some other destination, you can do
so by giving your own implementation
for the interface LogWriter.
Various properties of the LogWriter namely ClassName and Custom properties like MaxLineCount, ArchiveStatus, MaxFileCount etc., can be modified at runtime.
Writing Your Own LogWriter |
Reference Implementation |
To explain you how you can write your own Logwriter, we provide below
an example which explains how implementation can be given for directing
the messages to the DataBase. In this implementation, the log messages
are inserted to tables. Keeping
this implementation as a reference, you can write your own LogWriter,
by implementing the interface LogWriter or extending the abstract class
AbstractLogWriter.
For ease of reference, detailed explanation has been provided alongside
the code snippet itself.
package com.adventnet.afp.log;
//Product imports import com.adventnet.afp.log.*; import java.io.*; import java.sql.*; import java.util.*; // This class implements the interface LogWriter.
This class will move all the log messages into the database. The configuraton
details are given in the file logging.xml which can be read through LogConfigReader.
In the DataBase, the log messages will be moved to their respective Tables
bearing the names serverout, servererr etc.,
// The following tag is added in the file
logging.xml. This is for getting information about the database (i.e. Writer).
The information about database is required to get connection to the database
and to log the messages into the database. The InstanceName of this
DataBaseWriter is given as "SERVEROUT". The Loggers associated with this
DatabaseWriter should also have the same "SERVEROUT" as the WriterInstanceName
in order to log the messages into the database.
<LOG_WRITER>
public class DatabaseWriter extends AbstractLogWriter
// To get the properties from the class LogWriterProperties
and to set all the properties.
{ String str = null; //To get the custom properties from the LogWriterProperties.
//To get the Log Writer Instance from the LogWriterProperties.
// DataBase properties like URL, Driver name,
password and user name are given in the file logging.xml. In order to get
DataBase connection, get those properties
//To get the Password for connecting to the database.
//To get the User name for connecting to
the database.
this.user
= str;
//To get the connection to the database using the database properties like user name, URL and password conn=DriverManager.getConnection(url,user,pass);
//To create the table with the corresponding table names. String createString="create table "+tableName+" ( message varchar (250))"; //To insert the log messages into the respective
table name.
//To create the statement object for sending SQL
statements into the DataBase.
//To execute a SQL string
//To create the PreparedStatement object for sending
the SQL statements.
}
// This method returns all the messages that are
placed in the memory.
// To Flush the messages in the buffer into their
respective streams
{ ee.printStackTrace(); }
// To write the error messages and the stack traces
into the database line by line.
{ //To Set the message to be displayed in the first
parameter.
//To store the exception stack traces line by
line in order to get the line count.
//To get the complete Stack trace.
//To get the stack trace line by line
StringTokenizer stkr=new StringTokenizer(tmpstr,System.getProperty("line.separator"));
//To get the message in each line and to execute the SQL string for inserting each line into the respective table.
prst.setString(1,stkr.nextToken());
} } } catch(Exception ec) { ec.printStackTrace();
//To Write the output messages into the database.
try
//To insert the messages into their respective tables in the database by executing the SQL statement.
prst.setString(1,message);
// To close the database connections that were
opened.
//To close the connections which were opened for
writing log messages into the database.
} |
Integrating Your LogWriter |
The above code snippet explained you how to create a LogWriter. You need to follow the steps given below for integrating it with the Logging Mechanism. You can add the LogWriters either in the configuration details or at runtime programmatically.
Adding in the configuration details
Step1
<LOGGER>
<ClassName>com.adventnet.afp.log.LoggerImpl</ClassName>
<InstanceName>TestLogger</InstanceName>
<WriterInstanceName>SERVEROUT</WriterInstanceName>
<LogLevel>3</LogLevel>
<Properties>
<Property>
<Key>DisplayName</Key>
<Value>TEST</Value>
</Property>
</Properties>
</LOGGER>
Step 3
The following are the steps to be followed to send the messages to be
logged, to the LogWriter.
Creating LogWriter at Runtime
Step1
Step 3
Method Summary | |
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Method for adding PropertyChangeListener to this LogWriter instance. |
void |
close()
For closing all the writer instances. |
void |
err(java.lang.String message,
java.lang.Throwable Exception)
For logging the error messages with the throwable Exceptions. |
void |
flush()
For flushing out the current buffer. |
java.lang.String |
getInstanceName()
Method to get the instance name. |
void |
init(LogWriterProperties logWriterProperties)
For initializing implementation class with required properties. |
void |
out(java.lang.String message)
Derived classes for logging messages. |
boolean |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Used to remove the registered PropertyChangeListener. |
Method Detail |
public void out(java.lang.String message)
message
- String to be loggedpublic void err(java.lang.String message, java.lang.Throwable Exception)
Exception
- throwablemessage
- the error message to be loggedpublic void flush() throws LogException
LogException
- if an error occurs during flushing of contentspublic void close() throws LogException
LogException
- if an error occurs while closing the writerpublic void init(LogWriterProperties logWriterProperties) throws LogException
logWriterProperties
- The Writer properties are obtainedLogException
- if an error occurs during initializationpublic java.lang.String getInstanceName()
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- PropertyChangeListener to be addedpublic boolean removePropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- reference of PropertyChangeListener to be removed
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |