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

Logging to file for action server #306

Merged
merged 15 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rasa_sdk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from rasa_sdk import utils
from rasa_sdk.endpoint import create_argument_parser, run
from rasa_sdk.constants import APPLICATION_ROOT_LOGGER_NAME


def main_from_args(args):
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("matplotlib").setLevel(logging.WARN)

utils.configure_colored_logging(args.loglevel)
utils.configure_file_logging(logging.getLogger(APPLICATION_ROOT_LOGGER_NAME), args.log_file, args.loglevel)
utils.update_sanic_log_level()

run(
Expand Down
1 change: 1 addition & 0 deletions rasa_sdk/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
ENV_SANIC_WORKERS = "ACTION_SERVER_SANIC_WORKERS"
DEFAULT_LOG_LEVEL_LIBRARIES = "ERROR"
ENV_LOG_LEVEL_LIBRARIES = "LOG_LEVEL_LIBRARIES"
APPLICATION_ROOT_LOGGER_NAME = "rasa_sdk"
3 changes: 2 additions & 1 deletion rasa_sdk/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def create_argument_parser():

parser = argparse.ArgumentParser(description="starts the action endpoint")
add_endpoint_arguments(parser)
utils.add_logging_option_arguments(parser)
utils.add_logging_level_option_arguments(parser)
utils.add_logging_file_arguments(parser)
return parser


Expand Down
32 changes: 31 additions & 1 deletion rasa_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def all_subclasses(cls: Any) -> List[Any]:
]


def add_logging_option_arguments(parser):
def add_logging_level_option_arguments(parser):
"""Add options to an argument parser to configure logging levels."""

# arguments for logging configuration
Expand Down Expand Up @@ -70,6 +70,15 @@ def add_logging_option_arguments(parser):
)


def add_logging_file_arguments(parser):
parser.add_argument(
"--log-file",
type=str,
default=None,
help="Store logs in specified file.",
)


def configure_colored_logging(loglevel):
import coloredlogs

Expand All @@ -86,6 +95,27 @@ def configure_colored_logging(loglevel):
)


def configure_file_logging(logger_obj: logging.Logger, log_file: Optional[Text], loglevel: int) -> None:
"""Configure logging to a file.

Args:
:param logger_obj: Logger object to configure.
:param log_file: Path of log file to write to.
:param loglevel:
"""
if not log_file:
return

if not loglevel:
loglevel = logging.INFO

formatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s] %(name)s - %(message)s")
file_handler = logging.FileHandler(log_file, encoding="utf-8")
file_handler.setLevel(loglevel)
file_handler.setFormatter(formatter)
logger_obj.addHandler(file_handler)


def arguments_of(func) -> AbstractSet[Text]:
"""Return the parameters of the function `func` as a list of their names."""

Expand Down