简体   繁体   中英

Proper way of logging and printing to console

# --------------------------------------
# Start of logging of removed folders
# ----------------------------------------
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='RemovedCRMFolders.log',
                    filemode='w')


# -------------------------------------------
# Checks to see if listed folders exists and then deletes
# -------------------------------------------


def remove_directory(pathslist):
    for path in pathslist:
        if os.path.exists(path) and os.path.isdir(path):
            shutil.rmtree(path)
            logging.warning('Found ' + path + ' removing')
            print(colored('Found ' + path + ' removing', 'green'))
        else:
            print('Looking for ' + path + ' not found..')

dirs_to_delete = [
    'C:\Folder1',
    'C:\Folder2'
    ]

would using both the logging.warning and print underneath be a way to still show the output in console/window if it finds a folder? Right now if you write the code without print and just logging.warning it will no longer print to console stating it sees the folder and will just write to the log file and I am looking to have it show in console and still print to log is there a easier way to do this within the logging command or is this the best way?

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='RemovedCRMFolders.log',
                    filemode='w')

here you have specified your logs to go to RemovedCRMFolders.log . your logs are being written in that file. you won't see your output in console. to see your output in console you need to remove filename when you're configuring your logger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM