Skip to main content

janus-logging

https://travis-ci.com/madkote/janus_logging.svg?branch=master https://codecov.io/gh/madkote/janus-logging/branch/master/graph/badge.svg https://img.shields.io/pypi/v/janus_logging.svg

sync and async logging within one logger instance.

Installation

pip install janus-logging

Usage

Default

import asyncio
import logging
import sys

import janus_logging

def threaded(sync_log, counter: int):
    for i in range(counter):
        sync_log.info('s-Hello #%s' % i)
        sync_log.info('s-Finished #%s' % counter)

async def async_coro(async_log, counter: int):
    for i in range(counter):
        await async_log.info('aio-Hello #%s' % i)
        await async_log.info('aio-Finished #%s' % counter)

#
counter = 4
name = 'my_janus_logger'
level = logging.DEBUG
stream = sys.stdout
loop = asyncio.get_event_loop()
#
logger = janus_logging.JanusLogger(name=name, level=level, loop=loop, stream=stream)
loop.run_until_complete(
    asyncio.gather(
    loop.run_in_executor(
        None,
        threaded,
        logger.logger_sync(),
        counter
    ),
    async_coro(
        logger.logger_async(),
        counter
    )
    )
)
logger.shutdown()
#
#
loop.close()

The output of above will look like:

s-Hello #0
s-Hello #1
s-Hello #2
aio-Hello #0
s-Hello #3
s-Finished #4
aio-Hello #1
aio-Hello #2
aio-Hello #3
aio-Finished #4

Please note, that the output might be different on your instance.

JSON

Simply use fixtures.

import asyncio
import logging
import sys

import janus_logging

def threaded(sync_log, counter: int):
    for i in range(counter):
        sync_log.info(
            's-Hello #%s' % i,
            extra=dict(counter=i, log_type='sync', log_status='in progress')
        )
    sync_log.info(
        's-Finished #%s' % counter,
        extra=dict(total=counter, log_type='sync', log_status='finished')
    )


async def async_coro(async_log, counter: int):
    for i in range(counter):
        await async_log.info(
            'aio-Hello #%s' % i,
            extra=dict(counter=i, log_type='async', log_status='in progress')
        )
    await async_log.info(
        'aio-Finished #%s' % counter,
        extra=dict(total=counter, log_type='async', log_status='finished')
    )

#
counter = 4
name = 'my_janus_logger'
level = logging.DEBUG
stream = sys.stdout
loop = asyncio.get_event_loop()
#
logger = janus_logging.JanusLogger(
    name=name,
    level=level,
    loop=loop,
    fixture=janus_logging.fixture_json,
    stream=stream,
    extra=dict(bla='blabla')
)
loop.run_until_complete(
    asyncio.gather(
        loop.run_in_executor(
            None,
            threaded,
            logger.logger_sync(logger_name='logger_sync'),
            counter
        ),
        async_coro(
            logger.logger_async(logger_name='logger_async'),
            counter
        )
    )
)
logger.shutdown()
#
#
loop.close()

The output of above will look like:

{"level": "INFO", "msg": "s-Hello #0", "log_type": "sync", "bla": "blabla", "logger_name": "logger_sync", "counter": 0, "log_status": "in progress", "logged_at": "2019-09-27T12:00:02.517101+02:00", "line_numer": 35, "function": "threaded", "file_path": "demo_janus_log.py"}
{"level": "INFO", "msg": "aio-Hello #1", "log_type": "async", "logged_at": "2019-09-27T12:00:02.518000+02:00", "line_number": 60, "function": "info", "file_path": "/home/madkote/janus-logging/janus_logging/__init__.py", "bla": "blabla", "logger_name": "logger_async", "counter": 1, "log_status": "in progress"}
...

Custom

If a custom logger, formatter, handler are required, then create custom fixtures and pass them to the JanusLogger.

def fixture_custom(
            name: str,
            level: int,
            loop: loop: asyncio.AbstractEventLoop,
            **kwargs
            ) -> logging.Logger:
    ...
    return ...

logger = janus_logging.JanusLogger(
    ...,
    fixture=fixture_custom,
    ...
)

Development

Issues and suggestions are welcome through issues

Changes

1.3.2 (2020-11-25)

  • Cleanup, remove dependency from aiologger, which has too many issues right now.

1.3.1 (2020-02-05)

  • Cleanup

  • add missing parameter for formatter

1.3.0 (2020-01-31)

  • Cleanup

  • Fix duplicate handlers

1.2.1 (2019-11-19)

  • Fix shutdown()

1.2.0 (2019-10-04)

  • Executor based AsyncLoggerAdapter due to issues with aiologger==0.4.0

1.1.1 (2019-10-01)

  • Fix SyncJsonFormatter

1.1.0 (2019-09-27)

  • Add AsyncNullHandler

1.0.0 (2019-09-27)

  • Initial release

Metadata

Release files for janus-logging 1.3.2

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

Source distribution (sdist)

Source distribution for janus-logging 1.3.2
File Size Uploaded Provenance
janus-logging-1.3.2.tar.gz 8.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for janus-logging 1.3.2
File Interpreter ABI Platform
janus_logging-1.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 kB

Release files / janus-logging-1.3.2.tar.gz

Download URL janus-logging-1.3.2.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
77360de8f1b1cfbeba8736b4a4c43357451333d4edc5795987f40b540c073ac1
BLAKE2b-256 checksum
How to use checksums
9ffaac23eb718489743d08658a90a2993bf9c8f0022e8f7095bd86dfd6f72fc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.9

Release files / janus_logging-1.3.2-py3-none-any.whl

Download URL janus_logging-1.3.2-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef29ec3eb497eefad205110f96b68dd816567502ea144a0dffc5ad8715139fb5
BLAKE2b-256 checksum
How to use checksums
ce8694a797081c4b25d009d78d893333da9c9a4fbe0762ba038c5ca2d6454c3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.9

Security for janus-logging 1.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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