我正在尝试将日志从Python写入Mongo DB。我的代码正在执行并将数据库创建为“ DSLogSummary”,但没有将条目写入集合:
from apscheduler.schedulers.blocking import BlockingScheduler
from pymongo import MongoClient
from pymongo import ASCENDING
from pytz import utc
import datetime
client = MongoClient()
db = client.DSLogSummary
collection = db.ds_jobLogEntry
collection.ensure_index([("timestamp", ASCENDING)])
def log(msg):
entry = {}
entry['timestamp'] = datetime.datetime.utcnow()
entry['msg'] = msg
collection_id = collection.insert_one(entry).inserted_id
def job_test():
print ("Hello World")
log('Job executed')
jobstores = {
'mongo': {'type': 'mongodb'},
}
sched = BlockingScheduler()
sched.configure(jobstores=jobstores, timezone=utc)
sched.add_job(job_test, trigger='cron', month='*', hour='*', minute = '*', second='*/5')
sched.start()
输出为: 你好,世界 你好,世界 你好,世界 你好,世界 你好,世界 你好世界