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
Prev Previous commit
Next Next commit
update log initialization without rich
  • Loading branch information
kohya-ss committed Feb 8, 2024
commit 9b8ea12d34ecb2db7d4bbdad52569d1455df042b
9 changes: 8 additions & 1 deletion library/utils.py
Original file line number Diff line number Diff line change
@@ -34,12 +34,14 @@ def setup_logging(args=None, log_level=None, reset=False):
else:
return

# log_level can be set by the caller or by the args, the caller has priority. If not set, use INFO
if log_level is None and args is not None:
log_level = args.console_log_level
if log_level is None:
log_level = "INFO"
log_level = getattr(logging, log_level)

msg_init = None
if args is not None and args.console_log_file:
handler = logging.FileHandler(args.console_log_file, mode="w")
else:
@@ -50,7 +52,8 @@ def setup_logging(args=None, log_level=None, reset=False):

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.

except ImportError:
print("rich is not installed, using basic logging")
# print("rich is not installed, using basic logging")
msg_init = "rich is not installed, using basic logging"

if handler is None:
handler = logging.StreamHandler(sys.stdout) # same as print
@@ -63,3 +66,7 @@ def setup_logging(args=None, log_level=None, reset=False):
handler.setFormatter(formatter)
logging.root.setLevel(log_level)
logging.root.addHandler(handler)

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