Subprocess: Need Help Sending Errors, and Output to the Log File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jool
    New Member
    • Nov 2006
    • 3

    #1

    Subprocess: Need Help Sending Errors, and Output to the Log File

    I have this code:
    Code:
    cmd_output =subprocess.Popen([cmd], shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
                        log.debug(cmd))
    I have a logger defined, and all the logs goes to the file. Now, what I want to do is, log output and error of subprocess to this log_file. Also I can you tell what does it do, when you set stderr=subproce ss.PIPE and stdout=subproce ss.PIPE, and where does the log get output and errors get written?
    Last edited by bartonc; Dec 15 '06, 01:11 AM. Reason: added code tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Python Library Reference: 6.29 logging -- Logging facility for Python
    Logging messages are subjected to a dispatch mechanism through the use of handlers, which are instances of subclasses of the Handler class. Handlers are responsible for ensuring that a logged message (in the form of a LogRecord) ends up in a particular location (or set of locations) which is useful for the target audience for that message (such as end users, support desk staff, system administrators, developers). Handlers are passed LogRecord instances intended for particular destinations. Each logger can have zero, one or more handlers associated with it (via the addHandler() method of Logger). In addition to any handlers directly associated with a logger, all handlers associated with all ancestors of the logger are called to dispatch the message.
    Sounds like you need to write the handler; maybe there are default handlers to write files for you.

    Comment

    • jool
      New Member
      • Nov 2006
      • 3

      #3
      Originally posted by bartonc
      Sounds like you need to write the handler; maybe there are default handlers to write files for you.
      I did setup a handler in a method called LogSetup:
      Code:
      def LogSetup():
          log = logging.getLogger("sitelogs")
          fmt = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
          
          log.setLevel(logging.DEBUG)
          fhnd = logging.FileHandler('logs.log', 'a')
          fhnd.setLevel(logging.DEBUG)
          fhnd.setFormatter(fmt)
          log.addHandler(fhnd)
      My question how would I use this with subprocess ?
      Last edited by bartonc; Dec 15 '06, 03:24 AM. Reason: added [code][/code] tags

      Comment

      Working...