The log module in Python

Source: Internet
Author: User

The simplest usage:
ImportLogginglogging.debug ("Logging Debug") Logging.info ("Logging Info") logging.warning ("user [Qianduoduo] attempted wrong password more than 3 times") Logging.error ("Logging Error") logging.critical ("Logging Critical")#OutputWARNING:root:user [Qianduoduo] attempted wrong password more than 3timesERROR:root:logging errorCRITICAL:root:logging Critical#root is the default user name

Focus: Why the above Debug and info No output, that is because a module default log level is warning, lower than his level will not output

Write the logs to the file system.
Import logginglogging.basicconfig (filename= ' Duoduo.log ',                    level=logging. DEBUG,                    format= '% (asctime) s% (message) s ', #asctime字符串形式的当前时间, message user output messages                    datefmt= '%y-%m-%d%i:%m:%s%p ') Logging.debug ("Logging Debug") Logging.info ("Logging info") logging.warning ("User [Qianduoduo] attempted wrong Password more than 3 times ") Logging.error (" Logging Error ") logging.critical (" Logging critical ") #输出到文件 ' Duoduo.log ' 2018-01-27 07:33:30 pm  Logging debug2018-01-27 07:33:30 pm  Logging info2018-01-27 07:33:30 pm  User [ Qianduoduo] attempted wrong password more than 3 times2018-01-27 07:33:30 pm  Logging error2018-01-27 07:33:30 pm  L Ogging Critical

log format for format

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

Configure log with configuration file
{"Version": 1, "disable_existing_loggers": false, "formatters": {"simple": {"format": "% (asctime) S-% (name) s-% (levelname) s-% (message) S "}}," handlers ": {" console ": {" class ":" Logging.        Streamhandler "," Level ":" DEBUG "," Formatter ":" Simple "," stream ":" Ext://sys.stdout "            }, "Info_file_handler": {"Class": "Logging.handlers.RotatingFileHandler", "Level": "Info", "Formatter": "Simple", "filename": "Logs/info.log", "MaxBytes": 10485760, "Backupcou NT ": $," encoding ":" UTF8 "}," Error_file_handler ": {" Class ":" Logging.handlers.Rotating            Filehandler "," Level ":" ERROR "," Formatter ":" Simple "," filename ":" Logs/errors.log ", "MaxBytes": 10485760, "Backupcount": "Encoding": "UTF8"}}, "loggers": {"            My_module ": {"Level": "DEBUG", "Handlers": ["Info_file_handler"], "propagate": "No"}, "root": { "Level": "INFO", "Handlers": ["Console", "Info_file_handler", "Error_file_handler"]}}

Referencing in programs

Import jsonimport loggingimport logging.configimport osdef setup_logging (default_path= "Logging.json", default_level= Logging.info, env_key= "Log_cfg"):    Path = default_path    value = os.getenv (Env_key, None)    if value:        path = value    if os.path.exists (path):        with open (path, "R") as f:            config = json.load (f)            Logging.config.dictConfig (config)    else:        logging.basicconfig (level=default_level) If __name__ = = "__main__" :    setup_logging (default_path= "Logging.json")

The log module in Python

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.