Python Learning---8.10-pack usage and Loggin module

Source: Internet
Author: User

First, the use of the package

1. What is a package

A package is a folder that contains a __init__.py file

Package is the essence of a module, that is, the package is used for import, the package contains files are also used to be imported

2. Why use the Package

The essence of a package is a folder, so the only function of the folder is to organize the files.

As we write more and more, we can't put all our functions into one file, so we use modules to organize our functions, and as we get more and more modules, we need to organize the module files in a folder to improve the structure and maintainability of the program.

Three things happen when you first import a package

1. Create a namespace based on the __init__.py file under the package

2. Execute the code for the __init__.py file under the package and throw the name generated during execution into the namespace

3. Get a name in the current execution file P1, which is the P1 that points to the __init__.py namespace.

3. Note

In Python2, there must be a __init__.py under the package, and even if there is no error in Python3

However, if you have a point in the import statement, the left side of the point must be a package

The import package is the __init__.py file under the Guide package

If you use absolute import, the starting location for absolute Imports is the starting point of the package's top-level directory

However, the import of the package internals should typically use a relative import, using. Represents the folder that is currently located (not the execution file). Represents the previous level

Emphasize:

Relative imports can only be imported between modules within a package

.. Upper level cannot exceed top level package

Second, logging module

1. Log level

Critical=50ERROR=40WARNING=30  #默认级别INFO=20DEBUG=10notest =0

2.logging module Specifies global configuration, valid for all logger, control printing to file

#====== IntroductionThe default behavior of the logging module can be changed by specific parameters in the Logging.basicconfig () function, the available parameters are filename: Create a filedhandler with the specified file name (the concept of handler is explained in the back), This allows the log to be stored in the specified file. FileMode: File is opened by using this parameter when filename is specified, and the default value is "a" and can be specified as "W". Format: Specifies the log display format used by handler. DATEFMT: Specifies the date time format. Level: Set Rootlogger (The following explains the specific concept) stream: Create Streamhandler with the specified stream. You can specify the output to Sys.stderr,sys.stdout or to a file, and the default is Sys.stderr. If you list both the filename and stream two parameters, the stream parameter is ignored. formatting strings that may be used in the format parameter:%(name) s Logger's name%(Levelno) s log level in digital form%(levelname) s log level in text form%(pathname) s The full pathname of the module that invokes the log output function, possibly without%(filename) s The file name of the module that invokes the log output function%module Name of the log output function called by (module) s%(funcName) s function name of the call log output function%(Lineno) d The line of code where the statement that invokes the log output function%(created) F current time, represented by the UNIX standard floating-point number representing the time%(relativecreated) d when the log information is output, the number of milliseconds since logger was created% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds%(thread) d thread ID. Probably not .%(threadname) s thread name. Probably not .%(process) d ID. Probably not .%(message) s user-output messages#======== UseImportlogginglogging.basicconfig (filename='Access.log', Format='% (asctime) s-% (name) s-% (LevelName) s-% (module) s:% (message) s', Datefmt='%y-%m-%d%h:%m:%s%p', level=10) Logging.debug ('Debugging Debug') Logging.info ('Message Info') logging.warning ('warning Warn') Logging.error ('Wrong Error') logging.critical ('Severe critical')#======== ResultsAccess.log Content:2017-07-28 20:32:17 Pm-root-debug-Test: Debugging Debug2017-07-28 20:32:17 Pm-root-info-Test: Message Info2017-07-28 20:32:17 pm-root-warning-test: Warning Warn2017-07-28 20:32:17 Pm-root-error-Test: Err Error2017-07-28 20:32:17 pm-root-critical-test: Critical Criticalpart2: You can specify the module-level configuration for the logging module, which is the configuration of all logger
View Code

Formatter,handler,logger,filter Objects for 3.logging modules

Logger: The object that generated the log

Filter: Object that filters logs

Handler: Accept the log and then control print to a different place, Filehandler used to print to the file, Streamhandler used to print to the terminal

Formatter: You can customize different log format objects and then bind to different handler objects to control the different handler log formats

      

" "critical=50error =40warning =30info = 20debug =10" "ImportLogging#1, Logger object: Responsible for generating the log, and then to filter filtering, and then to different handler outputLogger=logging.getlogger (__file__)#2. Filter object: Not commonly used, slightly#3, Handler object: Receive logger from the log, and then control the outputH1=logging. Filehandler ('T1.log')#Print to FileH2=logging. Filehandler ('T2.log')#Print to FileH3=logging. Streamhandler ()#Print to Terminal#4. Formatter object: Log FormatFormmater1=logging. Formatter ('% (asctime) s-% (name) s-% (LevelName) s-% (module) s:% (message) s', Datefmt='%y-%m-%d%h:%m:%s%p',) Formmater2=logging. Formatter ('% (asctime) s:% (message) s', Datefmt='%y-%m-%d%h:%m:%s%p',) Formmater3=logging. Formatter ('% (name) s% (message) s',)#5, for handler object binding fixed-typeH1.setformatter (formmater1) h2.setformatter (formmater2) h3.setformatter (FORMMATER3)#6. Add handler to logger and set the log levelLogger.addhandler (H1) Logger.addhandler (H2) Logger.addhandler (H3) Logger.setlevel (10)#7. TestingLogger.debug ('Debug') Logger.info ('Info') logger.warning ('Warning') Logger.error ('Error') logger.critical ('Critical')
View Code

Levels of 4.Logger and handler

Logger is the first level of filtering, and then to handler, in order to set the level of these two, it is best to set the same level.

Python Learning---8.10-pack usage and Loggin module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.