3
'''Setting configuration for the logger'''

import logging
from logging.handlers import RotatingFileHandler
import os
import sys
import structlog

LOG_LEVEL = os.environ.get("LOG_LEVEL", "info").upper()
LOG_HANDLER = os.environ.get("LOG_HANDLER", "file").upper()
LOG_BASE_PATH = os.environ.get("LOG_BASE_PATH", "/var/log")
LOG_DEFAULT_NAME = os.environ.get("LOG_BASE_NAME", "extraction_training_service.log")

def get_logging_env_vars():
    ''' Use for level and handler setting'''
    if LOG_LEVEL == "INFO":
        level = logging.INFO
    else:
        level = logging.DEBUG
    if LOG_HANDLER == "FILE":
        handler = RotatingFileHandler(LOG_BASE_PATH + "/" + LOG_DEFAULT_NAME,
                                      mode='a', maxBytes=5 * 1024 * 1024,
                                      backupCount=10, encoding=None, delay=0)
    else:
        handler = logging.StreamHandler(sys.stdout)
    return handler, level


HANDLER, LEVEL = get_logging_env_vars()
logging.basicConfig(
    format="%(message)s",
    level=LEVEL,
)
logging.root.handlers = [HANDLER]
print(logging.root.handlers)
structlog.configure(
    processors=[
        structlog.stdlib.filter_by_level,
        structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S"),
        structlog.stdlib.add_log_level,
        structlog.stdlib.add_logger_name,
        structlog.stdlib.PositionalArgumentsFormatter(),
        structlog.processors.StackInfoRenderer(),
        structlog.processors.format_exc_info,
        structlog.processors.UnicodeDecoder(),
        structlog.processors.ExceptionPrettyPrinter(),
        structlog.processors.JSONRenderer(),
    ],
    context_class=dict,
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.BoundLogger,
    cache_logger_on_first_use=True,
)

The above is the config but when I run this along with my text extraction service it doesn't write to file but to stdout only

Extraction service is an CNN OCR using tensor-flow. if I make it into function and call it then all the output is redirected into the file but i want to just log the items I have added

JAPSIMRAN
  • 31
  • 7
  • When you say "stdout only" it implies that the handler for some reason isn't set correctly? What does your `print(logging.root.handlers)` line print? Does logging.getLogger().info("test")` go to stdout or to the file? – hynek Dec 19 '19 at 14:26
  • stderr, I solved the issue it seems logging service was being utilized at more than one time and that was causing the issue – JAPSIMRAN Dec 20 '19 at 05:40

0 Answers0