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

Use Python's logging instead of print function? #15

Open
astropenguin opened this issue Feb 23, 2022 · 1 comment
Open

Use Python's logging instead of print function? #15

astropenguin opened this issue Feb 23, 2022 · 1 comment

Comments

@astropenguin
Copy link
Contributor

I would like to propose to use the Python's logging feature instead of the print function for logging. This would be nice for users to control levels and destination of the PRIISM log messages. The following changes are what we need to implement the feature.

  1. Add a module logger to each PRIISM submodule:
     import logging
    
     logger = logging.getLogger(__name__)
  2. Replace each print function with a logger method of an appropriate level:
    + logger.info("information")
    - print("information")
    
    + logger.debug("for debug")
    - print("for debug")

Then users can customize logging by adding basicConfig at the top of a Python file. For example:

import logging

logging.basicConfig(
    filename="priism.log",
    format="%(asctime)s %(message)s",
    level="INFO",
)

It would also be nice to use the CASA logger. For example, the following code can create a custom log handler that posts a log message to the CASA logger:

Click to show the code
import logging
from casatasks import casalog


CASALOG_LEVELS = {
    logging.DEBUG: "DEBUGGING",
    logging.INFO: "INFO",
    logging.WARNING: "WARN",
    logging.ERROR: "SEVERE",
    logging.CRITICAL: "SEVERE",
}


class CasalogHandler(logging.Handler):
    def __init__(self, level: int = logging.DEBUG) -> None:
        super().__init__(level)

    def emit(self, record: logging.LogRecord) -> None:
        casalog.post(
            message=self.format(record),
            priority=CASALOG_LEVELS[record.levelno],
        )

Then users can alter the destination of log messages. For example:

logging.basicConfig(
    handlers=(CasalogHandler()),
    format="%(asctime)s %(message)s",
    level="INFO",
)

Because the updates change many lines of the codes, I'm not going to argue that it should be implemented right away... it is just a personal proposal.

@tnakazato
Copy link
Owner

Dear @astropenguin,

Thank you very much for your suggestion. I agree that this is useful feature for users. Although users can take full control over the log messages from Python layer by this change, it might not affect the messages from core library (sparseimaging) written in C++. If that is acceptable, I will work on the proposed improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants