Software development program ape daily necessities, now available check-log record

Keywords: Python encoding Unix

logging module

Logging Uses:
1. Recording User Information
2. Recording personal running water
3. Recording the running status of the software
4. Record the instructions issued by the programmer (springboard machine)
5. For programmer code debugging (print consumes memory and is not recommended during development and testing)

Functional simple configuration

importlogging
logging.debug()     #debug debugging
logging.info()      #info information
logging.warning()   # info warning
logging.error()     # Error error
logging.critical()  # critical danger
  • The information to be recorded in the log is recorded by default from warning
  • By default, Python's logging module prints the logs to standard output, and only shows logs greater than or equal to the WARNING level. The level logs behind do not show, which means that the default log level is set to WARNING.
  • (Log level: CRITICAL (50) > ERROR (40) > WARNING (30) > INFO (20) > DEBUG (10))
  • The default log format is log level: Logger name: user output message

Manual gear - already configured, take it over and use it directly

import logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename="test.log",
                    filemode="a",
)

logging.debug('Debugging messages')
logging.info('information message')
logging.warning('Warning message')
logging.error('Error message')
logging.critical('Key information')
Automatic Gear - You need to configure it yourself
import logging
//Initialize an empty log
logger = logging.getLogger()   # Create an object
//Create a file for logging information
fh = logging.FileHandler('test.log',encoding='utf-8')
# Create a file for logging information

fh1 = logging.FileHandler('test1.log',encoding='utf-8')

# Create something that can be output on the screen

ch = logging.StreamHandler()

# Define the format of the information to be recorded

msg = logging.Formatter('%(asctime)s - [line:%(lineno)d] %(filename)s - %(levelname)s - %(message)s')

# Define the format of the information to be recorded

msg1 = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

# Setting Record Level

logger.setLevel(10) or logger.setLevel(logging.DEBUG)

# Rank correspondence table

'''
DEBUG - 10
INFO - 20
WARNING - 30
ERROR - 40
CRITICAL - 50
'''

# Bind the format we set to the file

fh.setFormatter(msg)
fh1.setFormatter(msg)

# Bind our format to the screen

ch.setFormatter(msg1)

# Binding files that set up to store log information to logger logs

logger.addHandler(fh) #logger objects can add multiple fh and ch objects
logger.addHandler(fh1)
logger.addHandler(ch)

# Logging

logger.debug([1,2,3,4,])
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')

In the basic Config () function, the default behavior of the logging module can be changed by specific parameters, such as:

  • Filename: Create FiledHandler with the specified filename, so that the log is stored in the specified file.
  • filemode: File opening mode, which is used when filename is specified. The default value is "a" and "w".
  • Format: Specifies the log display format used by handler.
  • datefmt: Specifies the date and time format.
  • Level: Sets the level of the logging
  • Stream: Create StreamHandler with the specified stream. You can specify output to
  • sys.stderr,sys.stdout, or file (f=open('test.log','w'), defaults to sys.stderr. If both filename and stream parameters are listed at the same time, the stream parameters will be ignored.

Formatting strings that may be used in format parameters:

  • (name)s Logger's name
  • Log level in digital form of%(levelno)s
  • (level name) s text-based log level
  • (pathname)s The full pathname of the module calling the log output function, which may not be available
  • (filename)s The file name of the module that calls the log output function
  • (module)s calls the module name of the log output function
  • (funcName)s calls the function name of the log output function
  • (lineno)d The line of code where the statement calling the log output function is located
  • (created)f Current time, expressed in UNIX standard floating point numbers for time
  • The number of milliseconds since Logger was created when (relativeCreated)d outputs log information
  • (asctime) the current time in the form of a string. The default format is "2003-07-08 16:49:45,896". After the comma is milliseconds.
  • (thread)d thread ID. Maybe not.
  • (threadName)s thread name. Maybe not.
  • (process)d process ID. Maybe not.
  • (message)s User Output Message

Posted by Skull on Tue, 27 Aug 2019 03:39:48 -0700