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 standard logger in log function #158

Merged
merged 13 commits into from
Nov 26, 2024
Merged

Conversation

dormrod
Copy link
Contributor

@dormrod dormrod commented Oct 16, 2024

Description

We are currently considering how to improve the PLAMS logging for a better user experience.

This includes having logging of job summaries etc. as in #156

As a first step though, a refactor of the existing log method in PLAMS. This currently had it's own logging implementation to write to stdout and a file, including handling the thread safety etc.

This change maintains the current behaviour exactly, but instead uses the standard python logger and formatter etc. This is more a best-practice change at the moment, but also provides a pattern for adding other logger types.

Implementation

A wrapper class Logger has been added to scm.plams.core.logging. It is a thin wrapper over the standard library logger, which has an interface optimised for the current usage in PLAMS. It has a configure method and a log method which are both used by the general log function.

Note that the configure methods are called very frequently (every time log is called), but they only update the handlers if there is an actual change to the config. This is required as the current behaviour is to log to the global config default job manger, which can be changed dynamically in the script. So this can't be a one-time-setup type of configuration.

Instead of instantiating the Logger directly, a LogManager static class has also been added. This allows the loggers to be accessed by name and makes sure they are singletons. This is exposed to the user via a get_logger helper method.

Example

An example usage is as follows:

Screenshot 2024-10-18 at 10 02 46

Testing

Logging tests were added for log function, which passed before and after this change.

Specific tests have also been added for the Logger class.

In addition, the scripting tests for PLAMS were run, which should flag any issues as the diffs would be incorrect

Future Extensions

It would now be possible to add a get_csv_logger method which could return a csv logger based on a similar pattern to the current logger. There could also be an (abstract) base class for the different logger types.

@dormrod dormrod force-pushed the DavidOrmrodMorley/default_logger branch 5 times, most recently from 8cc9f58 to 974f4aa Compare October 18, 2024 08:05
core/logging.py Outdated
self._logger.addHandler(self._stdout_handler)

# Update the stdout handler level if required
if stdout_level != self._stdout_handler.level:
Copy link

@GiulioIlBen GiulioIlBen Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would sugget this correction:

if stdout_level != 28 - self._stdout_handler.level:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good spot - updated

@dormrod dormrod force-pushed the DavidOrmrodMorley/default_logger branch from 974f4aa to 6073be1 Compare November 25, 2024 16:13
@dormrod dormrod force-pushed the DavidOrmrodMorley/default_logger branch 2 times, most recently from bb30972 to 195f611 Compare November 26, 2024 10:28
@dormrod dormrod force-pushed the DavidOrmrodMorley/default_logger branch 2 times, most recently from 3f63ebe to 831c851 Compare November 26, 2024 14:20
@dormrod dormrod force-pushed the DavidOrmrodMorley/default_logger branch from 831c851 to 919ec0d Compare November 26, 2024 14:37
Copy link

@GiulioIlBen GiulioIlBen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me.

@dormrod dormrod merged commit a0c4341 into trunk Nov 26, 2024
17 checks passed
@dormrod dormrod deleted the DavidOrmrodMorley/default_logger branch November 26, 2024 16:00
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

Successfully merging this pull request may close these issues.

2 participants