Two thoughts.
The error handling is not useful. The routine is used by the programmer, this is not something that depends on user input and needs strong validation somehow.
So it probably won't trigger anyway, unless you make a typo somewhere. You can just let Python crash and it will display an error. Your script does not add anything useful.
It is preferable to have a config file of some sort, rather than hardcode the desired level etc. You can for example use a .ini file with the configparser lib.
Then some code like this:
CONFIG_FILE_NAME = "config.ini"
def main():
logger = logging.getLogger("app")
try:
# get current application dir
current_dir = os.path.dirname(os.path.realpath(__file__))
ini_file_path = Path(current_dir) / CONFIG_FILE_NAME
# load configuration settings from file
config = configparser.ConfigParser()
config.read(ini_file_path)
# set logging from config file
logging.config.fileConfig(ini_file_path)
logger.info("Application starting")
The .ini file can be along these lines:
[loggers]
keys=root, app
[handlers]
keys=console, file
[formatters]
keys=standard
[logger_root]
level=DEBUG
handlers=console
[logger_app]
qualname=app
level=DEBUG
handlers=console,file
propagate=0
[handler_console]
class=StreamHandler
level=DEBUG
formatter=standard
args=(sys.stdout,)
[handler_file]
class=handlers.TimedRotatingFileHandler
level=DEBUG
formatter=standard
args=('transfers.log', 'd', 5)
[formatter_standard]
format=%(asctime)s - %(filename)s:%(lineno)s - %(funcName)s - %(levelname)s - %(message)s
This is a copy-paste from here I think. It can probably be simplified. Please consider this as an example and not a finished product.
I have used YAML files too. This is a question of preference.
Sometimes, you will want to run the application in different scenarios. For example, in debugging mode you will want to enable logs at level DEBUG, and have a copy of all log messages to a file. In standard mode, it would be sufficient to output to console only, at level INFO or even WARNING to reduce noise. Using a config file makes that a bit easier I think.
Besides, an application of moderate complexity will usually already have a config file of some sort.
Another point is that sometimes we will want to tweak the formatter too. It would be nice to be able to do that without touching the code. As it stands, your routine lacks flexibility. Note that you can output log messages to multiple destinations (eg. console, file) at different levels, but the formatter can be different for destination as well.
There are quite a few available parameters, so using an external config file provides a lot more flexibility.