Package: logger-master
logger-masterA package for pushing serialized error log which generated byloguruto remote storage.Description:A package for pushing serialized error log which generated byloguruto remote storage (including mongodb / redis / fluentd).How To Use:With MongoDBparameters:mongo_instance(object): mongodb instancemongo_db(str): database for storing log datamongo_collection(str): collection for storing log dataterminal_displayed(bool): display log information on terminal screen or notrotation(str):logururotationserialize(bool): data serialized or notlog_format(str):loguruformatlog_path(str): file path for storing log datacustom_func(function): function for custom serialized dataimportjsonfrompymongoimportMongoClientfromlogger_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))With Redisparameters:redis_instance(object): redis instancekey_prefix(str): prefix for redis keyterminal_displayed(bool): display log information on terminal screen or notrotation(str):logururotationserialize(bool): data serialized or notlog_format(str):loguruformatlog_path(str): file path for storing log datacustom_func(function): function for custom serialized dataimportjsonimportredisfromlogger_master.loggerimportRedisLogger# basic usage for pushing log to redis instanceredis_instance=redis.StrictRedis(host='localhost',password='root',port='root')# pushing log to mongodb with 'my_error_log' key prefixlogger=RedisLogger(redis_instance=redis_instance,key_prefix='my_error_log')# pushing serialize log to ./my_log/log_filelogger=RedisLogger(redis_instance=redis_instance,key_prefix='my_error_log',serialize=True,log_path='./my_log/log_file',rotation='1 week')# disable display log on screenlogger=RedisLogger(redis_instance=redis_instance,key_prefix='my_error_log',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=RedisLogger(redis_instance=redis_instance,key_prefix='my_error_log',custom_func=custom_function)try:100/0exceptExceptionase:logger.error(str(e))With Fluentdparameters:hostname(str): fluentd hostnameport(int): fluentd portkey_prefix(str): prefix for fluentd keyterminal_displayed(bool): display log information on terminal screen or notrotation(str):logururotationserialize(bool): data serialized or notlog_format(str):loguruformatlog_path(str): file path for storing log datacustom_func(function): function for custom serialized dataimportjsonfromlogger_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))you can also push log to both fluentd and mongodb withfluent-plugin-mongo.Buy me a coffee, if you like it!