python logging basic how to use -


python logging helps me create transcripts of background processing scripts.

i have far used basic configuration:

import logging log_filename = 'example.log' logging.basicconfig(filename=log_filename,level=logging.debug)  logging.debug('this message should go log file') 

as explained in python docs: 15.6. logging. nice thing of doing way can use line:

logging.debug('this message should go log file') 

throughout modules , classes without bothering of logging configurations. setup logging in main script go file , messages go same file. easy.

there way of doing same, goes along these lines:

my_logger = logging.getlogger('mylogger') my_logger.setlevel(logging.debug) my_logger.debug('test string') 

which same thing except have instance keep track of: my_logger. modules , classes don't know how logger of day named , having pass variable around cumbersome. suspect miss basic here, either use of logging module faulty , works glitch or 2nd method must easy well.

i want switch 2nd method because seems way make use of additional features such rotatingfilehandler.

from documentation (https://docs.python.org/2/library/logging.html):

multiple calls getlogger() same name return reference same logger object.

so away without having pass variable around.

furthermore, loggers form hierarchy:

"loggers names of foo.bar, foo.bar.baz, , foo.bam descendants of foo"

so documentation recommends using:

logging.getlogger(__name__) 

and in way, organize loggers in same way organize modules.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -