Python selenium2 Example-log management

Source: Internet
Author: User


Preface to Logger succession diagram

In the process of automated test practice, it is necessary to do log management, easy debugging and production problem tracking, Python provides logging module for log management. Below we will take a layer of logging module learning and use to promote the demonstration learning.

Python's logging module provides a common logging system that can be used by third-party modules or applications. This module provides different logging levels, and can be used in different ways to log records, such as files, HTTP get/post, SMTP, sockets, etc., and can even be customized to implement specific logging methods.

The logging module is the same as the Java log4j mechanism, but the specific language implementation details are somewhat different. The Python logging module provides basic classes such as logger, handler, filter, formatter, and so on.

1. Logger: Provides the log interface for application invocation. Logger the most commonly used operations are two main classes: Configuring and sending log messages.

2, Handler: Send log records to the appropriate purposes, such as files, sockets and so on. A Logger object can add 0 to n handler through the AddHandler method, and each hangdler can also define different log levels for log rating filtering.

3. Filter: Provides an elegant way to determine whether a log record is sent to handler.

4. Formatter: Specifies the output format of the log record. The formatter method requires two parameters: the format string of the message and the date string, which are optional.

By default, logging outputs the log to the console with a log level of warning.

Critical > ERROR > WARNING > INFO >debug > NOTSET, and of course you can customize the log level in logging by log-level size relationship.

Simple log

Let's take a look at a simple log example to output a log record to the console:

#-*-Coding:utf-8-*-

Import logging

if __name__ = = ' __main__ ':

Logging.debug (U ' This is bug level logging ')

Logging.info (U ' This is informational level logging ')

Logging.warning (U ' This is warning level logging ')

Output information in the console:

Warning:root: This is a warning level of logging

Why does it only output one? Because logging the log output level by default is: Wanring
Log format and level control

Next we look at how to control the output format and log level of the log. The code examples are as follows:

#-*-Coding:utf-8-*-

Import logging

if __name__ = = ' __main__ ':

Logging.basicconfig (level=logging. DEBUG, # Log level settings
format= "% (asctime) s% (filename) s [Line:% (Lineno) d]% (levelname) s% (message) s",
Datefmt= '%a,%d%b%Y%h:%m:%s ',
Filename= ' MyLog.log ',
Filemode= ' W '
)
Logging.debug (U ' This is debug level logging ')
Logging.info (U ' This is informational level logging ')
Logging.warning (U ' This is warning level logging ')

The contents of the MyLog.log file in the current directory are:
Mon, Mar 16:21:28 log.py [line:14] Debug This is debug level logging
Mon, Mar 16:21:28 log.py [line:15] Info This is informational level logging
Mon, Mar 16:21:28 log.py [line:16] WARNING This is warning level logging

Description of each parameter of Logging.basicconfig function

FileName: Specify the log output file name

FileMode: The same as the file function, specifying the open mode of the log file, ' W or a '

Format: Specifies the log output format and content, format can output a lot of useful information, as shown in the example above:

% (Levelno) S: Print the value of the log level

% (levelname) S: Print log level name

% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]

% (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

% (threadname) s: Print thread name

% (process) d: Print process ID

% (message) s: Print log information

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

Level: Specifies the log levels, which default to logging. WARNING

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

Log input Orientation

Let's look at how to output logs to both the console and the file, with the following code examples:

#-*-Coding:utf-8-*-

Import logging

if __name__ = = ' __main__ ':

Logging.basicconfig (level=logging. DEBUG, # Log level settings

format= "% (asctime) s% (filename) s [Line:% (Lineno) d]% (levelname) s% (message) s",

Datefmt= '%a,%d%b%Y%h:%m:%s ',

Filename= ' MyLog.log ',

Filemode= ' W ')

#####################################################
# define a Streamhandler to output logs of info-level or higher to the wrong error
# and add it to the current log processing object
console = logging. Streamhandler ()
Console.setlevel (Logging.info)
Formatter = logging. Formatter ('% (name) -12s:% (levelname) -8s% (message) s ')
Console.setformatter (Formatter)
Logging.getlogger ("). AddHandler (console)
####################################################
Logging.debug (U "This is debug logging")
Logging.info (U ' this is info log record ')
Logging.warning (U ' This is warning log record ')
Output the following log records in the console:
Root:info This is INFO logging
Root:warning This is WARNING log record

In the current directory, the contents of the MyLog.log file are:
Mon, Mar 17:32:43 log.py [line:26] Debug this is debug logging
Mon, Mar 17:32:43 log.py [line:27] Info this is info logging
Mon, Mar 17:32:43 log.py [line:28] WARNING this is WARNING logging

In this example, different levels of log redirection are output to different destinations, depending on the needs.

Log configuration

In all of the above examples, the log configuration is implemented in code, but in the actual application process, we generally need to dynamically configure the log information, or to meet the needs of custom, the following we have a custom log configuration for a sample demonstration:

# define a configuration file, here named Logger.conf, for the standard INI format file, the content is as follows

###############################################

###### three logger:root,demo01,demo01 are defined below

[Loggers]

Keys=root,demo01,demo01

[Logger_root]

Level=debug

Handlers=hand01,hand02

[LOGGER_DEMO01]

Handlers=hand01,hand02

Qualname=demo01

Propagate=0

[Logger_demo02]

Handlers=hand01,hand03

Qualname=demo02

Propagate=0

###############################################

# # # # # # # Three of handler:hand01,hand02,hand03 defined below

[Handlers]

Keys=hand01,hand02,hand03

[HANDLER_HAND01]

Class=streamhandler

Level=info

Formatter=form02

Args= (Sys.stderr,)

[HANDLER_HAND02]

Class=filehandler

Level=debug

Formatter=form01

args= (' MyLog.log ', ' a ')

[HANDLER_HAND03]

Class=handlers. Rotatingfilehandler

Level=info

Formatter=form02

args= (' MyLog.log ', ' a ', 10*1024*1024, 5)

###############################################

# # # The following defines two kinds of formatter:form01,form02

[Formatters]

Keys=form01,form02

[FORMATTER_FORM01]

format=% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s

Datefmt=%a,%d%b%Y%h:%m:%s

[FORMATTER_FORM02]

format=% (name) -12s:% (levelname) -8s% (message) s

datefmt=

Use the DEMO01 Logger code example:

#-*-Coding:utf-8-*-

Import logging

Import Logging.config

if __name__ = = ' __main__ ':

Logging.config.fileConfig ("logger.conf")

Logger = Logging.getlogger ("demo01")

Logger.debug (U ' This is demo01 debug logging ')

Logger.info (' u ' this is demo01 info log record ')

Logger.warning (U ' This is demo01 warning log record ')

The following is an example of using the Demo02 logger code:

#-*-Coding:utf-8-*-

Import logging

Import Logging.config

if __name__ = = ' __main__ ':

Logging.config.fileConfig ("logger.conf")

Logger = Logging.getlogger ("demo02")

Logger.debug (U ' This is demo02 debug logging ')

Logger.info (' u ' this is demo02 info log record ')

Logger.warning (U ' This is demo02 warning log record ')

Conclusion

In this paper, from the basic application of the log to the more advanced application mode layer to promote the demonstration, of course, in the actual automated testing practice, the logging module needs to be more advanced packaging to improve its reusability, to achieve high availability. For testers more need to strengthen the basic programming skills, improve the testing technology capabilities, more flexible application of a variety of basic technology.

Python selenium2 Example-log management

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.