Skip to main content

JSON Logger for MicroServices. Prints logs to the stdout of the service and can be shipped to ES by leveraging a centralized tool like Fluentd.

Project description

JSON Logger Stdout

Log to the stdout directly so that they can be consumed by some centralized log collecting service like Fluentd.

=================================================================================================

JSON Logger for MicroServices. Prints logs to the stdout of the service and can be shipped to ES by leveraging a centralized tool like Fluentd

Usage Examples json_std_logger is the log object that the library exports and it exposes methods for all log levels which is shown in the examples. This is an instance of the class JSONLoggerStdout

Important Note: By default the log level is set at INFO. Please change it using the setLevel method which is exposed out.

from json_logger_stdout import JSONStdFormatter, json_std_logger

#By Default the log level is INFO

json_std_logger.error('error log')      # {"timestamp": "2022-01-21T06:36:32.668292Z", "level": "ERROR", "message": "error log"}
json_std_logger.info('info log')        # {"timestamp": "2022-01-21T06:36:32.668420Z", "level": "INFO", "message": "info log"}
json_std_logger.debug('debug log, no print')      # Prints Nothing as the current level by default is INFO

import logging
json_std_logger.setLevel(logging.DEBUG) # Set Log Level
json_std_logger.debug('debug log')      # {"timestamp": "2022-01-21T06:36:32.668476Z", "level": "DEBUG", "message": "debug log"}

List of Exposed Methods

getLogger : Returns the already initialized log object.

setLevel : Sets Log Level. Pass any valid log level from the python logging module.

setFormatter : Sets a custom log formatter if needed. This call will clear out all the other handlers, so please call this before adding more log handlers. It takes two arguments, the first one is simply a string that takes the fields to print in the final log. See #Example-Set-Formatter-1 for more details. It is possible to use a different log formatter altogether and use the second parameter to pass the log formatter object. See #Example-Set-Formatter-2 for more details.

addHandlers : Pass an array of log handlers that will be attached to the log object.

Extra Params for the logger instance

You can also configure permanent extra params to be used for a log object if you prefer not to mention it everytime in your log messages. You can either pass the params in during initializing a new JSONLoggerStdout instance or adding them using the _setParams method in the json_std_logger.

from json_logger_stdout import JSONLoggerStdout
logger = JSONLoggerStdout(
    service="Running Service",
    id="CAT-232"
)
logger.error('error log')          # {"timestamp": "2023-03-17T16:11:32.858440Z", "level": "ERROR", "message": "error log", "service": "Running Service", "id": "CAT-232"}
from json_logger_stdout import json_std_logger
json_std_logger._setParams(
    service="Different Service",
    id="ASD-233"
)
json_std_logger.info('info log')            # {"timestamp": "2023-03-17T16:11:32.858540Z", "level": "INFO", "message": "info log", "service": "Different Service", "id": "ASD-233"}

Advanced Usage

The package exposes two classes JSONLoggerStdout & JSONStdFormatter It is possible for the user to get a different log object by using the base class JSONLoggerStdout

json_logger = JSONLoggerStdout(loggerName=<optional>)

NOTE : All the unnamed parameters that are passed to the logger would be converted to string and concatenated with a space . However sending named parameters to the logger would add the keys as extra parameters in the log record. Please see the last example for more clarity on this.

json_std_logger.setFormatter('%(timestamp)s %(level)s %(name) %(filename)s %(lineno)s %(module)s %(message)s')     # Example-Set-Formatter-1
json_std_logger.setFormatter(None, JSONStdFormatter('%(timestamp)s %(level)s %(name) %(filename)s %(lineno)s %(message)s'))   # Example-Set-Formatter-2

# Usage with variable parameters and named parameters to the logger.
json_std_logger.debug({'unnamedObjKey1': 'will print in message'}, {'unnamedObjKey2': 'should be concatenated with the previous part'}, extra='Named Parameter, so will be addded as an extra parameter')
# {"timestamp": "2022-01-21T07:40:03.363500Z", "level": "DEBUG", "name": "root", "filename": "json_logger_stdout.py", "lineno": 67, "module": "json_logger_stdout", "message": "{'unnamedObjKey1': 'will print in message'}, {'unnamedObjKey2': 'should be concatenated with the previous part'}", "extra": "Named Parameter, so will be addded as an extra parameter"}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_logger_stdout-1.2.6.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

json_logger_stdout-1.2.6-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file json_logger_stdout-1.2.6.tar.gz.

File metadata

  • Download URL: json_logger_stdout-1.2.6.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.8.17 Linux/5.4.109+

File hashes

Hashes for json_logger_stdout-1.2.6.tar.gz
Algorithm Hash digest
SHA256 ed451614e0e0b4f1b21fc785070a7d0b896be62adf806d3b46ebc414dfaaabf7
MD5 c3a3ac9f338f3341093dfb4b84fd6c2c
BLAKE2b-256 2f93f7820d13d935f606b36f10a377321ba56358efbd2854e954ad9d7fb95e61

See more details on using hashes here.

File details

Details for the file json_logger_stdout-1.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for json_logger_stdout-1.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 be311f99fe01effa2fb1249982f884ad7ebe6acc34d2076da43e50341eab5e22
MD5 44fe8f359cec9769875c03f5795e5f2e
BLAKE2b-256 9489110a6a537695d5648ff57c2ae9372f06feaec979fed4a551c46a70427342

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page