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

feat: Add --verbose flag #691

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 29 additions & 10 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from robyn.reloader import setup_reloader
from robyn.env_populator import load_vars
from robyn.events import Events
from robyn.logger import logger

# from robyn.logger import logger
from robyn.processpool import run_processes
from robyn.responses import serve_file, serve_html
from robyn.robyn import (
Expand All @@ -32,6 +33,21 @@

__version__ = get_version()

INFO_APP = 55 # Level number, higher than CRITICAL (50)
logging.addLevelName(INFO_APP, "INFO_APP") # Add level name


def info_app(self, message, *args, **kwargs):
self._log(INFO_APP, message, args, kwargs)


logging.Logger.info_app = info_app # Add method to Logger class

logging.INFO_APP = INFO_APP # Optional - for convenience

logger = logging.getLogger(__name__)
Comment on lines +36 to +48
Copy link
Member

Choose a reason for hiding this comment

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

Hey @carlosm27 👋

Thank you for the PR 😄 Can you please explain what the INFO_APP parameter does?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @sansyrox it creates a new log level because with the info level shows also the logs from Actix too. But, I just notice in the Logger documentation that it doesn't advice creating new logs levels because it can create conflicts with other libraries. So, I will continue working on this and making updates to receive their review.

Copy link
Contributor

@shivaylamba shivaylamba Feb 9, 2024

Choose a reason for hiding this comment

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

Hey @carlosm27 any further updates to this? Let us know if you are stuck on the issue

logger.setLevel(INFO_APP)


class Robyn:
"""This is the python wrapper for the Robyn binaries."""
Expand All @@ -43,13 +59,14 @@ def __init__(self, file_object: str, config: Config = Config()) -> None:
self.config = config

load_vars(project_root=directory_path)
logging.basicConfig(level=self.config.log_level)

if self.config.log_level.lower() != "warn":
logger.info(
"SERVER IS RUNNING IN VERBOSE/DEBUG MODE. Set --log-level to WARN to run in production mode.",
color=Colors.BLUE,
)
if self.config.verbose is True:
logging.basicConfig(level=self.config.log_level)

logger.info(
"SERVER IS RUNNING IN VERBOSE/DEBUG MODE. Set --log-level to WARN to run in production mode.",
color=Colors.BLUE,
)
# If we are in dev mode, we need to setup the reloader
# This process will be used by the watchdog observer while running the actual server as children processes
if self.config.dev and not os.environ.get("IS_RELOADER_RUNNING", False):
Expand Down Expand Up @@ -150,7 +167,9 @@ def add_web_socket(self, endpoint: str, ws: WebSocket) -> None:
self.web_socket_router.add_route(endpoint, ws)

def _add_event_handler(self, event_type: Events, handler: Callable) -> None:
logger.info("Add event %s handler", event_type)
if self.config.verbose is True:
logger.info("Add event %s handler", event_type)

if event_type not in {Events.STARTUP, Events.SHUTDOWN}:
return

Expand All @@ -174,8 +193,8 @@ def start(self, host: str = "127.0.0.1", port: int = 8080):
port = int(os.getenv("ROBYN_PORT", port))
open_browser = bool(os.getenv("ROBYN_BROWSER_OPEN", self.config.open_browser))

logger.info("Robyn version: %s", __version__)
logger.info("Starting server at %s:%s", host, port)
logger.info_app("Robyn version: %s", __version__)
logger.info_app("Starting server at %s:%s", host, port)

mp.allow_connection_pickling()

Expand Down
7 changes: 7 additions & 0 deletions robyn/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def __init__(self) -> None:
default=False,
help="Show the Robyn version.",
)
parser.add_argument(
"--verbose",
action="store_true",
default=False,
help="Add logs",
)

args, _ = parser.parse_known_args()

Expand All @@ -65,6 +71,7 @@ def __init__(self) -> None:
self.docs = args.docs
self.open_browser = args.open_browser
self.version = args.version
self.verbose = args.verbose

if self.dev and (self.processes != 1 or self.workers != 1):
raise Exception("--processes and --workers shouldn't be used with --dev")
Expand Down
Loading