Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using logging statements for development #11

Open
carterbox opened this issue Oct 26, 2018 · 4 comments
Open

Using logging statements for development #11

carterbox opened this issue Oct 26, 2018 · 4 comments
Labels
enhancement New feature or request

Comments

@carterbox
Copy link
Member

carterbox commented Oct 26, 2018

We want to set up a system for logging information about the progress of reconstruction algorithms such as convergence criteria and which number of iterations have been completed. This system should probably use the standard logger of python and print statements for storing information optionally.

Instead of changing the API of tike to accommodate development, we can use the following tools to capture data in text files and display messages to stout.

Example

Here's an example that demonstrates these concepts.

dev_logging.py:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Define an example of the logging system to be used in tike."""

import numpy as np
import logging
# First set the logging level to logging.INFO.
logging.basicConfig(level=logging.INFO)
# Then we need to grab a reference to the existing logger or make a new one.
logger = logging.getLogger(__name__)

if __name__ == '__main__':
    # Go to https://pyformat.info/ for a summary of how to use format()
    print("{} is a random number.\nThis string will go to the standard output "
          "stream".format(np.random.randint(0, 100)))
    logger.info("This is an info level statement.\nLogging statements go to "
                "standard error.")
    logger.debug("This is a debugging level statement.\nIt appears when the "
                 "logging level is det to DEBUG or higher.")
    if __debug__:
        print("This statement appears if Python was unoptimized.\n"
              "i.e. if it was started without the -O option.")
    logger.info("Good bye!")

Run the script in a bash terminal:

$ python -O dev_logging.py > some_data.txt
INFO:__main__:This is an info level statement.
INFO:__main__:Good bye!

some_data.txt:

46 is a random number.
This string will go to the standard outputstream
@carterbox carterbox added the enhancement New feature or request label Oct 26, 2018
@carterbox
Copy link
Member Author

@dgursoy, This is what we talked about today. Any more thoughts?

@carterbox carterbox changed the title Verbose logging statements Using logging statements for development Oct 26, 2018
@dgursoy
Copy link
Contributor

dgursoy commented Oct 26, 2018

Looks good. One concern is we need a level of control for the print statements though. Otherwise, it won't be structured and we can't use them easily afterwards. For example, if I print a "Hello World" somewhere, I may not want to have it in the txt file.

@carterbox
Copy link
Member Author

I guess you could use the __debug__ switch to save files instead. For example:

if __debug__:
    np.save('./debug_data/', some_data)

But also, if the user doesn't use the -O switch, it's better to dump a bunch of text to stdout instead of making files on their disk?

I think __debug__ can only be set before runtime, so we can't protect the user by setting it for them in the code.

@carterbox
Copy link
Member Author

Today I learned that the logger in a python library should be set to use the NullHandler. This is how it is set up for TomoPy and it allows the library to inherit whatever logging environment the end user has set up.

https://docs.python.org/3/howto/logging.html#library-config

@carterbox carterbox pinned this issue May 5, 2020
@YudongYao YudongYao unpinned this issue May 6, 2020
@carterbox carterbox pinned this issue Jun 19, 2020
YudongYao added a commit that referenced this issue Sep 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants