简体   繁体   中英

Log progress statement to separate file

I am using the logger from the standard library and would like to log a progress statement to its own file. I have the logger set up to log to both the console as well as a file. The setup looks like this.

def setup_logging(args):
  try:
    numeric_level = getattr(logging, args.loglevel.upper())

    # If they provide a full path, ensure that the path is valid.
    log_dir = os.path.dirname(args.logfile)
    if log_dir and not os.path.isdir(log_dir):
        # If the provided path doesn't exist, write the log file
        # using the provided name to the current directory
        args.logfile = os.path.basename(args.logfile)
        sys.stderr.write("Invalid logfile path. Defaulting to current directory\n")

    logging.basicConfig(level=numeric_level,
                        format='%(asctime)s - %(name)s - %(levelname)s - %(funcName)s - %(message)s',
                        datefmt='%m-%d-%Y %H:%M:%S',
                        filename=args.logfile,
                        filemode='w')

    console = logging.StreamHandler()
    console.setLevel(logging.ERROR)
    formatter = logging.Formatter('%(name)s - %(funcName)s - %(levelname)s - %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)

   except Exception as e:
    logging.critical("Failed to configure logging for the following reason: %s", e.message)
    return False

   return True

This works great for logging to the the console and logging all statements of a given level or above to the file. The problem comes from the fact that I would like to log a progress statement (And only that statement) to a separate file. I am currently printing the statement to the console in the following manner.

if current < num_file:
    print("Processing file {:.0f} of {} ({:.2%} Completed)".format(current + 1, num_file, current/num_file),
              end='\r')

else:
    print("Finished file {:.0f} of {} ({:.2%} Completed)".format(current, num_file, current/num_file))

Unfortunately this statement doesn't seem to print on all consoles and gets overwritten when the logger logs an error statement to the console. Because of this I'd like to additionally write the statement to a file.

I know that I could use the following to accomplish this, but I'm wondering if there is a way to do it using the logging library. Id rather not have to worry about dealing with the file myself if it can be avoided.

prog_file = open("progress.txt", 'w') 

...

print("Processing file {:.0f} of {} ({:.2%} Completed)".format(current + 1, num_file, current/num_file),
              end='\r', file=prog_file)

You can try logging filter , something like:

class ProgressFilter(logging.Filter):

    def filter(self, record):
        if record.msg.endswith('Completed)'):
            return True

logging.getLogger().handlers[0].addFilter(ProgressFilter())

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