How to write to a file, using the logging Python module?

后端 未结 9 2023
天涯浪人
天涯浪人 2020-11-27 10:34

How can I use the logging module in Python to write to a file? Every time I try to use it, it just prints out the message.

相关标签:
9条回答
  • 2020-11-27 10:39

    Taken from the "logging cookbook":

    # create logger with 'spam_application'
    logger = logging.getLogger('spam_application')
    logger.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    fh = logging.FileHandler('spam.log')
    fh.setLevel(logging.DEBUG)
    logger.addHandler(fh)
    

    And you're good to go.

    P.S. Make sure to read the logging HOWTO as well.

    0 讨论(0)
  • 2020-11-27 10:49

    An example of using logging.basicConfig rather than logging.fileHandler()

    logging.basicConfig(filename=logname,
                                filemode='a',
                                format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                                datefmt='%H:%M:%S',
                                level=logging.DEBUG)
    
    logging.info("Running Urban Planning")
    
    self.logger = logging.getLogger('urbanGUI')
    

    In order, the five parts do the following:

    1. set the output file (filename=logname)
    2. set it to append rather than overwrite (filemode='a')
    3. determine the format of the output message (format=...)
    4. determine the format of the output time (datefmt='%H:%M:%S')
    5. and determine the minimum message level it will accept (level=logging.DEBUG).
    0 讨论(0)
  • 2020-11-27 10:49
    import sys
    import logging
    
    from util import reducer_logfile
    logging.basicConfig(filename=reducer_logfile, format='%(message)s',
                        level=logging.INFO, filemode='w')
    
    0 讨论(0)
  • 2020-11-27 10:50

    I prefer to use a configuration file. It allows me to switch logging levels, locations, etc without changing code when I go from development to release. I simply package a different config file with the same name, and with the same defined loggers.

    import logging.config
    if __name__ == '__main__':
        # Configure the logger
        # loggerConfigFileName: The name and path of your configuration file
        logging.config.fileConfig(path.normpath(loggerConfigFileName))
    
        # Create the logger
        # Admin_Client: The name of a logger defined in the config file
        mylogger = logging.getLogger('Admin_Client')
    
        msg='Bite Me'
        myLogger.debug(msg)
        myLogger.info(msg)
        myLogger.warn(msg)
        myLogger.error(msg)
        myLogger.critical(msg)
    
        # Shut down the logger
        logging.shutdown()
    

    Here is my code for the log config file

    #These are the loggers that are available from the code
    #Each logger requires a handler, but can have more than one
    [loggers]
    keys=root,Admin_Client
    
    
    #Each handler requires a single formatter
    [handlers]
    keys=fileHandler, consoleHandler
    
    
    [formatters]
    keys=logFormatter, consoleFormatter
    
    
    [logger_root]
    level=DEBUG
    handlers=fileHandler
    
    
    [logger_Admin_Client]
    level=DEBUG
    handlers=fileHandler, consoleHandler
    qualname=Admin_Client
    #propagate=0 Does not pass messages to ancestor loggers(root)
    propagate=0
    
    
    # Do not use a console logger when running scripts from a bat file without a console
    # because it hangs!
    [handler_consoleHandler]
    class=StreamHandler
    level=DEBUG
    formatter=consoleFormatter
    args=(sys.stdout,)# The comma is correct, because the parser is looking for args
    
    
    [handler_fileHandler]
    class=FileHandler
    level=DEBUG
    formatter=logFormatter
    # This causes a new file to be created for each script
    # Change time.strftime("%Y%m%d%H%M%S") to time.strftime("%Y%m%d")
    # And only one log per day will be created. All messages will be amended to it.
    args=("D:\\Logs\\PyLogs\\" + time.strftime("%Y%m%d%H%M%S")+'.log', 'a')
    
    
    [formatter_logFormatter]
    #name is the name of the logger root or Admin_Client
    #levelname is the log message level debug, warn, ect 
    #lineno is the line number from where the call to log is made
    #04d is simple formatting to ensure there are four numeric places with leading zeros
    #4s would work as well, but would simply pad the string with leading spaces, right justify
    #-4s would work as well, but would simply pad the string with trailing spaces, left justify
    #filename is the file name from where the call to log is made
    #funcName is the method name from where the call to log is made
    #format=%(asctime)s | %(lineno)d | %(message)s
    #format=%(asctime)s | %(name)s | %(levelname)s | %(message)s
    #format=%(asctime)s | %(name)s | %(module)s-%(lineno) | %(levelname)s | %(message)s
    #format=%(asctime)s | %(name)s | %(module)s-%(lineno)04d | %(levelname)s | %(message)s
    #format=%(asctime)s | %(name)s | %(module)s-%(lineno)4s | %(levelname)-8s | %(message)s
    
    format=%(asctime)s | %(levelname)-8s | %(lineno)04d | %(message)s
    
    
    #Use a separate formatter for the console if you want
    [formatter_consoleFormatter]
    format=%(asctime)s | %(levelname)-8s | %(filename)s-%(funcName)s-%(lineno)04d | %(message)s
    
    0 讨论(0)
  • 2020-11-27 10:52

    This example should work fine. I have added streamhandler for console. Console log and file handler data should be similar.

        # MUTHUKUMAR_TIME_DATE.py #>>>>>>>> file name(module)
    
        import sys
        import logging
        import logging.config
        # ================== Logger ================================
        def Logger(file_name):
            formatter = logging.Formatter(fmt='%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s',
                                          datefmt='%Y/%m/%d %H:%M:%S') # %I:%M:%S %p AM|PM format
            logging.basicConfig(filename = '%s.log' %(file_name),format= '%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s',
                                          datefmt='%Y/%m/%d %H:%M:%S', filemode = 'w', level = logging.INFO)
            log_obj = logging.getLogger()
            log_obj.setLevel(logging.DEBUG)
            # log_obj = logging.getLogger().addHandler(logging.StreamHandler())
    
            # console printer
            screen_handler = logging.StreamHandler(stream=sys.stdout) #stream=sys.stdout is similar to normal print
            screen_handler.setFormatter(formatter)
            logging.getLogger().addHandler(screen_handler)
    
            log_obj.info("Logger object created successfully..")
            return log_obj
        # =======================================================
    
    
    MUTHUKUMAR_LOGGING_CHECK.py #>>>>>>>>>>> file name
    # calling **Logger** function
    file_name = 'muthu'
    log_obj =Logger(file_name)
    log_obj.info("yes   hfghghg ghgfh".format())
    log_obj.critical("CRIC".format())
    log_obj.error("ERR".format())
    log_obj.warning("WARN".format())
    log_obj.debug("debug".format())
    log_obj.info("qwerty".format())
    log_obj.info("asdfghjkl".format())
    log_obj.info("zxcvbnm".format())
    # closing file
    log_obj.handlers.clear()
    
    OUTPUT:
    2019/07/13 23:54:40 MUTHUKUMAR_TIME_DATE,line: 17     INFO | Logger object created successfully..
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 8     INFO | yes   hfghghg ghgfh
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 9 CRITICAL | CRIC
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 10    ERROR | ERR
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 11  WARNING | WARN
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 12    DEBUG | debug
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 13     INFO | qwerty
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 14     INFO | asdfghjkl
    2019/07/13 23:54:40 MUTHUKUMAR_LOGGING_CHECK,line: 15     INFO | zxcvbnm
    
    Thanks, 
    
    0 讨论(0)
  • 2020-11-27 10:54

    here's a simpler way to go about it. this solution doesn't use a config dictionary and uses a rotation file handler, like so:

    import logging
    from logging.handlers import RotatingFileHandler
    
    logging.basicConfig(handlers=[RotatingFileHandler(filename=logpath+filename,
                         mode='w', maxBytes=512000, backupCount=4)], level=debug_level,
                         format='%(levelname)s %(asctime)s %(message)s', 
                        datefmt='%m/%d/%Y%I:%M:%S %p')
    
    logger = logging.getLogger('my_logger')
    

    or like so:

    import logging
    from logging.handlers import RotatingFileHandler
    
    handlers = [
                RotatingFileHandler(filename=logpath+filename, mode='w', maxBytes=512000, 
                                    backupCount=4)
               ]
    logging.basicConfig(handlers=handlers, level=debug_level, 
                        format='%(levelname)s %(asctime)s %(message)s', 
                        datefmt='%m/%d/%Y%I:%M:%S %p')
    
    logger = logging.getLogger('my_logger')
    

    the handlers variable needs to be an iterable. logpath+filename and debug_level are just variables holding the respective info. of course, the values for the function params are up to you.

    the first time i was using the logging module i made the mistake of writing the following, which generates an OS file lock error (the above is the solution to that):

    import logging
    from logging.handlers import RotatingFileHandler
    
    logging.basicConfig(filename=logpath+filename, level=debug_level, format='%(levelname)s %(asctime)s %(message)s', datefmt='%m/%d/%Y
     %I:%M:%S %p')
    
    logger = logging.getLogger('my_logger')
    logger.addHandler(RotatingFileHandler(filename=logpath+filename, mode='w', 
                      maxBytes=512000, backupCount=4))
    

    and Bob's your uncle!

    0 讨论(0)
提交回复
热议问题