Python logger not working with subproccess










0















I am attempting to run code from one program (program1) via the usage of a subproccess.popen call. Program 2(the one being called) does not display logger statements. In the example code below program1 calls program2 with logging setup for each code.



program1.py



import subprocess
import logging
import sys

def configure_logging(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger

logger = configure_logging(__name__)
python_path = "Path/to/specific/pythonexe"
py_path = "Path/to/program2.py"
cmd = " ".format(python_path,py_path)
logger.info("Cmd: ".format(cmd))
cmd = cmd.split()
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)


program2.py



import logging
def logstuff(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger

def main():
logger.info("hi")
print("hi2")

if __name__ == "__main__":
logger = logstuff(__name__)
main()


Using the above setup program1 calls program2 but the logger.info statement is not printed out only the print() statement is.



I can't import program2 as it relies on a specific python version encased in an anaconda environment.



My desired output would be to have the logger.info and any subsequent logger calls printed to terminal so how would I go about achieving this?










share|improve this question


























    0















    I am attempting to run code from one program (program1) via the usage of a subproccess.popen call. Program 2(the one being called) does not display logger statements. In the example code below program1 calls program2 with logging setup for each code.



    program1.py



    import subprocess
    import logging
    import sys

    def configure_logging(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)
    logger.addHandler(stream_handler)
    return logger

    logger = configure_logging(__name__)
    python_path = "Path/to/specific/pythonexe"
    py_path = "Path/to/program2.py"
    cmd = " ".format(python_path,py_path)
    logger.info("Cmd: ".format(cmd))
    cmd = cmd.split()
    proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)


    program2.py



    import logging
    def logstuff(name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)
    logger.addHandler(stream_handler)
    return logger

    def main():
    logger.info("hi")
    print("hi2")

    if __name__ == "__main__":
    logger = logstuff(__name__)
    main()


    Using the above setup program1 calls program2 but the logger.info statement is not printed out only the print() statement is.



    I can't import program2 as it relies on a specific python version encased in an anaconda environment.



    My desired output would be to have the logger.info and any subsequent logger calls printed to terminal so how would I go about achieving this?










    share|improve this question
























      0












      0








      0








      I am attempting to run code from one program (program1) via the usage of a subproccess.popen call. Program 2(the one being called) does not display logger statements. In the example code below program1 calls program2 with logging setup for each code.



      program1.py



      import subprocess
      import logging
      import sys

      def configure_logging(name):
      logger = logging.getLogger(name)
      logger.setLevel(logging.DEBUG)
      formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
      stream_handler = logging.StreamHandler()
      stream_handler.setFormatter(formatter)
      logger.addHandler(stream_handler)
      return logger

      logger = configure_logging(__name__)
      python_path = "Path/to/specific/pythonexe"
      py_path = "Path/to/program2.py"
      cmd = " ".format(python_path,py_path)
      logger.info("Cmd: ".format(cmd))
      cmd = cmd.split()
      proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)


      program2.py



      import logging
      def logstuff(name):
      logger = logging.getLogger(name)
      logger.setLevel(logging.DEBUG)
      formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
      stream_handler = logging.StreamHandler()
      stream_handler.setFormatter(formatter)
      logger.addHandler(stream_handler)
      return logger

      def main():
      logger.info("hi")
      print("hi2")

      if __name__ == "__main__":
      logger = logstuff(__name__)
      main()


      Using the above setup program1 calls program2 but the logger.info statement is not printed out only the print() statement is.



      I can't import program2 as it relies on a specific python version encased in an anaconda environment.



      My desired output would be to have the logger.info and any subsequent logger calls printed to terminal so how would I go about achieving this?










      share|improve this question














      I am attempting to run code from one program (program1) via the usage of a subproccess.popen call. Program 2(the one being called) does not display logger statements. In the example code below program1 calls program2 with logging setup for each code.



      program1.py



      import subprocess
      import logging
      import sys

      def configure_logging(name):
      logger = logging.getLogger(name)
      logger.setLevel(logging.DEBUG)
      formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
      stream_handler = logging.StreamHandler()
      stream_handler.setFormatter(formatter)
      logger.addHandler(stream_handler)
      return logger

      logger = configure_logging(__name__)
      python_path = "Path/to/specific/pythonexe"
      py_path = "Path/to/program2.py"
      cmd = " ".format(python_path,py_path)
      logger.info("Cmd: ".format(cmd))
      cmd = cmd.split()
      proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)


      program2.py



      import logging
      def logstuff(name):
      logger = logging.getLogger(name)
      logger.setLevel(logging.DEBUG)
      formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
      stream_handler = logging.StreamHandler()
      stream_handler.setFormatter(formatter)
      logger.addHandler(stream_handler)
      return logger

      def main():
      logger.info("hi")
      print("hi2")

      if __name__ == "__main__":
      logger = logstuff(__name__)
      main()


      Using the above setup program1 calls program2 but the logger.info statement is not printed out only the print() statement is.



      I can't import program2 as it relies on a specific python version encased in an anaconda environment.



      My desired output would be to have the logger.info and any subsequent logger calls printed to terminal so how would I go about achieving this?







      python logging






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 14:03









      cd123cd123

      96119




      96119






















          1 Answer
          1






          active

          oldest

          votes


















          1














          I believe you messed up with the logger setup. Also the subprocess.Popen with stdout PIPE and such didn't work for me. Here's what I got working locally, hope it helps.



          program1.py



          import logging
          import subprocess

          logger = logging.getLogger(__name__)
          logger.setLevel(logging.DEBUG)
          formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
          stream_handler = logging.StreamHandler()
          stream_handler.setFormatter(formatter)
          logger.addHandler(stream_handler)

          python_path = 'python'
          py_path = 'program2.py'
          cmd = ' '.format(python_path, py_path)
          logger.info('Cmd: '.format(cmd))
          cmd = cmd.split()
          subprocess.call(cmd)


          program2.py



          import logging
          logger = logging.getLogger(__name__)
          logger.setLevel(logging.DEBUG)
          formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
          stream_handler = logging.StreamHandler()
          stream_handler.setFormatter(formatter)
          logger.addHandler(stream_handler)


          def main():
          logger.info('hi program2 here')

          if __name__ == '__main__':
          main()


          Output:



          $ python program1.py 
          2018-11-14 15:38:53,905:__main__:Cmd: python program2.py
          2018-11-14 15:38:53,917:__main__:hi program2 here





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302037%2fpython-logger-not-working-with-subproccess%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I believe you messed up with the logger setup. Also the subprocess.Popen with stdout PIPE and such didn't work for me. Here's what I got working locally, hope it helps.



            program1.py



            import logging
            import subprocess

            logger = logging.getLogger(__name__)
            logger.setLevel(logging.DEBUG)
            formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
            stream_handler = logging.StreamHandler()
            stream_handler.setFormatter(formatter)
            logger.addHandler(stream_handler)

            python_path = 'python'
            py_path = 'program2.py'
            cmd = ' '.format(python_path, py_path)
            logger.info('Cmd: '.format(cmd))
            cmd = cmd.split()
            subprocess.call(cmd)


            program2.py



            import logging
            logger = logging.getLogger(__name__)
            logger.setLevel(logging.DEBUG)
            formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
            stream_handler = logging.StreamHandler()
            stream_handler.setFormatter(formatter)
            logger.addHandler(stream_handler)


            def main():
            logger.info('hi program2 here')

            if __name__ == '__main__':
            main()


            Output:



            $ python program1.py 
            2018-11-14 15:38:53,905:__main__:Cmd: python program2.py
            2018-11-14 15:38:53,917:__main__:hi program2 here





            share|improve this answer



























              1














              I believe you messed up with the logger setup. Also the subprocess.Popen with stdout PIPE and such didn't work for me. Here's what I got working locally, hope it helps.



              program1.py



              import logging
              import subprocess

              logger = logging.getLogger(__name__)
              logger.setLevel(logging.DEBUG)
              formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
              stream_handler = logging.StreamHandler()
              stream_handler.setFormatter(formatter)
              logger.addHandler(stream_handler)

              python_path = 'python'
              py_path = 'program2.py'
              cmd = ' '.format(python_path, py_path)
              logger.info('Cmd: '.format(cmd))
              cmd = cmd.split()
              subprocess.call(cmd)


              program2.py



              import logging
              logger = logging.getLogger(__name__)
              logger.setLevel(logging.DEBUG)
              formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
              stream_handler = logging.StreamHandler()
              stream_handler.setFormatter(formatter)
              logger.addHandler(stream_handler)


              def main():
              logger.info('hi program2 here')

              if __name__ == '__main__':
              main()


              Output:



              $ python program1.py 
              2018-11-14 15:38:53,905:__main__:Cmd: python program2.py
              2018-11-14 15:38:53,917:__main__:hi program2 here





              share|improve this answer

























                1












                1








                1







                I believe you messed up with the logger setup. Also the subprocess.Popen with stdout PIPE and such didn't work for me. Here's what I got working locally, hope it helps.



                program1.py



                import logging
                import subprocess

                logger = logging.getLogger(__name__)
                logger.setLevel(logging.DEBUG)
                formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
                stream_handler = logging.StreamHandler()
                stream_handler.setFormatter(formatter)
                logger.addHandler(stream_handler)

                python_path = 'python'
                py_path = 'program2.py'
                cmd = ' '.format(python_path, py_path)
                logger.info('Cmd: '.format(cmd))
                cmd = cmd.split()
                subprocess.call(cmd)


                program2.py



                import logging
                logger = logging.getLogger(__name__)
                logger.setLevel(logging.DEBUG)
                formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
                stream_handler = logging.StreamHandler()
                stream_handler.setFormatter(formatter)
                logger.addHandler(stream_handler)


                def main():
                logger.info('hi program2 here')

                if __name__ == '__main__':
                main()


                Output:



                $ python program1.py 
                2018-11-14 15:38:53,905:__main__:Cmd: python program2.py
                2018-11-14 15:38:53,917:__main__:hi program2 here





                share|improve this answer













                I believe you messed up with the logger setup. Also the subprocess.Popen with stdout PIPE and such didn't work for me. Here's what I got working locally, hope it helps.



                program1.py



                import logging
                import subprocess

                logger = logging.getLogger(__name__)
                logger.setLevel(logging.DEBUG)
                formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
                stream_handler = logging.StreamHandler()
                stream_handler.setFormatter(formatter)
                logger.addHandler(stream_handler)

                python_path = 'python'
                py_path = 'program2.py'
                cmd = ' '.format(python_path, py_path)
                logger.info('Cmd: '.format(cmd))
                cmd = cmd.split()
                subprocess.call(cmd)


                program2.py



                import logging
                logger = logging.getLogger(__name__)
                logger.setLevel(logging.DEBUG)
                formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
                stream_handler = logging.StreamHandler()
                stream_handler.setFormatter(formatter)
                logger.addHandler(stream_handler)


                def main():
                logger.info('hi program2 here')

                if __name__ == '__main__':
                main()


                Output:



                $ python program1.py 
                2018-11-14 15:38:53,905:__main__:Cmd: python program2.py
                2018-11-14 15:38:53,917:__main__:hi program2 here






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 14:40









                The PjotThe Pjot

                7771416




                7771416





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302037%2fpython-logger-not-working-with-subproccess%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto