Skip to content

Commit

Permalink
Make logging optional (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeron authored Oct 17, 2023
1 parent 704c998 commit f54d807
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ Options:
--backlog INTEGER RANGE Maximum number of connections to hold in
backlog. [env var: GRANIAN_BACKLOG;
default: 1024; x>=128]
--log / --no-log Enable logging [env var:
GRANIAN_LOG_ENABLED; default: (enabled)]
--log-level [critical|error|warning|warn|info|debug]
Log level [env var: GRANIAN_LOG_LEVEL;
default: info]
Expand Down
2 changes: 2 additions & 0 deletions granian/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def main(
loop: Loops = typer.Option(Loops.auto.value, help='Event loop implementation'),
loop_opt: bool = typer.Option(False, '--opt/--no-opt', help='Enable loop optimizations', show_default='disabled'),
backlog: int = typer.Option(1024, min=128, help='Maximum number of connections to hold in backlog.'),
log_enabled: bool = typer.Option(True, '--log/--no-log', help='Enable logging', show_default='enabled'),
log_level: LogLevels = typer.Option(LogLevels.info.value, help='Log level', case_sensitive=False),
log_config: Optional[Path] = typer.Option(
None, help='Logging configuration file (json)', exists=True, file_okay=True, dir_okay=False, readable=True
Expand Down Expand Up @@ -79,6 +80,7 @@ def main(
http=http,
websockets=websockets,
backlog=backlog,
log_enabled=log_enabled,
log_level=log_level,
log_dictconfig=log_dictconfig,
ssl_cert=ssl_certificate,
Expand Down
14 changes: 10 additions & 4 deletions granian/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class LogLevels(str, Enum):
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'root': {'level': 'INFO', 'handlers': ['console']},
'formatters': {
'generic': {
'()': 'logging.Formatter',
Expand All @@ -35,14 +34,21 @@ class LogLevels(str, Enum):
}
},
'handlers': {'console': {'formatter': 'generic', 'class': 'logging.StreamHandler', 'stream': 'ext://sys.stdout'}},
'loggers': {'_granian': {'handlers': ['console'], 'level': 'INFO', 'propagate': False}},
}

logger = logging.getLogger()
# NOTE: to be consistent with the Rust module logger name
logger = logging.getLogger('_granian')


def configure_logging(level: LogLevels, config: Optional[Dict[str, Any]] = None):
def configure_logging(level: LogLevels, config: Optional[Dict[str, Any]] = None, enabled: bool = True):
log_config = copy.deepcopy(LOGGING_CONFIG)

if config:
log_config.update(config)
log_config['root']['level'] = log_levels_map[level]

log_config['loggers']['_granian']['level'] = log_levels_map[level]
logging.config.dictConfig(log_config)

if not enabled:
logger.setLevel(logging.CRITICAL + 1)
19 changes: 15 additions & 4 deletions granian/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
websockets: bool = True,
backlog: int = 1024,
http1_buffer_size: int = 65535,
log_enabled: bool = True,
log_level: LogLevels = LogLevels.info,
log_dictconfig: Optional[Dict[str, Any]] = None,
ssl_cert: Optional[Path] = None,
Expand All @@ -64,11 +65,14 @@ def __init__(
self.websockets = websockets
self.backlog = max(128, backlog)
self.http1_buffer_size = http1_buffer_size
self.log_enabled = log_enabled
self.log_level = log_level
self.log_config = log_dictconfig
self.url_path_prefix = url_path_prefix
self.reload_on_changes = reload
configure_logging(self.log_level, self.log_config)

configure_logging(self.log_level, self.log_config, self.log_enabled)

self.build_ssl_context(ssl_cert, ssl_key)
self._shd = None
self._sfd = None
Expand Down Expand Up @@ -101,14 +105,16 @@ def _spawn_asgi_worker(
http1_buffer_size,
websockets,
loop_opt,
log_enabled,
log_level,
log_config,
ssl_ctx,
scope_opts,
):
from granian._loops import loops, set_loop_signals

configure_logging(log_level, log_config)
configure_logging(log_level, log_config, log_enabled)

loop = loops.get(loop_impl)
sfd = socket.fileno()
callback = callback_loader()
Expand Down Expand Up @@ -144,14 +150,16 @@ def _spawn_rsgi_worker(
http1_buffer_size,
websockets,
loop_opt,
log_enabled,
log_level,
log_config,
ssl_ctx,
scope_opts,
):
from granian._loops import loops, set_loop_signals

configure_logging(log_level, log_config)
configure_logging(log_level, log_config, log_enabled)

loop = loops.get(loop_impl)
sfd = socket.fileno()
target = callback_loader()
Expand Down Expand Up @@ -187,14 +195,16 @@ def _spawn_wsgi_worker(
http1_buffer_size,
websockets,
loop_opt,
log_enabled,
log_level,
log_config,
ssl_ctx,
scope_opts,
):
from granian._loops import loops, set_loop_signals

configure_logging(log_level, log_config)
configure_logging(log_level, log_config, log_enabled)

loop = loops.get(loop_impl)
sfd = socket.fileno()
callback = callback_loader()
Expand Down Expand Up @@ -228,6 +238,7 @@ def _spawn_proc(self, id, target, callback_loader, socket_loader) -> multiproces
self.http1_buffer_size,
self.websockets,
self.loop_opt,
self.log_enabled,
self.log_level,
self.log_config,
self.ssl_ctx,
Expand Down

0 comments on commit f54d807

Please sign in to comment.