Article From:https://www.cnblogs.com/cheese320/p/9061275.html
# !/user/bin/python
# -*- coding: utf-8 -*-
'''
subprocess : You need to test shell on the Linux platformLogging'''

import logging
# Output the log in the file
# logging.basicConfig(filename="app.log", level=logging.DEBUG)
logging.basicConfig(filename="app.log",
                    level=logging.WARNING,
                    format='%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s',
                    datefmt='%m/%d/%Y %I:%M:%S %p')  # Time is added to the log.%p stands for pm. TODO. Why didn't she take the number of trips?
logging.debug("test debug")
logging.info("test info")
logging.error("test error")
logging.warning("User [alex] attempted wrong password more than 3 times")


# At the same time, print the log on the screen and output it in the file.
# step 1, create logger
logger = logging.getLogger("TEST-LOG")
logger.setLevel(logging.DEBUG)

# step2, create console handler and set level to debug
ch=logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# step3, create file handler and set level to warning
fh = logging.FileHandler("process.log")
fh.setLevel(logging.ERROR)

# step3, define format
fh_formatter = logging.Formatter('%(asctime)s %(levelname)s  %(filename)s:%(lineno)d  - %(message)s')
ch_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh.setFormatter(fh_formatter)
ch.setFormatter(ch_formatter)

# step4, connect handlers to logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.warning("ddddd")

 

LevelWhen it’s used
DEBUGDetailed information, typically of interest only when diagnosing problems.
INFOConfirmation that things are working as expected.
WARNINGAn indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERRORDue to a more serious problem, the software has not been able to perform some function.
CRITICALA serious error, indicating that the program itself may be unable to continue running.
Link of this Article: Python logger /logging

Leave a Reply

Your email address will not be published. Required fields are marked *