Study notes (November 10)--python use of common built-in modules (logging, OS, command)

Source: Internet
Author: User

Four weeks five sessions (November 10)


First, logging

Log is our key tool to troubleshoot problems, write log records, and when we have problems, you can quickly locate the code range to modify. Python gives us developers a good log module, let's introduce the logging module:

First, let's look at an example:

Import Logginglogging.debug (' This was debug message ') Logging.info (' This is Info message ') logging.warning (' This is Warning message ')

Results:

WARNING:root:This is WARNING message

Explanation: We wrote three words, but only a warning level log was printed on the screen, what's going on?

By default, logging prints logs to the screen with log-level size relationships: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET, and of course you can define the log level yourself.

DEBUG: Detailed information, usually appearing only on diagnostic issues.

INFO: Make sure everything works as expected

WARNING: A warning, there may be some unexpected things happening, or indicate some problems in the near future (for example. Low disk space "). This software can work as expected

ERROR: A more serious problem, the software does not perform some functions

CRITICAL: A serious error that indicates that the program itself may not continue to run

and logging The default log level is warning, generally basically all programs are this level, help us to troubleshoot problems, but when there is a problem, we can not locate the problem, in many cases we need to raise the log level to the debug level, then what?


Configure the output format and mode of log by Logging.basicconfig function

In the real work, often we want to write the log in the log file, how to do it? Look at the following code:

Import Logginglogging.basicconfig (level=logging. DEBUG, format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ', datefmt= '%y/%m/%d%h:%m:%s ', Filename= ' Myapp.log ', filemode= ' w ') logger = Logging.getlogger (__name__) logging.debug (' This is Debug message ') Logging.info (' This was Info message ') logging.warning (' This is warning message ')

Results:

a new one has been added to the current folder Myapp.log file, the contents of the file are as follows:

2017/11/10 14:21:03 20171101.py[line:10] Debug this was DEBUG MESSAGE2017/11/10 14:21:03 20171101.py[line:11] INFO this is Info MESSAGE2017/11/10 14:21:03 20171101.py[line:12] WARNING This is WARNING message

Explain:

is mainly through Logging.basicconfig function to operate.


Logging.basicconfig function Use of parameters:

Level: Sets the log levels by default to logging. WARNING

FileName: Specifies the log file name.

FileMode: Same as file function, specify open mode of log file, ' W ' or ' a '

Format: Specifies the formats and contents of the output, format can output a lot of useful information, as in the example above:

% (levelname) S: Print log level name

% (filename) s: Prints the current name of the executing program

% (funcName) s: Print the current function of the log

% (Lineno) d: Print the current line number of the log

% (asctime) s: Time to print logs

% (thread) d: Print thread ID

% (process) d: Print process ID

% (message) s: Print log information

DATEFMT: Specify time format, same as Time.strftime ()

Stream: Specifies the output stream of the log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr, stream is ignored when stream and filename are specified simultaneously

Logging.getlogger ([name]): Create a Log object:

Returns a logger instance that returns root logger if no name is specified. As long as name is the same, the returned logger instances are the same and only one, that is, the name and the logger instance are one by one corresponding. This means that there is no need to pass logger instances in each module. As long as you know name, you can get the same logger instance.

Logging.getlogger (__name__) __name__ in the above example refers to __main__.


Second, OS module


Third, Command module


Four, sys module


Study notes (November 10)--python use of common built-in modules (logging, OS, command)

Related Article

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.