Created
August 16, 2023 07:06
-
-
Save palawer/fab2ecfcd025efef0ac2734d8908057c to your computer and use it in GitHub Desktop.
Logger with custom log file form parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import logging | |
import os | |
logging.basicConfig( | |
format='%(asctime)s %(levelname)s %(message)s', | |
level=logging.INFO, | |
handlers=[logging.StreamHandler()] | |
) | |
def some_function(): | |
logging.info(f'Hello from SomeFunction') | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-l", "--log-file", type=str, default="default.log") | |
args = parser.parse_args() | |
logging.getLogger().addHandler(logging.FileHandler(args.log_file)) | |
logging.info(f'Hello from Main') | |
some_function() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment