Im trying to setup logging for my module:
Used the exact snippet hosted here: https://gist.github.com/kingspp/9451566a5555fb022215ca2b7b802f19
logging.yaml:
info_file_handler: class: logging.handlers.RotatingFileHandler level: INFO formatter: standard filename: /tmp/info.log maxBytes: 10485760 # 10MB backupCount: 20 encoding: utf8 setup_logging.py
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'): path = default_path value = os.getenv(env_key, None) if value: path = value if os.path.exists(path): with open(path, 'rt') as f: try: config = yaml.safe_load(f.read()) logging.config.dictConfig(config) Im initiating this in server.py as: moto/server.py
setup_logging() logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.info("Logger SETUP successfully") This logs the output to the /tmp/info.log appropriately but when I initialize the same in another file in a child module Im not seeing the logs getting written to the file:
moto/ec2/models.py
logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) def describe_internet_gateways( self, internet_gateway_ids=None, filters=None): igws = [] #logger = logging.getLogger(__name__) #logger.setLevel(logging.DEBUG) print("\n\n\n\n\n{}\n\n\n\n\n".format(__name__)) logger.info("in describe_internet_gateways") session = get_db_session() igws = session.query(InternetGateway_t) But if I initialize the logger inside the method(if I uncomment the commented section inside the method above) then the logs are written appropriately in the /tmp/info.log file
Could someone please help me what Im missing out, how to initialize the logs only once and use it across modules?
Read and tried the logging module explanation from python docs, but Im stilling hitting the issue.
Edit-1: Tried the solution below by @olinox14 below by doing the following edits:
logging.json
"handlers": { "console": { "class": "logging.StreamHandler", "level": "DEBUG", "formatter": "standard", "stream": "ext://sys.stdout" }, "info_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "INFO", "formatter": "standard", "filename": "/tmp/info.log", "maxBytes": 10485760, "backupCount": 20, "encoding": "utf8" }, "error_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "ERROR", "formatter": "error", "filename": "/tmp/errors.log", "maxBytes": 10485760, "backupCount": 20, "encoding": "utf8" }, "debug_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "DEBUG", "formatter": "standard", "filename": "/tmp/debug.log", "maxBytes": 10485760, "backupCount": 20, "encoding": "utf8" }, "critical_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "CRITICAL", "formatter": "standard", "filename": "/tmp/critical.log", "maxBytes": 10485760, "backupCount": 20, "encoding": "utf8" }, "warn_file_handler": { "class": "logging.handlers.RotatingFileHandler", "level": "WARN", "formatter": "standard", "filename": "/tmp/warn.log", "maxBytes": 10485760, "backupCount": 20, "encoding": "utf8" } }, "root": { "level": "NOTSET", "handlers": [ "console", ], "propagate": true }, "loggers": { "test": { "level": "DEBUG", "handlers": [ "console", "info_file_handler", "error_file_handler", "critical_file_handler", "debug_file_handler", "warn_file_handler" ], "propagate": "no" } } } But now Im seeing logs on console but not logged in the file.