koki-nakamura22 / pyqlite

Apache License 2.0
0 stars 0 forks source link

Implements an executed SQL log output function #9

Closed koki-nakamura22 closed 2 years ago

koki-nakamura22 commented 2 years ago

An example code.

def test_log(caplog):
    from logging import DEBUG, INFO, ERROR, getLogger

    caplog.set_level(DEBUG)
    # caplog.set_level(INFO)
    logger = getLogger('test')
    logger.debug('debug', extra={'additionaldata': 'debug'})
    logger.debug('debug2', extra={'additionaldata': 'debug2'})
    logger.info('info', extra={'additionaldata': 'info'})

    print(caplog.record_tuples)
    print(caplog.records)
    print(f"logger name: {caplog.records[0].name}")
    print(f"log level name: {caplog.records[0].levelname}")
    print(f"log level no: {caplog.records[0].levelno}")
    print(f"log level msg: {caplog.records[0].msg}")
    print(f"log extra info: {caplog.records[0].additionaldata}")
koki-nakamura22 commented 2 years ago

An example code 2. -> Output log to console.

def test_log2():
    from logging import INFO, getLogger, StreamHandler, Formatter, basicConfig

    log_level = INFO

    stream_handler = StreamHandler()
    stream_handler.setLevel(log_level)
    stream_handler.setFormatter(
        Formatter("[%(asctime)s]:%(levelname)s:%(message)s:%(additionaldata)s"))

    basicConfig(level=log_level, handlers=[stream_handler])

    logger = getLogger('test')
    logger.setLevel(log_level)
    logger.debug('debug', extra={'additionaldata': 'debug'})
    logger.debug('debug2', extra={'additionaldata': 'debug2'})
    logger.info('info', extra={'additionaldata': {"name": "Koki"}})