Note that there are some explanatory texts on larger screens.

plurals
  1. POPythonic way to log specific things to a file?
    primarykey
    data
    text
    <p>I understand that Python loggers cannot be instantiated directly, as the documentation suggests:</p> <blockquote> <p>Note that Loggers are never instantiated directly, but always through the module-level function <code>logging.getLogger(name)</code></p> </blockquote> <p>.. which is reasonable, as you are expected <em>not</em> to create logger objects for every class/module for there is a <a href="https://stackoverflow.com/questions/401277/naming-python-loggers/402471#402471">better alternative</a>.</p> <p>However, there are cases where I want to <em>create</em> a logger object and attach a file to it exclusively for logging some app-specific output to that file; and then close the log file.</p> <p>For instance, I have a program that builds all packages in <a href="http://pypi.python.org/" rel="nofollow noreferrer">PyPI</a>. So basically assume there is a <code>for</code> loop going over every package. Inside the loop, I want to "create" a logger, attach a file handler (eg: /var/logs/pypi/django/20090302_1324.build.log and send the output of <code>python setup.py build</code> (along with other things) to this log file. Once that is done, I want to close/destroy the logger and <code>continue</code> building other packages in similar fashion.</p> <p>So you see .. the normal Pythonic way of calling <code>logging.getLogger</code> does not apply here. One needs to create <strong>temporary</strong> logger objects. </p> <p>Currently, I achieve this by passing the file name itself as the logger name:</p> <pre><code>&gt;&gt;&gt; packagelog = logging.getLogger('/var/..../..34.log') &gt;&gt;&gt; # attach handler, etc.. </code></pre> <p>I want to ask .. is there a better way to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload