Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sounds like the internals of your class should probably have a <a href="http://docs.python.org/2/library/logging.html#logger-objects" rel="noreferrer"><code>Logger</code></a> and that you'll want to add a <a href="http://docs.python.org/2/library/logging.handlers.html#filehandler" rel="noreferrer"><code>FileHandler</code></a> to the <code>Logger</code>. You might want to consider just using a factory method that creates <code>Logger</code>s and adds the handler instead of implementing your own class. You may need to create the directories that hold the log files. See <a href="https://stackoverflow.com/questions/273192/python-best-way-to-create-directory-if-it-doesnt-exist-for-file-write">this answer</a> for advice on creating directories.</p> <p>Edit:</p> <p>I don't think you need to write your own <code>Logger</code> class. Python's <code>logging</code> module has all the pieces you need. You probably just need a factory method. The key to realize is you need to create two separate, completely independent logging objects. You do this with <code>logging.getLogger</code>, and any time you pass it a different name, it gives you a different logger. You can use anything you want for the logger's name. For sure, you want to stay away from <code>basicConfig</code> for what you're doing. It's designed to be something simple for people who just want one <code>Logger</code> not doing anything too special.</p> <p>I think this demonstrates the functionality you're after. The key is create two different loggers with different handlers. Then use them separately. Keep in mind that my second call to <code>logging.getLogger</code> doesn't create a new logger; it gets the one we set up initially in <code>setup_logger</code>.</p> <p>log_test.py:</p> <pre><code>from __future__ import absolute_import import logging def setup_logger(logger_name, log_file, level=logging.INFO): l = logging.getLogger(logger_name) formatter = logging.Formatter('%(asctime)s : %(message)s') fileHandler = logging.FileHandler(log_file, mode='w') fileHandler.setFormatter(formatter) streamHandler = logging.StreamHandler() streamHandler.setFormatter(formatter) l.setLevel(level) l.addHandler(fileHandler) l.addHandler(streamHandler) def main(): setup_logger('log1', r'C:\temp\log1.log') setup_logger('log2', r'C:\temp\log2.log') log1 = logging.getLogger('log1') log2 = logging.getLogger('log2') log1.info('Info for log 1!') log2.info('Info for log 2!') log1.error('Oh, no! Something went wrong!') if '__main__' == __name__: main() </code></pre> <p>Sample run:</p> <pre><code>C:\temp&gt;C:\Python\27\python.exe logtest.py 2013-06-12 02:00:13,832 : Info for log 1! 2013-06-12 02:00:13,832 : Info for log 2! 2013-06-12 02:00:13,832 : Oh, no! Something went wrong! </code></pre> <p>log1.log:</p> <pre><code>2013-06-12 02:00:13,832 : Info for log 1! 2013-06-12 02:00:13,832 : Oh, no! Something went wrong! </code></pre> <p>log2.log:</p> <pre><code>2013-06-12 02:00:13,832 : Info for log 2! </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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