From 92e89637275a3d7b364bee718d8926b220e08fb2 Mon Sep 17 00:00:00 2001 From: Kamil Date: Mon, 9 Dec 2024 10:25:44 +0000 Subject: [PATCH] add rotating file handler for logging based on worker type --- app/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index bb36dc0..bb0ef3c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -106,6 +106,18 @@ app.logger.setLevel(log_level) FORMAT = "[%(asctime)s][%(filename)18s:%(lineno)4s - %(funcName)20s() ] %(levelname)7s - %(message)s" logging.basicConfig(format=FORMAT) +# Add RotatingFileHandler to log to a file +# if worker is in sys.argv, we are running a celery worker, so we log to a different file +if 'worker' in sys.argv: + log_file = os.path.join("/var/log/", 'jellyplist_worker.log') +elif 'beat' in sys.argv: + log_file = os.path.join("/var/log/", 'jellyplist_beat.log') +else: + log_file = os.path.join("/var/log/", 'jellyplist.log') +file_handler = RotatingFileHandler(log_file, maxBytes=2 * 1024 * 1024, backupCount=10) +file_handler.setFormatter(logging.Formatter(FORMAT)) +app.logger.addHandler(file_handler) + Config.validate_env_vars() cache = Cache(app) redis_client = redis.StrictRedis(host=app.config['CACHE_REDIS_HOST'], port=app.config['CACHE_REDIS_PORT'], db=0, decode_responses=True)