Skip to content

Commit

Permalink
Merge branch 'main' into sp-opl
Browse files Browse the repository at this point in the history
  • Loading branch information
sarathbrp authored Dec 15, 2023
2 parents ec76b0e + 1319c45 commit 3900c1a
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions core/opl/skelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

def setup_logger(app_name, stderr_log_lvl):
"""
Create logger that logs to both stderr and log file but with different log level
Create logger that logs to both stderr and log file but with different log levels
"""
# Remove all handlers from root logger if any
logging.basicConfig(level=logging.NOTSET, handlers=[])
# Change root logger level from WARNING (default) to NOTSET in order for all messages to be delegated.
logging.basicConfig(level=logging.NOTSET, handlers=[]) # `force=True` was added in Python 3.8 :-(
# Change root logger level from WARNING (default) to NOTSET in order for all messages to be delegated
logging.getLogger().setLevel(logging.NOTSET)

# Log message format
Expand All @@ -22,30 +22,30 @@ def setup_logger(app_name, stderr_log_lvl):
)
formatter.converter = time.gmtime

# Configure loggers of libraries we use
# Silence loggers of some chatty libraries we use
urllib_logger = logging.getLogger("urllib3.connectionpool")
urllib_logger.setLevel(stderr_log_lvl)
selenium_logger = logging.getLogger("selenium.webdriver.remote.remote_connection")
selenium_logger.setLevel(stderr_log_lvl)

# Add stderr handler, with level INFO
console = logging.StreamHandler()
console.setFormatter(formatter)
console.setLevel(stderr_log_lvl)
logging.getLogger(app_name).addHandler(console)
# Add stderr handler, with provided level
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
console_handler.setLevel(stderr_log_lvl)
logging.getLogger().addHandler(console_handler)

# Add file rotating handler, with level DEBUG
rotating_handler = logging.handlers.RotatingFileHandler(
filename=f"/tmp/{app_name}.log", maxBytes=100 * 1000, backupCount=2
)
rotating_handler.setLevel(logging.DEBUG)
rotating_handler.setFormatter(formatter)
rotating_handler.setLevel(logging.DEBUG)
logging.getLogger().addHandler(rotating_handler)

return logging.getLogger(app_name)

@contextmanager
def test_setup(parser, logger_name=None):
def test_setup(parser, logger_name="root"):
parser.add_argument(
"--status-data-file",
default=os.getenv("STATUS_DATA_FILE", "/tmp/status-data.json"),
Expand All @@ -63,9 +63,6 @@ def test_setup(parser, logger_name=None):
)
args = parser.parse_args()

if logger_name is None:
logger_name = "root"

if args.debug:
logger = setup_logger(logger_name, logging.DEBUG)
elif args.verbose:
Expand Down

0 comments on commit 3900c1a

Please sign in to comment.