ฉันจะใช้โมดูลการบันทึกใน Python เพื่อเขียนลงไฟล์ได้อย่างไร ทุกครั้งที่พยายามใช้มันก็แค่พิมพ์ข้อความออกมา
ฉันจะใช้โมดูลการบันทึกใน Python เพื่อเขียนลงไฟล์ได้อย่างไร ทุกครั้งที่พยายามใช้มันก็แค่พิมพ์ข้อความออกมา
คำตอบ:
ตัวอย่างของการใช้logging.basicConfigมากกว่า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')ตามลำดับห้าส่วนทำดังต่อไปนี้:
filename=logname)filemode='a')format=...)datefmt='%H:%M:%S')level=logging.DEBUG)if __name__ == '__main__':หากใช้งานบน apache
                    if __name__ == '__main__':จึงไม่ถูกเรียกใช้งาน
                    นำมาจาก " ตำราการบันทึก ":
# 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)และคุณพร้อมที่จะไป
ปล. อย่าลืมอ่านHOWTO การบันทึกด้วย
logger.warning("message")ไม่สามารถใช้มิได้logger.info("message") logger.debug("message")ที่น่ารำคาญเล็กน้อย
                    logger.setLevel(logging.DEBUG)คนตัดไม้ที่ตัวเองต้องการระดับการบันทึกของตัวเองที่จะกำหนดค่าให้ยอมรับระดับของข้อความเข้าสู่ระบบเช่นว่า เครื่องบันทึกสามารถกำหนดค่าได้ด้วยตัวจัดการหลายตัว ระดับที่กำหนดค่าไว้ในตัวบันทึกจะเป็นตัวกำหนดว่าข้อความบันทึกระดับความรุนแรงใดที่จะส่งไปยังตัวจัดการแต่ละตัวและระดับที่ตั้งไว้ในตัวจัดการจะกำหนดระดับที่ตัวจัดการจะประมวลผล โปรดทราบว่าผู้ที่ต้องการพิมพ์ข้อความข้อมูลจะต้องตั้งค่านี้เป็นINFOทั้งในตัวบันทึกและตัวจัดการเท่านั้น
                    logger.setLevel(logging.DEBUG)- ขอบคุณสำหรับความคิดเห็น
                    ฉันชอบใช้ไฟล์คอนฟิกูเรชัน ช่วยให้ฉันสามารถเปลี่ยนระดับการบันทึกตำแหน่ง ฯลฯ โดยไม่ต้องเปลี่ยนรหัสเมื่อฉันเปลี่ยนจากการพัฒนาไปสู่รุ่น ฉันแค่ทำแพ็กเกจไฟล์กำหนดค่าอื่นที่มีชื่อเดียวกันและมีผู้บันทึกที่กำหนดไว้เหมือนกัน
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()นี่คือรหัสของฉันสำหรับไฟล์กำหนดค่าบันทึก
#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%%ใน Python 3 เช่นtime.strftime("%%Y%%m%%D")
                    http://docs.python.org/library/logging.html#logging.basicConfig
logging.basicConfig(filename='/path/to/your/log', level=....)loggingเอกสารโมดูลอย่างเป็นทางการอนุญาตให้ทำสิ่งนี้ได้ คุณยังสามารถเลือกได้ว่าจะบันทึกอะไรในเทอร์มินัลและบันทึกใดไปยังไฟล์และแอปพลิเคชันที่น่าสนใจอื่น ๆ docs.python.org/3/howto/…
                    นี่เป็นวิธีที่ง่ายกว่าในการดำเนินการนี้ โซลูชันนี้ไม่ใช้พจนานุกรมการกำหนดค่าและใช้ตัวจัดการไฟล์การหมุนเช่นนี้:
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')หรือชอบ:
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')ตัวแปรตัวจัดการจำเป็นต้องทำซ้ำได้ logpath + ชื่อไฟล์และ debug_level เป็นเพียงตัวแปรที่เก็บข้อมูลที่เกี่ยวข้อง แน่นอนว่าค่าสำหรับพารามิเตอร์ของฟังก์ชันขึ้นอยู่กับคุณ
ครั้งแรกที่ฉันใช้โมดูลการบันทึกฉันทำผิดพลาดในการเขียนสิ่งต่อไปนี้ซึ่งทำให้เกิดข้อผิดพลาดในการล็อกไฟล์ OS (ด้านบนเป็นวิธีแก้ปัญหานั้น):
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))และบ็อบเป็นลุงของคุณ!
http://docs.python.org/library/logging.handlers.html#filehandler
FileHandlerชั้นตั้งอยู่ในหลักloggingแพคเกจส่งผลผลิตเข้าสู่ระบบแฟ้มดิสก์
FileHandlerสำหรับสถานการณ์ต่างๆเช่นกัน ( WatchedFileHandler, RotatingFileHandlerฯลฯ )
                    import sys
import logging
from util import reducer_logfile
logging.basicConfig(filename=reducer_logfile, format='%(message)s',
                    level=logging.INFO, filemode='w')ตัวอย่างนี้ควรใช้งานได้ดี ฉันได้เพิ่ม streamhandler สำหรับคอนโซลแล้ว ข้อมูลบันทึกคอนโซลและตัวจัดการไฟล์ควรคล้ายกัน
    # 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, คำอธิบายรูปแบบ
#%(name)s       Name of the logger (logging channel).
#%(levelname)s  Text logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
#%(asctime)s    Human-readable time when the LogRecord was created. By default this is of the form ``2003-07-08 16:49:45,896'' (the numbers after the comma are millisecond portion of the time).
#%(message)s    The logged message. วิธีการโทรปกติ
import logging
#logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.info('Start reading database')
# read database here
records = {'john': 55, 'tom': 66}
logger.debug('Records: %s', records)
logger.info('Updating records ...')
# update records here
logger.info('Finish updating records')เอาท์พุต
INFO:__main__:Start reading database
DEBUG:__main__:Records: {'john': 55, 'tom': 66}
INFO:__main__:Updating records ...
INFO:__main__:Finish updating recordsใช้ Dict ค่าโทร
import logging
import logging.config
import otherMod2
def main():
    """
    Based on http://docs.python.org/howto/logging.html#configuring-logging
    """
    dictLogConfig = {
        "version":1,
        "handlers":{
                    "fileHandler":{
                        "class":"logging.FileHandler",
                        "formatter":"myFormatter",
                        "filename":"config2.log"
                        }
                    },        
        "loggers":{
            "exampleApp":{
                "handlers":["fileHandler"],
                "level":"INFO",
                }
            },
        "formatters":{
            "myFormatter":{
                "format":"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
                }
            }
        }
    logging.config.dictConfig(dictLogConfig)
    logger = logging.getLogger("exampleApp")
    logger.info("Program started")
    result = otherMod2.add(7, 8)
    logger.info("Done!")
if __name__ == "__main__":
    main()otherMod2.py
import logging
def add(x, y):
    """"""
    logger = logging.getLogger("exampleApp.otherMod2.add")
    logger.info("added %s and %s to get %s" % (x, y, x+y))
    return x+yเอาท์พุต
2019-08-12 18:03:50,026 - exampleApp - INFO - Program started
2019-08-12 18:03:50,026 - exampleApp.otherMod2.add - INFO - added 7 and 8 to get 15
2019-08-12 18:03:50,027 - exampleApp - INFO - Done!