Use of logs in Python web development-flask

Source: Internet
Author: User

Flask How to use logging:

1. Initialize Flask Application Example

Using logger in flask requires initializing an flask application

App = Flask (__name__)

2. Call Logger

Calling the Logger method directly

App.logger.info ("My first Logging")

The log at info level is recorded here

3. View Results

The results of the operation are as follows:

INFO in Code [d:/xxxxx/flask/code.py:20]:

My first logging

By default, flask logs are printed on the screen. But in general we need to log in a file, let's look at how to set the log record on the file.

1. Quoting logging

Because flask's log is referenced by Python's logging, the settings need to be done through Python's logging, as follows:

Logging

2. Set the path to the logging

Logging.basicconfig (filename="D:\ \flasklog\log.txt", format=' % (asctime) s-% (name) s-% (levelname) s-% (message) s ')

The above code set two parameters, filename set is the log file (note the permissions to set folder), the next parameter set the format of the log printing.

The common parameters in formatting are as follows:

% (name) s

Logger's name.

% (Levelno) s

Log level in digital form

% (LevelName) s

Log level in text form

% (pathname) s

The full path name of the module that called the log output function may not have

% (filename) s

The file name of the module that called the log output function

% (module) s

Call the module name of the log output function

% (FuncName) s

Function name of the call log output function

% (Lineno) d

The line of code where the statement that called the log output function is located

% (created) f

Current time, represented by the UNIX standard floating-point number representing the time

% (relativecreated) d

The number of milliseconds since logger was created when the log information is output

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

The thread ID. Probably not.

% (ThreadName) s

The name of the thread. Probably not.

% (process) d

The process ID. Probably not.

% (message) s

User-Output messages

3. View Results

After running again, you can see the log record in the TXT file under the path we set

Here we see there is a applogger, this is the name of the log, through Logger.name can set this name, such as:

App.logger.name="Applogger"

For details, please refer to the official documentation: Https://docs.python.org/3/library/logging.html#logger-objects

More articles about the public number "kick genius"

Use of logs in Python web development-flask

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.