Get the App
SLTechnology News&Howtos  ›  Development  › 

Description and example usage of python logging

Shulou Source: shulou.com Published: 2022-06-03 17:30:52 10月17日 Update

This article mainly introduces "the explanation and example usage of python logging". In the daily operation, I believe that many people have doubts about the explanation and example usage of python logging. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "explanation and example usage of python logging". Next, please follow the editor to study!

Description

1. Output file, console and Elasticsearch.

Output to the console is only convenient for direct viewing.

2. The output to the file is stored directly and the backup of all historical records is kept.

3. Output to Elasticsearch, directly as the center of storage and analysis.

Kibana makes it very easy to analyze and view the operation.

Example

Import loggingimport sysfrom os import makedirsfrom os.path import dirname Exists from cmreslogging.handlers import CMRESHandler loggers = {} LOG_ENABLED = True # whether log is enabled _ TO_CONSOLE = True # whether to output to the console LOG_TO_FILE = True # whether to output to a file LOG_TO_ES = True # whether to output to Elasticsearch LOG_PATH ='. / runtime.log' # log file path LOG_LEVEL = 'DEBUG' # log level LOG_FORMAT ='% (levelname) s -% (asctime) s-process:% (process) d -% (filename) s -% (name) s -% (lineno) d -% (module) s -% (message) s'# per log output format ELASTIC_SEARCH_HOST = 'eshost' # Elasticsearch HostELASTIC_SEARCH_PORT = 9200 # Elasticsearch PortELASTIC_SEARCH_INDEX =' runtime' # Elasticsearch Index NameAPP_ENVIRONMENT = 'dev' # runtime environment For example, test environment or production environment def get_logger (name=None): "get logger by name: param name: name of logger: return: logger"global loggers if not name: name= _ _ name__ if loggers.get (name): return loggers.get (name) logger = logging.getLogger (name) logger.setLevel (LOG_LEVEL) # output to the console If LOG_ENABLED and LOG_TO_CONSOLE: stream_handler = logging.StreamHandler (sys.stdout) stream_handler.setLevel (level=LOG_LEVEL) formatter = logging.Formatter (LOG_FORMAT) stream_handler.setFormatter (formatter) logger.addHandler (stream_handler) # output to file if LOG_ENABLED and LOG_TO_FILE: # if the path does not exist Create a log file folder log_dir = dirname (log_path) if not exists (log_dir): makedirs (log_dir) # add FileHandler file_handler = logging.FileHandler (log_path) Encoding='utf-8') file_handler.setLevel (level=LOG_LEVEL) formatter = logging.Formatter (LOG_FORMAT) file_handler.setFormatter (formatter) logger.addHandler (file_handler) # output to Elasticsearch if LOG_ENABLED and LOG_TO_ES: # add CMRESHandler es_handler = CMRESHandler (hosts= [{'host': ELASTIC_SEARCH_HOST,' port': ELASTIC_SEARCH_PORT}] # you can configure the corresponding authentication permissions auth_type=CMRESHandler.AuthType.NO_AUTH and es_index_name=ELASTIC_SEARCH_INDEX # one Index index_name_frequency=CMRESHandler.IndexNameFrequency.MONTHLY a month # add additional environment ID es_additional_fields= {'environment': APP_ENVIRONMENT}) es_handler.setLevel (level=LOG_LEVEL) formatter = logging.Formatter (LOG_FORMAT) es_handler.setFormatter (formatter) Logger.addHandler (es_handler) # Save to global loggers loggers [name] = logger return logger to this The study on "explanation and example usage of python logging" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Output file instance log console environment learning control more path analysis storage help run practical next global history just backup Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Shulou Technology NVidia Huawei