Python Logging is really much more complicated than I imagined, and the learning route is comparable to GIT. However, the alternatives is not enough, so we must attach importance to it. We must learn from it conscientiously and conscientiously.

learning Logging's purpose:

simple script is fine, print is enough.

, but a little more complicated, even if three or four files add up to two hundred or three hundred lines of code, debugging is starting to get complicated.

, plus the script that runs in the background, the investigation of running information is more complicated. At the beginning of

, I was still looking for a variety of crontab logs, or python background running view, or python stdout access and so on, all in the wrong direction. The real solution to

lies in the right logging. If the

is well recorded, I don't need to go to the python console to output stdout, nor do I need to find the log of crontab, just check the log file. Below

is the logging learning record of Python.

's simplest log output (no file record)

 import logging logging.error ("error appeared") ("print information") logging.warning ("warning information") 

first, forget ()! Forget it! All kinds of articles on the Internet about Python logging are too inconsiderate to the novice, and the complex things of logging want to be very simple, and use a variety of simple things to make a false image.

actually we really want to use the log, absolutely not directly using () and logging.basicConfig () , this is the official push out of this module to confuse people - it seems to let you one button, quickly see the results, but really do not match the actual!

so it's easy to explain later. We must warn this: forget both of them!

remember, the only thing that needs logging. is logging.getLogger () .

understands the workflow of logging

does not want to go up with things like flowcharts, but it is more confused.

simply says: the

logging module will automatically make your self customized logger object global ,

, that is, as long as you define a logger in your own module, such as log, so long as it is the other file that runs in the same module. Can read it.

, for example, you customize a logger in the main file, and you may set up what output files, what's the output format, and then you will refer to some other files or modules in, such as, so you don't have to set up anything in this, just use it. One logger = logging.getLogger (prior to's definition of the log name ') can get all the custom settings before.

, of course, in the invoked file (first called a sub module), with logging.getLogger ('log name') , it is best to add a . subname after the log name, such as main.sub. This output will show that a log is from this file. Of course, the front parent logger must be identical, and it will be identified.

then, the sub log can also be re - sublog, and even a sub module can let all the functions of another sub log, such as main.sub.func1. Logging will identify the relationship between subordinates and subordinates.

, in fact, is the kind of mechanism inherited by class class. You inherit according to the parent name, and then you can rewrite your new settings. After understanding these concepts,

can talk about code. In fact, it's much better to understand.

set the logger method

look, this article is more comprehensive and clear, many of the following are referred to its content: Python 101: An Intro to logging

generally want to customize a custom, such as let it output the message in what format to display, where to output to There are three ways to set up a file. There are three ways to set up:

  1. directly sets
  2. in Python code, configuring
  3. with
  4. to configure
  5. for Python in dict dictionaries, the purposes are the same, and few and inconvenient for dictionaries. The configuration file is better than the.Ini syntax is not very convenient to read, and it is not easy to do the dynamic setting of the variable, so it is generally written directly in the Python code. The common set of

    setting statements,

    , is a general description of the main entry file of the program. Be sure to define logger at the main entrance so that all the other sub modules can be inherited. The submodule def main () that will be invoked under import logging import otherMod2: "" "this file is the main entrance of the program" "" define_logger () log = logging.getLogger (") output information test =) (7, 8). This is the method from other modules ("Done!") def define_logger (): logger = logging.getLogger ("exampleApp") logger.setLevel (logging.INFO) logger.setLevel (logging.INFO) setting output format formatter = logging.Formatter ('% (asctime)% -% ()%%% -%) The processor FH = logging.FileHandler ("new_snake.log") fh.setFormatter (formatter) adds a format to the processor and sets the screen stdout output processor sh = logging.StreamHandler (stream=None) sh.setFormatter (formatter) and adds the processor to the logger. Ndler (SH) if __name__ = = "__main__": Main () 

    under the submodule's call method (very simple) R = logging.getLogger ("exampleApp.otherMod2.add") output test ("added%s and%s to get%s") can be defined in any place or in any single function or class. As long as you define the sub module before you call it, you can do it. Once defined, the log name is written down, and the sub module can be easily inherited.

    above is the whole content of this article, I hope it will help everyone's learning, and I hope you will support the script home.

    you may be interested in the article:

    This concludes the body part

    This paper fixed link: | Script Home | +Copy Link

    Article reprint please specify:Python Logging log entry learning | Script Home

    You may also be interested in these articles!