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

My previous logging to file function will be affected by add pyxcp application .The previous log.info function reported errors #176

Open
wangwei357 opened this issue Oct 12, 2024 · 11 comments

Comments

@wangwei357
Copy link

wangwei357 commented Oct 12, 2024

My previous logging to file function will be affected by add pyxcp application . The previous log.info function reported errors
only log to file is affected , the steram out is OK

File "C:\Users\XXXXXX\AppData\Local\anaconda3\Lib\logging_init_.py", line 1113, in emit
stream.write(msg + self.terminator)
ValueError: I/O operation on closed file.

in my other coder logger config , I have code like this :

    handler = logging.FileHandler(filepath, mode='w')
    formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s')
    handler.setFormatter(formatter)
    self.log.addHandler(handler)
    console = logging.StreamHandler()
    console.setFormatter(formatter)
    if console_debug:
        console.setLevel(logging.DEBUG)
    else:
        console.setLevel(logging.INFO)
    self.log.addHandler(console)
@wangwei357 wangwei357 changed the title My previous logging to file function will be affected and cannot be used. The previous log.info function reported errors My previous logging to file function will be affected by add pyxcp application .The previous log.info function reported errors Oct 12, 2024
@wangwei357
Copy link
Author

wangwei357 commented Oct 12, 2024

It‘s seems that : myself logger file handler was force closed when setup the pyxcy application. and the affected from class PyXCP(Application) which inherit from traitlet lib. But I can't locate the cause code in traitlet .

@christoph2
Copy link
Owner

Yes, pyXCP uses traitlets under the hood, which is used by projects like IPython and Jupyter, so the likehood that there is a hidden bug is really small. Maybe there's a similar logging issue on Stackoverflow?
For now you may just swallow the exception:

try:
   # Offending code goes here.
except ValueError as e:
   print(f"Logging error {e!r}")

@christoph2
Copy link
Owner

The effect of closing can be easily observed...

python -c "import sys as s;s.stdout.close();print('hello')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: I/O operation on closed file.

@wangwei357
Copy link
Author

@christoph2 Can I disable pyxcp logger function? Now this feature applies to my use of other programs

@christoph2
Copy link
Owner

At the first sight, there are two places which could be commented out:

# Remove any handlers installed by `traitlets`.

self.log.addHandler(rich_handler)

If it helps, I'll add a configuration option.

@christoph2
Copy link
Owner

Also interesting would be something like

logger = logging.getLogger("my-own-logger")

as a starting point for your logging stuff.

@christoph2
Copy link
Owner

I've added a small test for a already configured root handler, if so use it.
Hope it helps.

@wangwei357
Copy link
Author

wangwei357 commented Oct 14, 2024

@christoph2 Thansk for your support ! I had try your method, It can solve the problem of error, but the function of affecting the file output to the file is no longer available. I use debug to locate the problem statement from this location.
https://github.com/christoph2/pyxcp/blob/41d6fd9cc96d4c5fd830fedd30886ad818468c28/pyxcp/config/__init__.py#L970C3-L970C62

    def read_configuration_file(self, file_name: str, emit_warning: bool = True):
        self.legacy_config: bool = False

this self.read_configuration_file(file_name,False) in _read_configuration(self, file_name: str, emit_warning: bool = True) -> None
made my other logger file handler error

@wangwei357
Copy link
Author

wangwei357 commented Oct 14, 2024

Fortunately, I solved the problem by adjusting the search. However, this logger is redirected to the root logger, I do not know if there will be other effects

    def start(self):
        if self.subapp:
            self.subapp.start()
            exit(2)
        else:
            # self._setup_logger()
            if logging.getLogger().hasHandlers():
                self.log = logging.getLogger()
            else:
                self._setup_logger()
            self._read_configuration(self.config_file)

@christoph2
Copy link
Owner

if logging.getLogger().hasHandlers():
    self.log = logging.getLogger()
else:
    self._setup_logger()
self._read_configuration(self.config_file)

vs.

self._read_configuration(self.config_file)
if logging.getLogger().hasHandlers():
    self.log = logging.getLogger()
else:
    self._setup_logger()

Really makes a difference?

The other thing that came to mind: I could add a use_existing_logger configuration option.

@wangwei357
Copy link
Author

Yea,It's make difference

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