OFBiz Tutorial: Store OFBiz logs in NoSql-Mongo DB

Servers generate a large number of events (i.e. logging) that contain useful information about their operation including errors, warnings, and users behavior. By default, most servers, store these data in plain text log files on their local file systems. While plain-text logs are accessible and human-readable, they are difficult to use, reference, and analyze without holistic systems for aggregating and storing these data. —www.mongodb.org


This tutorial is the first in a multi-post tutorial series about utilizing MongoDB functionality for your OFBiz-based digital commerce solution. In today’s post we’ll show you how to store OFBiz logs in a NoSql-Mongo DB.

Why use a NoSQL database to store data logs?

Storing data logs in NoSQL databases such as Cassandra, CouchDB and MongoDB offers numerous advantages, including:

  • Traditional logging and I/O operations are often a bottleneck for capacity of applications
  • Ability to scale to support more data and concurrent threads
  • Logs don’t need to be archived, compressed or deleted on a regular basis as it happens with logs stored in text files
  • Logs in a NoSql database can easily be searched, filtered and analyzed

In this tutorial, we’ll show you how to store OFBiz logs in a NoSql database, specifically, MongoDB.

The following steps will describe how to setup MongoDB, Apache OFBiz, the Java Driver for MongoDB and the NoSql adapter for Apache Log4J, the logging framework used by OFBiz.

To store OFBiz logs in a NoSql database, follow these steps:
  1. Pre-requisites:
  1. Download the Mongo Java Driver (version 2.12.4) and copy it in the framework/base/lib directory
  2. Download “apache-log4j-2.1-bin.tar.gz” here. Extract the file and copy “log4j-nosql-2.1.jar” file from the extracted folder into framework/base/lib directory
  3. Start the MongoDB server by using command “mongod
  4. After successfully starting the MongoDB server, start Mongo Shell by issuing the command “mongo
  5. Create a new database or collection using the command “use ofbiz” (this command will determine whether or not the collection named “ofbiz” has already been created; if not, it will create the collection)
  6. Create a new database user for MongoDB using the following command:
j = {user:"ofbiz", pwd:"ofbiz", roles:["readWrite"]}
db.addUser(j)
  1. Now apply the following patch to OFBiz to configure the settings related to log4j: it will add a new appender to log4j, as shown below:
Index: framework/base/config/log4j2.xml
===================================================================
--- framework/base/config/log4j2.xml	(revision 1651591)
+++ framework/base/config/log4j2.xml	(working copy)
@@ -33,10 +33,15 @@
             </Policies>
             <DefaultRolloverStrategy max="3"/>
         </RollingFile>
+        <NoSql name="databaseAppender">
+            <MongoDb databaseName="ofbiz" collectionName="applicationLog" server="localhost"
+                       username="ofbiz" password="ofbiz" />
+        </NoSql>
         <Async name="async">
             <AppenderRef ref="ofbiz"/>
             <AppenderRef ref="stdout"/>
             <AppenderRef ref="error"/>
+            <AppenderRef ref="databaseAppender"/>
         </Async>
     </Appenders>
     <Loggers>
  1. Next, build and start an OFBiz trunk by using “./ant build start“. At this point, logs should be added to the “ofbiz” Mongo database in the collection named “applicationLog”.
  2. To check the logs that have been inserted into the MongoDB, you can use any one of the following commands:
db.applicationLog.find()

db.applicationLog.find().pretty()
 
db.applicationLog.find({},{ISODate:1, loggerName:1, message:1, _id:0})

Thanks for checking out our OFBiz Tutorial Series. In an upcoming blog we will describe how to create a screen in OFBiz and display the logs from MongoDB there.

About HotWax Systems

HotWax Systems, creator of Apache OFBiz Accelerator, develops, implements, and supports fully customizable, enterprise-class, digital commerce platforms powered by Apache OFBiz. For more information about how to leverage the power and flexibility of OFBiz for your business, contact us for a free consultation.

Photo credit: neil cummings / Foter / CC BY-SA


DATE: Apr 29, 2015
AUTHOR: Arun Patidar
OFBiz Tutorials, Enterprise eCommerce, OFBiz Development, Apache OFBiz, Enterprise ERP, Apache OFBiz 13.07, OFBiz