Skip to main content

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"}

Release files for json-logger-stdout 1.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-logger-stdout 1.2.6
File Size Uploaded
json_logger_stdout-1.2.6.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-logger-stdout 1.2.6
File Interpreter ABI Platform
json_logger_stdout-1.2.6-py3-none-any.whl Python 3 none any Details

Total release size:7.6 kB

Release files / json_logger_stdout-1.2.6.tar.gz

Download URL json_logger_stdout-1.2.6.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ed451614e0e0b4f1b21fc785070a7d0b896be62adf806d3b46ebc414dfaaabf7
BLAKE2b-256 checksum
How to use checksums
2f93f7820d13d935f606b36f10a377321ba56358efbd2854e954ad9d7fb95e61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.17 Linux/5.4.109+

Release files / json_logger_stdout-1.2.6-py3-none-any.whl

Download URL json_logger_stdout-1.2.6-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be311f99fe01effa2fb1249982f884ad7ebe6acc34d2076da43e50341eab5e22
BLAKE2b-256 checksum
How to use checksums
9489110a6a537695d5648ff57c2ae9372f06feaec979fed4a551c46a70427342
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.17 Linux/5.4.109+

Release history Release notifications | RSS feed

This release

1.2.6 This release

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page