-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.py
37 lines (29 loc) · 1.08 KB
/
mongodb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import logging
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
__author__ = 'Simon Esprit'
class MongoDBConnection(object):
database = None
def __init__(self, collection, host="localhost", port=27017):
# collection in database where all messages will be stored
self.collection = collection
self.host = host
self.port = port
def connect(self):
try:
client = MongoClient(host=self.host, port=self.port)
self.database = client.syslog
return True
except ConnectionFailure as e:
logging.error("could not connect to mongod %s:%d" % (self.host, self.port))
return False
def store_message_data(self, json_data):
"""
Store SyslogData in a collection in the database.
:param json_data: json data that will be stored.
"""
if self.database is None:
logging.error("no database connection made yet")
return None
col = self.database[self.collection]
return col.insert_one(json_data)