Where does stuff "print" to when not running application from terminal?
Asked Answered
C

3

5

So I have a python application that is being bundled into a .app using py2app. I have some debugging print statements in there that would normally print to stdout if I was running the code in the terminal. If I just open the bundled .app, obviously I don't see any of this output. Is any of this actually being printed somewhere even though I don't see it?

Crichton answered 9/7, 2013 at 16:13 Comment(2)
You should probably be using python's logging module to begin with, but have you tried looking in Console.app?Altercation
yeah i thought it might be there, but i didn't see it there. Does the logging module make it display in console.app?Crichton
B
4

It goes to standard output (so in the command line, if you are opening it in command line, or eg. to the log, if you are running it with cron jobs).

For more advanced and flexible usage try using built-in logging support (logging module). It allows you to direct messages to the console, to the file, stream (like external logging server), syslog process, email etc., as well as filter based on log message level. Debug statements (logging statements) can then be still placed in the production code, but configured to not cause any logging other than errors. Very useful and simplifies logging a lot.

Brachium answered 9/7, 2013 at 16:16 Comment(2)
nice, this seems to be more of what I need. Is it possible to use the logging module to print things to stdout as well as to a Log, that way if I run something in the terminal, I'll just see the output in the terminal?Crichton
@vik: Yes, it is possible to have several handlers for any of the loggers. Furthermore, you can create pretty complex logging structures (hierarchical loggers, with or without propagation for each of them, different handlers, filters and formatters). Python logging was actually a solution that became a base for some other, newer solutions (such as Symfony's Monolog).Brachium
A
6

Where the stdout and stderr streams are redirect to depends on how you run the application, and on which OSX release you are.

When you run the application from the Terminal ("MyApp.app/Contents/MacOS/MyApp") the output ends up in the terminal window.

When you run the application by double clicking, or using the open(1) command, the output ends up in Console.app when using OSX before 10.8 and is discarded on OSX 10.8.

I have a patch that redirects output to the logs that Console.app reads even for OSX 10.8, but that is not in a released version at this point in time.

P.S. I'm the maintainer of py2app.

Asur answered 10/7, 2013 at 9:3 Comment(0)
B
4

It goes to standard output (so in the command line, if you are opening it in command line, or eg. to the log, if you are running it with cron jobs).

For more advanced and flexible usage try using built-in logging support (logging module). It allows you to direct messages to the console, to the file, stream (like external logging server), syslog process, email etc., as well as filter based on log message level. Debug statements (logging statements) can then be still placed in the production code, but configured to not cause any logging other than errors. Very useful and simplifies logging a lot.

Brachium answered 9/7, 2013 at 16:16 Comment(2)
nice, this seems to be more of what I need. Is it possible to use the logging module to print things to stdout as well as to a Log, that way if I run something in the terminal, I'll just see the output in the terminal?Crichton
@vik: Yes, it is possible to have several handlers for any of the loggers. Furthermore, you can create pretty complex logging structures (hierarchical loggers, with or without propagation for each of them, different handlers, filters and formatters). Python logging was actually a solution that became a base for some other, newer solutions (such as Symfony's Monolog).Brachium
T
-1

I think you may want to take a look at module named logging

You can than use this module to write your own logs into a separate file, for example, that's how I do this:

import logging

# Create a log file for the future debug
LOG_FILE_NAME = 'my-script.log'
log_path = path = os.path.join(os.path.expanduser('~'), '/mnt/logs/') 
if not os.path.exists(log_path):
    os.makedirs(log_path)
logger = logging.getLogger('my-script')
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler(log_path+LOG_FILE_NAME)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)

class Foo()

    def log(self, msg, level=None):
        print "LOG %s" % msg
        if level is not None:
            if level == 'INFO':
                logger.info(msg)
            elif level == 'WARNING':
                logger.warn(msg)
            elif level == 'ERROR':
                logger.error(msg)
            elif level == 'CRITICAL':
                logger.critical(msg)
        else:
            logger.debug(msg) # DEBUG

    self.log('Some text here', 'DEBUG') # I use self.log instead of print
Tittletattle answered 9/7, 2013 at 16:19 Comment(6)
Two things: 1) Why do you insist on overriding standard logging behaviour? It looks like this approach does not give you any advantage? 2) This approach makes any logging configuration to be overwritten, while better approach is to just give standard handler (null/no-op handler) and handle logging configuration elsewhere (as an example, take a look at Django's logging configuration).Brachium
So this would print the msg to stdout as well as log it?Crichton
Yes it would print it if you run in in terminal and also write them in log file.Tittletattle
Can you please change my answer to show what you mean on give standard handlerTittletattle
I think it should be log_path = os.path.join(os.path.expanduser('~'), 'mnt/logs/') because if you use '/mnt/logs/' the result is just that since it has a leading slash.Devoirs
@Vor: Here is information on how to do that for NullHandler, and explanation on why it should not be configured the way you have proposed: docs.python.org/2/howto/…Brachium

© 2022 - 2024 — McMap. All rights reserved.