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

replace print with logger #1104

Merged
merged 8 commits into from
Feb 12, 2024
Merged

replace print with logger #1104

merged 8 commits into from
Feb 12, 2024

Conversation

kohya-ss
Copy link
Owner

@kohya-ss kohya-ss commented Feb 4, 2024

No description provided.

shirayu and others added 3 commits February 4, 2024 18:14
* Add get_my_logger()

* Use logger instead of print

* Fix log level

* Removed line-breaks for readability

* Use setup_logging()

* Add rich to requirements.txt

* Make simple

* Use logger instead of print

---------

Co-authored-by: Kohya S <52813779+kohya-ss@users.noreply.github.com>
@kohya-ss
Copy link
Owner Author

kohya-ss commented Feb 4, 2024

@shirayu Thank you again for your great contribution. I have made the following modifications:

  • Added a fallback for cases where rich is not installed.
  • Enabled specifying the log output level, destination, and format via arguments in the training scripts.

Please take a look at utils.py, as well as fine_tune.py. If you have the time, I would appreciate your review and advice on these changes.

@kohya-ss kohya-ss changed the title replace print with logger if they are logs replace print with logger Feb 4, 2024
Copy link
Contributor

@shirayu shirayu left a comment

Choose a reason for hiding this comment

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

Great!
Thank you for your updates!
Almost everything is fine, but I did make a few comments.

I am sure you already understand this, but just to be clear, I note that the call of add_logging_arguments is also needed in other files like sdxl_train.py.

library/utils.py Outdated
handler = RichHandler()
except ImportError:
print("rich is not installed, using basic logging")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that it is better to use logger.info(....) after logging.root.addHandler(handler) like this.

msg_init = None

# set message on ImportError
msg_init = "rich is not installed, using basic logging"

....

logging.root.addHandler(handler)
if msg_init is not None:
    logger = logging.getLogger(__name__)
    logger.info(msg_init)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks for the great suggestion! I will adopt this one.

fine_tune.py Outdated


def setup_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()

add_logging_arguments(parser) # モジュール指定がないのがちょっと気持ち悪い / bit weird that this does not have module prefix
Copy link
Contributor

Choose a reason for hiding this comment

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

Moving add_logging_arguments() and setup_logging() from library/utils.py to library/logger_utils/__init__.py is an option.

The call will be like this.

logger_utils.add_logging_arguments(parser)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thank you! I think this is certainly one way to do it. I just don't want to add too many files, so I guess I'll just leave it as it was.

library/utils.py Outdated
from rich.logging import RichHandler

handler = RichHandler()
Copy link
Contributor

@shirayu shirayu Feb 8, 2024

Choose a reason for hiding this comment

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

I would like to add one more thing.
By default, rich logs to standard output (stdout), but I think it is better to change it as follows.

Textualize/rich#2022

Proposal

from rich.console import Console    
from rich.logging import RichHandler
handler = RichHandler(console=Console(stderr=True))

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thank you! That certainly seems to be consistent.

If someone wants the output to be on stdout as before, there is an option for it.

@kohya-ss kohya-ss merged commit 35c6053 into dev Feb 12, 2024
2 checks passed
@kohya-ss kohya-ss deleted the dev_improve_log branch February 12, 2024 02:33
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