com.adventnet.afp.log
Interface LogWriter

All Known Implementing Classes:
AbstractLogWriter

public interface LogWriter

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> 
        <ClassName>com.adventnet.afp.log.DatabaseWriter</ClassName>
        <InstanceName>SERVEROUT</InstanceName>
        <PROPERTIES>
            <PROPERTY>
                <Key>TableName</Key>
                <Value>serverout</Value>
            </PROPERTY>
            <PROPERTY>
                <Key>URL</Key>
                <Value>jdbc:mysql://localhost/WebNmsDB</Value>
            </PROPERTY>
            <PROPERTY>
                <Key>DriverName</Key>
                <Value>org.gjt.mm.mysql.Driver</Value>
            </PROPERTY>
            <PROPERTY>
                <Key>UserName</Key>
                <Value>root</Value>
            </PROPERTY>
            <PROPERTY>
                <Key>Password</Key>
                <Value>public</Value>
            </PROPERTY>
        </PROPERTIES> 
    <LOG_WRITER>

public class DatabaseWriter extends AbstractLogWriter
{
    Statement stmt=null;
    PreparedStatement prst=null;
    private String url=null;
    private String driverName=null;
    private String pass=null;
    private String user=null;
    private Connection conn=null;
    private String logWriterInstanceName=null;
    private String tableName=null;
 

// To get the properties from the class LogWriterProperties and to set all the properties.
    public void init(LogWriterProperties prop)

    {

        String str = null;

//To get the custom properties from the LogWriterProperties.
//To get the table name for the Log Writer (where the log messages are to be written in the database).
        if((str = prop.getCustomProperty("TableName")) != null)
        {
            this.tableName = str;
        }

//To get the Log Writer Instance from the LogWriterProperties.
        if((str = prop.getInstanceName()) != null)
        {
            this.logWriterInstanceName = str;
        }

// 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 URL properties .
        if((str = prop.getCustomProperty("URL")) != null)
        {
            this.url = str;
        }
        //To get the Driver name properties.
        if((str = prop.getCustomProperty("DriverName")) != null)
        {
            this.driverName = str;
        }

//To get the Password for connecting to the database.
        if((str = prop.getCustomProperty("Password")) != null)
        {
            this.pass = str;
        }

 //To get the User name for connecting to the database.
        if((str = prop.getCustomProperty("UserName")) != null)
        {

            this.user = str;
        }
        try
        {
            Class.forName(driverName);

//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.
           String insertString="insert into "+tableName+" values(?)";

//To create the statement object for sending SQL statements into the DataBase.
          stmt=conn.createStatement();

//To execute a SQL string
           stmt.executeUpdate(createString);

//To create the PreparedStatement object for sending the SQL statements.
            prst=conn.prepareStatement(insertString);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
 

// This method returns all the messages that are placed in the memory.
    public synchronized  void flush () 
    {

// To Flush the messages in the buffer into their respective streams
        try
        {
           conn.commit();
        }catch(Exception ee)

        {

           ee.printStackTrace();

       }
    }

// To write the error messages and the stack traces into the  database line by line.
    public synchronized  void err(String message ,Throwable exception) 
    {
        try

        {

//To Set the message to be displayed in the first parameter.
            prst.setString(1,message);
            //To execute a SQL string.
            prst.executeUpdate();
            if(exception !=null)
            {

//To store the exception stack traces line by line in order to get the line count.
                StringWriter sw=new StringWriter();
                PrintWriter pw=new PrintWriter(sw);
                Print the stack Trace.
                    exception.printStackTrace(pw);

//To get the complete Stack trace.
                String tmpstr=sw.getBuffer().toString();

//To get the stack trace line by line

               StringTokenizer stkr=new StringTokenizer(tmpstr,System.getProperty("line.separator"));
                while(stkr.hasMoreTokens())
                { 

//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());
                    prst.executeUpdate();

                }

            }

        }

       catch(Exception ec)

        {

           ec.printStackTrace();
        }
    } 
 

//To Write the output messages into the database.
    public synchronized  void out(String message) 
    {

        try
            {
 

//To insert the messages into their respective tables in the database by executing the SQL statement.

               prst.setString(1,message);
                prst.executeUpdate();
            }catch(Exception ece)
            {
                ece.printStackTrace();
            }
    }
 

// To close the database connections that were opened.
    public void close()
    {
        try
        {
 

//To close the connections which were opened for writing log messages into the database.
            conn.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }


 
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

Step 2 The Loggers for this writer can be given as

<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.
 

Now, all the log messages will be moved to the specified LogWriter (in this example, DataBase).

Creating LogWriter at Runtime

Step1

Step2


Step 3

Step 4


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

out

public void out(java.lang.String message)
Derived classes for logging messages. The output messages or any informative messages can be moved to their respective file using this method. By default the messages passed to this method will be moved to serverout.txt,serverout1.txt etc.
Parameters:
message - String to be logged

err

public void err(java.lang.String message,
                java.lang.Throwable Exception)
For logging the error messages with the throwable Exceptions. The error messages by default are moved to the file namely servererr.txt, servererr1.txt etc. The line count is made for the stack trace which will be very helpful in maintaining the line count when buffering is enabled.
Parameters:
Exception - throwable
message - the error message to be logged

flush

public void flush()
           throws LogException
For flushing out the current buffer. This method forms the basis for the file operations.When the buffer size is 0, the messages passed to the methods out and err will be flushed into their respective log files by this method. When buffering is enabled, depending upon the buffer type, the messages are flushed at a stretch into their respective files.
Throws:
LogException - if an error occurs during flushing of contents

close

public void close()
           throws LogException
For closing all the writer instances. This can also be used to clean up all the resources. The log files are written into the file that is opened using PrintStream. When the next log file is to be opened, then the currently opened file stream should be closed.
Throws:
LogException - if an error occurs while closing the writer

init

public void init(LogWriterProperties logWriterProperties)
          throws LogException
For initializing implementation class with required properties. The Custom properties are passed through this method from the LogWriterProperties.The properties are taken from the LogWriterProperties
Parameters:
logWriterProperties - The Writer properties are obtained
Throws:
LogException - if an error occurs during initialization

getInstanceName

public java.lang.String getInstanceName()
Method to get the instance name. The Log writer Instance name is passed as one of the log writer properties. For every Log Writer, the Instance is passed.
Returns:
The log Writer Instance is returned

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Method for adding PropertyChangeListener to this LogWriter instance. This can be used to get notification when any one of the property changes.
Parameters:
listener - PropertyChangeListener to be added

removePropertyChangeListener

public boolean removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Used to remove the registered PropertyChangeListener. On successful removal, this method will return true.
Parameters:
listener - reference of PropertyChangeListener to be removed
Returns:
boolean value to indicate the result of the operation.


Copyright (c)AdventNet Inc., 1996-2004