将loguru生成的序列化错误日志推送到mongodb/redis/fluentd等远程存储的包
logger-master的Python项目详细描述
记录器主机
用于将loguru生成的序列化错误日志推送到远程存储的包。
说明:
将loguru生成的序列化错误日志推送到远程存储(包括mongodb/redis/fluentd)的包。在
使用方法:
使用MongoDB
- 参数: 在
importjsonfrompymongoimportMongoClientfromlogger_master.loggerimportMongoLogger# pushing log to mongodb instancemongo_uri='mongodb://root:root@localhost:27017/?authMechanism=SCRAM-SHA-1'mongo_instance=MongoClient(mongo_uri)# basic usage for pushing log with specific database and collectionlogger=MongoLogger(mongo_instance=mongo_instance,mongo_db='my_log',mongo_collection='my_log_collection')# pushing serialize log to ./my_log/log_filelogger=MongoLogger(mongo_instance=mongo_instance,mongo_db='my_log',mongo_collection='my_log_collection',serialize=True,log_path='./my_log/log_file',rotation='1 week')# disable display log on screenlogger=MongoLogger(mongo_instance=mongo_instance,mongo_db='my_log',mongo_collection='my_log_collection',log_path='./my_log/log_file',terminal_displayed=False)# pushing serialized log with custom formatdefcustom_function(serialized_data):ifnotisinstance(serialized_data,dict):serialized_data=json.loads(serialized_data)serialized_data.update({'new_key':'new_value'})returnserialized_datalogger=MongoLogger(mongo_instance=mongo_instance,mongo_db='my_log',mongo_collection='my_log_collection',custom_func=custom_function)try:100/0exceptExceptionase:logger.error(str(e))
使用Redis
- 参数: 在
使用Fluentd
- 参数: 在
importjsonfromlogger_masterimportFluentdLogger# pushing log log to fluentd# basic usage for pushing log data to fluentdlogger=FluentdLogger(hostname='localhost',port=24224,key_prefix='mongo')# pushing serialize log to ./my_log/log_filelogger=FluentdLogger(hostname='localhost',port=24224,key_prefix='mongo',serialize=True,log_path='./my_log/log_file',rotation='1 week')# disable display log on screenlogger=FluentdLogger(hostname='localhost',port=24224,key_prefix='mongo',log_path='./my_log/log_file',terminal_displayed=False)# pushing serialized log with custom formatdefcustom_function(serialized_data):ifnotisinstance(serialized_data,dict):serialized_data=json.loads(serialized_data)serialized_data.update({'new_key':'new_value'})returnserialized_datalogger=FluentdLogger(hostname='localhost',port=24224,key_prefix='mongo',custom_func=custom_function)try:1/0exceptExceptionase:logger.error(msg=str(e))
您还可以使用fluent-plugin-mongo将日志推送到fluentd和mongodb。
给我买杯咖啡,如果你喜欢的话!
- 项目
标签: