Coder Social home page Coder Social logo

jimmyyyeh / logger-master Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 10 KB

A package for pushing serialized error log which generated by loguru to remote host.

Home Page: https://pypi.org/project/logger-master/

Python 100.00%
python logging mongodb redis efk fluentd

logger-master's Introduction

logger-master

A package for pushing serialized error log which generated by loguru to remote storage.

Description:

A package for pushing serialized error log which generated by loguru to remote storage (including mongodb / redis / fluentd).

How To Use:

With MongoDB

  • parameters:
    • mongo_instance(object): mongodb instance
    • mongo_db(str): database for storing log data
    • mongo_collection(str): collection for storing log data
    • terminal_displayed(bool): display log information on terminal screen or not
    • rotation(str): loguru rotation
    • serialize(bool): data serialized or not
    • log_format(str): loguru format
    • log_path(str): file path for storing log data
    • custom_func(function): function for custom serialized data
import json
from pymongo import MongoClient
from logger_master.logger import MongoLogger

# pushing log to mongodb instance

mongo_uri = 'mongodb://root:root@localhost:27017/?authMechanism=SCRAM-SHA-1'
mongo_instance = MongoClient(mongo_uri)

# basic usage for pushing log with specific database and collection
logger = MongoLogger(mongo_instance=mongo_instance,
                     mongo_db='my_log',
                     mongo_collection='my_log_collection')

# pushing serialize log to ./my_log/log_file
logger = 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 screen
logger = 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 format
def custom_function(serialized_data):
    if not isinstance(serialized_data, dict):
        serialized_data = json.loads(serialized_data)
    serialized_data.update({'new_key': 'new_value'})
    return serialized_data


logger = MongoLogger(mongo_instance=mongo_instance,
                     mongo_db='my_log',
                     mongo_collection='my_log_collection', 
                     custom_func=custom_function)

try:
    100 / 0
except Exception as e:
    logger.error(str(e))

With Redis

  • parameters:
    • redis_instance(object): redis instance
    • key_prefix(str): prefix for redis key
    • terminal_displayed(bool): display log information on terminal screen or not
    • rotation(str): loguru rotation
    • serialize(bool): data serialized or not
    • log_format(str): loguru format
    • log_path(str): file path for storing log data
    • custom_func(function): function for custom serialized data
import json
import redis
from logger_master.logger import RedisLogger

# basic usage for pushing log to redis instance

redis_instance = redis.StrictRedis(host='localhost',
                                   password='root',
                                   port='root')

# pushing log to mongodb with 'my_error_log' key prefix
logger = RedisLogger(redis_instance=redis_instance,
                     key_prefix='my_error_log')

# pushing serialize log to ./my_log/log_file
logger = 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 screen
logger = RedisLogger(redis_instance=redis_instance,
                     key_prefix='my_error_log',
                     log_path='./my_log/log_file',
                     terminal_displayed=False)

# pushing serialized log with custom format
def custom_function(serialized_data):
    if not isinstance(serialized_data, dict):
        serialized_data = json.loads(serialized_data)
    serialized_data.update({'new_key': 'new_value'})
    return serialized_data


logger = RedisLogger(redis_instance=redis_instance,
                     key_prefix='my_error_log',
                     custom_func=custom_function)

try:
    100 / 0
except Exception as e:
    logger.error(str(e))

With Fluentd

  • parameters:
    • hostname(str): fluentd hostname
    • port(int): fluentd port
    • key_prefix(str): prefix for fluentd key
    • terminal_displayed(bool): display log information on terminal screen or not
    • rotation(str): loguru rotation
    • serialize(bool): data serialized or not
    • log_format(str): loguru format
    • log_path(str): file path for storing log data
    • custom_func(function): function for custom serialized data
import json
from logger_master import FluentdLogger

# pushing log log to fluentd

# basic usage for pushing log data to fluentd
logger = FluentdLogger(hostname='localhost',
                       port=24224,
                       key_prefix='mongo')

# pushing serialize log to ./my_log/log_file
logger = FluentdLogger(hostname='localhost',
                       port=24224,
                       key_prefix='mongo',
                       serialize=True,
                       log_path='./my_log/log_file',
                       rotation='1 week')

# disable display log on screen
logger = FluentdLogger(hostname='localhost',
                       port=24224,
                       key_prefix='mongo',
                       log_path='./my_log/log_file',
                       terminal_displayed=False)


# pushing serialized log with custom format
def custom_function(serialized_data):
    if not isinstance(serialized_data, dict):
        serialized_data = json.loads(serialized_data)
    serialized_data.update({'new_key': 'new_value'})
    return serialized_data


logger = FluentdLogger(hostname='localhost',
                       port=24224,
                       key_prefix='mongo',
                       custom_func=custom_function)

try:
    1 / 0
except Exception as e:
    logger.error(msg=str(e))

you can also push log to both fluentd and mongodb with fluent-plugin-mongo.


Buy Me A Coffee

Buy me a coffee, if you like it!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.