Skip to content

Commit

Permalink
fix: Log ipython commands (backport #25364) (#25368)
Browse files Browse the repository at this point in the history
* fix: Log ipython commands

(cherry picked from commit 8bdc88f)

# Conflicts:
#	frappe/commands/utils.py

* fix: print frappe.log message

(cherry picked from commit 4ef0740)

# Conflicts:
#	frappe/__init__.py

---------

Co-authored-by: Ankush Menat <ankush@frappe.io>
  • Loading branch information
mergify[bot] and ankush committed Mar 12, 2024
1 parent 69c0d08 commit a2ebb4e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 1 addition & 2 deletions frappe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ def log(msg: str) -> None:
:param msg: Message."""
if not request:
if conf.get("logging") or False:
print(repr(msg))
print(repr(msg))

debug_log.append(as_unicode(msg))

Expand Down
17 changes: 17 additions & 0 deletions frappe/commands/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import subprocess
import sys
import typing
from shutil import which

import click
Expand All @@ -15,6 +16,9 @@
find_executable = which # backwards compatibility
EXTRA_ARGS_CTX = {"ignore_unknown_options": True, "allow_extra_args": True}

if typing.TYPE_CHECKING:
from IPython.terminal.embed import InteractiveShellEmbed


@click.command("build")
@click.option("--app", help="Build assets for app")
Expand Down Expand Up @@ -590,6 +594,18 @@ def _console_cleanup():
frappe.destroy()


def store_logs(terminal: "InteractiveShellEmbed") -> None:
from contextlib import suppress

frappe.log_level = 20 # info
with suppress(Exception):
logger = frappe.logger("ipython")
logger.info("=== bench console session ===")
for line in terminal.history_manager.get_range():
logger.info(line[2])
logger.info("=== session end ===")


@click.command("console")
@click.option("--autoreload", is_flag=True, help="Reload changes to code automatically")
@pass_context
Expand All @@ -613,6 +629,7 @@ def console(context, autoreload=False):

all_apps = frappe.get_installed_apps()
failed_to_import = []
register(store_logs, terminal) # Note: atexit runs in reverse order of registration

for app in list(all_apps):
try:
Expand Down

0 comments on commit a2ebb4e

Please sign in to comment.