How to get a Logger that Prints to Console and to File in fewest lines
<p>This is how I obtain a logger that prints to both console and a file:</p>

<pre><code>import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
handler2 = logging.FileHandler(os.path.join(os.path.split(os.path.abspath(__file__))[0], 'my.log'), mode='w')
handler2.setFormatter(formatter)
logger.addHandler(handler)
logger.addHandler(handler2)
</code></pre>

<p>Is there a way to accomplish something similar to this in fewer operations? I don't particularly care about the mode of the file or the filename.</p>

<p>Edit: The use case for this is when I'm prototyping a new script and do not wish to spend the time writing a configuration file until later.</p>

<p>I'm curious: why do you want that?</p>

<p>You can use an external configuration in an INI file and load it with <code>logging.config.fileConfig</code>.</p>

<p>Or</p>

<p>Create your own handle which combine file and console handlers.</p>

<p>Or</p>

<p>Create a file-like object which writes it a file and in the console. Then use this file with <code>logging.basicConfig</code>. </p>

python logging