Log4j

Log4j is a logging utility based on Java. Machbase provides the method to input event logs into Machbase database with an application that uses Log4j, only modifies "log4j.properties" file and not changing the existing source code. For more information, please refer to Log4j website.

List of Contents

Standard Log4j API

Log4j API Description

Application Development

Verification of Library Installation

changing the existing source code. For more information, please refer to Log4j website.
Check whether machbase.jar, machbaseLog.jar, and log4j.jar files are in lib directory.

[mach@localhost log4j]$ cd $MACHBASE_HOME/lib
[mach@localhost lib]$ ls -l *.jar
-rw-rw-r-- 1 mach mach  78647 Jun 19 10:26 machbase.jar
-rw-rw-r-- 1 mach mach   1557 Jun 19 10:26 machbaseLog.jar
-rw-r--r-- 1 mach mach 378087 Jun 19 10:26 log4j.jar

** The version of log4j.jar file in the example is 1.2.4.

Makefile Guide

CLASSPATH=".:$(MACHBASE_HOME)/lib/machbase.jar:$(MACHBASE_HOME)/lib/machbaseLog.jar:$(MACHBASE_HOME)/lib/log4j.jar"


SAMPLE_SRC = Sample5Log.java


all: build


build:
    -@rm -rf *.class
    javac -classpath $(CLASSPATH) -d . $(SAMPLE_SRC)

run_sample5:
    java -classpath $(CLASSPATH) Sample5Log


clean:
    rm -rf *.class
[mach@localhost log4j]$ make
javac -classpath ".:/home/machbase/machbase_home/lib/machbase.jar:/home/machbase/machbase_home/lib/machbaseLog.jar:/home/machbase/machbase_home/lib/log4j.jar" -d . Sample5Log.java

Example

Setting of Log4j.properties

These are the basic settings to enter created logs into Machbase database. With the append protocol, it enters data at a high speed.

log4j.rootCategory=DEBUG,DB


log4j.debug=false


log4j.appender.DB=machbase.log.MachJDBCAppender
log4j.appender.DB.URL=jdbc:machbase://localhost:5656/mydb
log4j.appender.DB.driver=com.machbase.jdbc.driver
log4j.appender.DB.user=sys
log4j.appender.DB.password=manager
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.appender.DB.table=log4j_table
log4j.appender.DB.values=%d{yyyy-MM-dd HH:mm:ss SSS}^%C^%p^%m
log4j.appender.DB.seperator=^
log4j.appender.DB.bufferSize=10
log4j.appender.DB.interval=3

Values must match with the order of the table column, and each column should be separated by delimiter (^). The separator must not be included in the content of %m. The separator can be used by changing it to a string temporarily. The values must be matched with the delimiter of the separator.In the case of entering the date, the format has to be yyyy-MM-dd HH:mm:ss SSS .
Here, Interval is base unit of time (seconds) that is defined to send data periodically before the buffer is fully filled when logs are generated slowly. That is, in this case, it sends data to Machbase database every 3 seconds. When log data are inserted and the transmission buffer is filled before 3 seconds, then all the data will be sent to the server.

Create Table


[mach@localhost log4j]$ machsql
=================================================================
     Machbase Client Query Utility
     Release Version 3.5.0
     Copyright 2014, Machbase Inc. or its subsidiaries.
     All Rights Reserved.
=================================================================
Machbase server address (Default:127.0.0.1): 
Machbase user ID  (Default:SYS)
Machbase user password: MANAGER
MACHBASE_CONNECT_MODE=INET, PORT=5656
mach> create table log4j_table( dt datetime, logger varchar(10), level varchar(10), message varchar(32));
Created successfully.
mach>

Create Source Code

import java.util.*;
import org.apache.log4j.Logger;

import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;

public class Sample5Log
{
    private Logger logger = Logger.getLogger(Sample5Log.class);


    public Sample5Log() {
    }


    public void writeLog() {
        logger.debug("LOG debug");
    }


    public static void main(String[] args) throws Exception {


        int i;
        Sample5Log logTest = new Sample5Log();

        for(i=0; i<10000;i++)
        {
            logTest.writeLog();
        }

        LogManager.shutdown();

    }
}

Execute and Verify Results

[mach@localhost log4j]$ make
make
javac -classpath ".:/home/machbase/machbase_home/lib/machbase.jar:/home/machbase/machbase_home/lib/machbaseLog.jar:/home/machbase/machbase_home/lib/log4j.jar" -d . Sample5Log.java Sample6Xml.java
[mach@localhost log4j]$ make run_sample5
make run_sample5
java -classpath ".:/home/machbase/machbase_home/lib/machbase.jar:/home/machbase/machbase_home/lib/machbaseLog.jar:/home/machbase/machbase_home/lib/log4j.jar" Sample5Log
[mach@localhost log4j]$ machsql
=================================================================
     Machbase Client Query Utility
     Release Version 3.5.0
     Copyright 2014, Machbase Inc. or its subsidiaries.
     All Rights Reserved.
=================================================================
Machbase server address (Default:127.0.0.1): 
Machbase user ID  (Default:SYS)
Machbase user password: MANAGER
MACHBASE_CONNECT_MODE=INET, PORT=5656
mach> select * from log4j_table limit 10;
DT                              LOGGER      LEVEL       MESSAGE                           
-----------------------------------------------------------------------------------------------
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        
2015-04-09 10:31:05 917:000:000 Sample5Log  DEBUG       ,LOG debug                        

[10] row(s) selected.
mach> exit
[mach@localhost log4j]$

Example of XML

Settings of Log4j.xml

Create the setting values as xml files in order to enter the logs into Machbase database.
The name of XML file is log4j.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="DB" class="machbase.log.MachJDBCAppender">
            <param name="URL" value="jdbc:machbase://localhost:5656/mydb"/>
            <param name="driver" value="com.machbase.jdbc.driver"/>
            <param name="user" value="sys"/>
            <param name="password" value="manager"/>
            <param name="table" value="log4j_table"/>
            <param name="values" value="%d{yyyy-MM-dd HH:mm:ss SSS}^%C^%p^,%m"/>
            <param name="seperator" value="^"/>
            <param name="bufferSize" value="10"/>
            <param name="interval" value="3"/>
    </appender>



    <root>
            <level value="DEBUG" />
            <appender-ref ref="DB" />
    </root>
</log4j:configuration>

Values must be matched with the order of the table column and be separated by the delimiter (^) of each column. The separator must not be included in the content of %m. The separator can be used by changing it into a string temporarily. The values must be matched with the delimiter of the separator. In the case of entering the date, the format has to be yyyy-MM-dd HH:mm:ss SSS.
Here, Interval is base unit of time (seconds) that is defined to send data periodically before the buffer is fully filled when logs are generated slowly. That is, in this case, it sends data to Machbase database every 3 seconds. When log data are inserted and the transmission buffer is filled before 3 seconds, then all the data will be sent to the server.

Create Table

[mach@localhost log4j]$ machsql
=================================================================
     Machbase Client Query Utility
     Release Version 3.5.0
     Copyright 2014, Machbase Inc. or its subsidiaries.
     All Rights Reserved.
=================================================================
Machbase server addr (Default:127.0.0.1): 
Machbase user ID  (Default:SYS)
Machbase user password: MANAGER
MACH_CONNECT_MODE=INET, PORT=5656
mach> create table log4j_table( dt datetime, logger varchar(10), level varchar(10), message varchar(32));
Created successfully.
mach>

Create Source Code

It creates the Java code that send the log data by using the XML setting file. The name of file is Sample6Xml.java.

import java.util.*;
import org.apache.log4j.Logger;

import org.apache.log4j.LogManager;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

public class Sample6Xml
{
    private static final String FILE_PATH = "log4j.xml";
    private Logger logger = Logger.getLogger(Sample6Xml.class);


    public Sample6Xml() {
        DOMConfigurator.configure(FILE_PATH);
    }

    public void writeLog() {
        logger.debug("xml conf debug ");
    }


    public static void main(String[] args) throws Exception {

        int i;
        Sample6Xml logTest = new Sample6Xml();

        for(i=0; i<10000;i++)
        {
            logTest.writeLog();
        }

        LogManager.shutdown();

    }
}

Execute and Verify Results

[mach@localhost log4j]$ make
make
javac -classpath ".:/home/machbase/machbase_home/lib/machbase.jar:/home/machbase/machbase_home/lib/machbaseLog.jar:/home/machbase/machbase_home/lib/log4j.jar" -d . Sample5Log.java Sample6Xml.java
[mach@localhost log4j]$ make run_sample6
make run_sample6
java -classpath ".:/home/machbase/machbase_home/lib/machbase.jar:/home/machbase/machbase_home/lib/machbaseLog.jar:/home/machbase/machbase_home/lib/log4j.jar" Sample6Xml
[mach@localhost log4j]$ machsql
=================================================================
     Machbase Client Query Utility
     Release Version 3.5.0
     Copyright 2014, Machbase Inc. or its subsidiaries.
     All Rights Reserved.
=================================================================
Machbase server address (Default:127.0.0.1): 
Machbase user ID  (Default:SYS)
Machbase user password: MANAGER
MACH_CONNECT_MODE=INET, PORT=5656
mach> select * from log4j_table limit 10;
DT                              LOGGER      LEVEL       MESSAGE                           
-----------------------------------------------------------------------------------------------
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 355:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 354:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
2015-04-09 10:26:20 354:000:000 Sample6Xml  DEBUG       ,xml conf debug                   
[10] row(s) selected.
mach>

results matching ""

    No results matching ""