Package: pylogrus
PyLogrus is a structured logger for Python which is inspired by Logrus
Golang libraryhttps://github.com/sirupsen/logrus. PyLogrus extends the
built-inloggingmodule with the ability making a colorized records
in log and records in JSON format.FeaturesUsing this package, you will be able to:colorize output in console (Textual format)switch off the colorization (Textual format)add extra fields in a log recordadd permanent extra fields in a log recordadd permanent prefix for messagecreate a new contextual instancesave log records in the JSON formatoverride the names of logging levelsoverride colors of base elements (Textual format)override name of keys (JSON format)define only needed fields in records (JSON format)create time of a record in Zulu formatInitializationimportloggingfrompylogrusimportPyLogruslogging.setLoggerClass(PyLogrus)FormattersTextFormatterTextFormatter class allows colorizing console output by setting acolorizeargument. The colorization can be switched off. Time of log
record may be set in Zulu format. Just setdatefmtargument as ‘Z’.importloggingfrompylogrusimportPyLogrus,TextFormatterlogging.setLoggerClass(PyLogrus)logger=logging.getLogger(__name__)# type: PyLogruslogger.setLevel(logging.DEBUG)formatter=TextFormatter(datefmt='Z',colorize=True)ch=logging.StreamHandler()ch.setLevel(logging.DEBUG)ch.setFormatter(formatter)logger.addHandler(ch)Overriding name of levelsYou can define only necessary changes:formatter=TextFormatter(datefmt='Z',colorize=True)formatter.override_level_names({'CRITICAL':'CRIT','ERROR':'ERRO','WARNING':'WARN','DEBUG':'DEBU'})or for instance:formatter.override_level_names({'CRITICAL':'FATAL'})Overriding colorsTextFormatter has several base elements which can be colorized. You can
get them viacolorproperty:print(formatter.color)
...
{
    'asctime':  '\x1b[2;37m',  # time of log record
    'prefix':   '\x1b[0;36m',  # message prefix
    'field':    '\x1b[0;32m',  # key of extra field
    'value':    '\x1b[0m',     # value of extra field
    'debug':    '\x1b[0;34m',
    'info':     '\x1b[0;32m',
    'warning':  '\x1b[0;33m',
    'error':    '\x1b[0;31m',
    'critical': '\x1b[2;31m'
}A color of elements can be changed using CL_* constants. You can
define new color only for those elements you need.frompylogrusimportPyLogrus,TextFormatter,CL_BLDYLW...formatter=TextFormatter(colorize=True)formatter.override_colors({'prefix':CL_BLDYLW})JsonFormatterJsonFormatter class allows to save log records in the JSON format.
During class initialization, you can:Set time of log record in Zulu format. Just setdatefmtargument
as ‘Z’.Define a list of enabled fields which will be present in a log record
viaenabled_fieldsargument. An enabled field is represented by
original field name or by a tuple which contains the original name
and new desirable name. The new name overrides the original one in an
output.For pretty print a JSON log record in a console, set theindentandsort_keysarguments (optional).importloggingfrompylogrusimportPyLogrus,JsonFormatterlogging.setLoggerClass(PyLogrus)logger=logging.getLogger(__name__)# type: PyLogruslogger.setLevel(logging.DEBUG)enabled_fields=[('name','logger_name'),('asctime','service_timestamp'),('levelname','level'),('threadName','thread_name'),'message',('exception','exception_class'),('stacktrace','stack_trace'),'module',('funcName','function')]formatter=JsonFormatter(datefmt='Z',enabled_fields=enabled_fields,indent=2,sort_keys=True)ch=logging.StreamHandler()ch.setLevel(logging.DEBUG)ch.setFormatter(formatter)logger.addHandler(ch)Overriding name of levelsName of levels can be overridden in the same way as in case of using the
TextFormatter.formatter=JsonFormatter()formatter.override_level_names({'WARNING':'WARN'})UsagePlease, see the examples of usage in theexamplesdirectory.Log message as usual:importloggingfrompylogrusimportPyLogrus,TextFormatterdefget_logger():logging.setLoggerClass(PyLogrus)...formatter=TextFormatter()...returnloggerlog=get_logger()log.debug("Using base logger")Log message with an extra field:log.withFields({'user':'John Doe'}).debug("Message with an extra field")Add permanent field(s) in logger and get a contextual instance:log_ctx=log.withFields({'context':1})log_ctx.info("Add permanent field into current logger")Add permanent prefix to message for current logger instance:log_ctx=log_ctx.withPrefix("[API]")log_ctx.info("Add prefix as a permanent part of a message")Log message with extra fields:log_ctx.withFields({'user':'Admin','transaction_id':str(uuid.uuid4())}).warning("Message with prefix and extra fields")