Logs stored under Windows to MongoDB

Source: Internet
Author: User
Tags log4j

Requirements: The system to do distributed, the various modules of the log write files, for the operation to find the log to inconvenient, unified write to MongoDB, the error level of the log is stored separately.

My environment is Win7 64-bit system

The first step:

Download mongdb,: Https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl, I downloaded: win32/mongodb-win32-x86_ 64-2008plus-ssl-v3.0-latest.zip, download and extract to the D packing directory, the path is: D:\mongodb

Start, enter cmd, enter d:\mongodb\bin>mongod-dbpath=d:\mongodb\db

Step Two:

Download rockmongo,:http://rockmongo.com/downloads, download and extract to the D packing directory, the path is D:\rockmongo-on-windows, modify the configuration file D:\ rockmongo-on-windows\web\rockmongo\config.php:

$MONGO ["Servers"] [$i] ["mongo_db"] = "logdb";//default MONGO db to connect, works only if mongo_auth=false$mongo["servers "[$i] [" mongo_user "] =" admin ";//mongo authentication user name, works only if mongo_auth=false$mongo[" Servers "[$i] [" Mongo_pass "] =" admin ";//mongo authentication password, works only if Mongo_auth=false

mongo_db, fill in your DB name Mongo_user, define the account Mongo_pass, define the password

Start, double-click D:\rockmongo-on-windows\rockstart.bat, will pop in the browser login box, enter the account number and password, you can not select the DB name, into the MongoDB management interface

Step Three:

Build a Maven project and add dependencies:

<!--log4j MONGO--><dependency><groupid>log4j</groupid><artifactid>log4j</ Artifactid><version>1.2.16</version></dependency><dependency><groupid> org.mongodb</groupid><artifactid>mongo-java-driver</artifactid><version>2.7.2</ Version></dependency><dependency><groupid>org.log4mongo</groupid><artifactid> log4mongo-java</artifactid><version>0.7.4</version></dependency><!--log4j MONGO-- >

Establishment of Log4j.properties under the resources:

log4j.rootcategory=info,mongodberror,mongodbinfo#---------------------------------------# log4mongo  info#---------------------------------------log4j.logger.mongodbinfo=infolog4j.appender.mongodbinfo= org.log4mongo.mongodbappenderlog4j.appender.mongodbinfo.threshold=info#log4j.appender.mongodbwarn= Org.log4mongo.mongodbpatternlayoutappenderlog4j.appender.mongodbinfo.hostname= 192.168.1.197log4j.appender.mongodbinfo.port=27017log4j.appender.mongodbinfo.databasename= logdblog4j.appender.mongodbinfo.collectionname=infolog# the layout property is  required only if the mongodbpatternlayoutappender appender is used.#  If a custom patternparser and custom patternconverters are required  to log additional data,# the specified layout class must  extend mongodbpatternlayout. a conversionpattern property# should also be specified.log4j.appender.mongodbinfo.layout=org.log4mongo.mongodbpatternlayout#------------- --------------------------# log4mongo error#--------------------------------------- Log4j.logger.mongodberror=errorlog4j.appender.mongodberror= Org.log4mongo.mongodbappenderlog4j.appender.mongodberror.threshold=error#log4j.appender.mongodberror= Org.log4mongo.mongodbpatternlayoutappenderlog4j.appender.mongodberror.hostname= 192.168.1.197log4j.appender.mongodberror.port=27017log4j.appender.mongodberror.databasename= logdblog4j.appender.mongodberror.collectionname=errorlog# the layout property is  required only if the mongodbpatternlayoutappender appender is used.#  If a custom patternparser and custom patternconverters are required  to log additional data,# the specified layout class must  Extend mongodbpatternlayout.&nbSp a conversionpattern property# should also be  Specified.log4j.appender.mongodberror.layout=org.log4mongo.mongodbpatternlayout

To build a test class:

Import Org.apache.log4j.logger;public class Log4j4mongo {public static void main (string[] args) {Logger Logger = logger.ge Tlogger (Log4j4mongo.class), Logger.info ("Guo Degang said crosstalk ..."); Logger.warn ("Guo Degang with the leadership of the evening show some contradictions ..."); Logger.error ("Guo Degang's daughter-in-law, Yu Qian ....");}}

After executing the test class, you will see in the Rockmongo that there are Infolog and errorlog two collection in the Logdb library,Infolog has three records, andErrorlog has a record

Reference: http://www.educity.cn/wenda/365222.html

Logs stored under Windows to MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.