Skip to content

Commit

Permalink
Add log_dictconfig param to Server (#52) (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro authored Mar 14, 2023
1 parent 69add73 commit 6b3d076
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 7 additions & 4 deletions granian/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging.config

from enum import Enum
from typing import Any, Dict, Optional


class LogLevels(str, Enum):
Expand Down Expand Up @@ -46,7 +47,9 @@ class LogLevels(str, Enum):
logger = logging.getLogger()


def configure_logging(level: LogLevels):
config = copy.deepcopy(LOGGING_CONFIG)
config["root"]["level"] = log_levels_map[level]
logging.config.dictConfig(config)
def configure_logging(level: LogLevels, config: Optional[Dict[str, Any]] = None):
log_config = copy.deepcopy(LOGGING_CONFIG)
if config:
log_config.update(config)
log_config["root"]["level"] = log_levels_map[level]
logging.config.dictConfig(log_config)
16 changes: 11 additions & 5 deletions granian/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from functools import partial
from pathlib import Path
from typing import List, Optional
from typing import Any, Dict, List, Optional

import watchfiles

Expand Down Expand Up @@ -42,6 +42,7 @@ def __init__(
backlog: int = 1024,
http1_buffer_size: int = 65535,
log_level: LogLevels = LogLevels.info,
log_dictconfig: Optional[Dict[str, Any]] = None,
ssl_cert: Optional[Path] = None,
ssl_key: Optional[Path] = None,
url_path_prefix: Optional[str] = None,
Expand All @@ -61,9 +62,10 @@ def __init__(
self.backlog = max(128, backlog)
self.http1_buffer_size = http1_buffer_size
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)
configure_logging(self.log_level, self.log_config)
self.build_ssl_context(ssl_cert, ssl_key)
self._shd = None
self._sfd = None
Expand Down Expand Up @@ -100,12 +102,13 @@ def _spawn_asgi_worker(
http1_buffer_size,
websockets,
log_level,
log_config,
ssl_ctx,
scope_opts
):
from granian._loops import loops, set_loop_signals

configure_logging(log_level)
configure_logging(log_level, log_config)
loop = loops.get(loop_impl)
sfd = socket.fileno()
callback = callback_loader()
Expand Down Expand Up @@ -153,12 +156,13 @@ def _spawn_rsgi_worker(
http1_buffer_size,
websockets,
log_level,
log_config,
ssl_ctx,
scope_opts
):
from granian._loops import loops, set_loop_signals

configure_logging(log_level)
configure_logging(log_level, log_config)
loop = loops.get(loop_impl)
sfd = socket.fileno()
target = callback_loader()
Expand Down Expand Up @@ -208,12 +212,13 @@ def _spawn_wsgi_worker(
http1_buffer_size,
websockets,
log_level,
log_config,
ssl_ctx,
scope_opts
):
from granian._loops import loops, set_loop_signals

configure_logging(log_level)
configure_logging(log_level, log_config)
loop = loops.get(loop_impl)
sfd = socket.fileno()
callback = callback_loader()
Expand Down Expand Up @@ -273,6 +278,7 @@ def _spawn_proc(
self.http1_buffer_size,
self.websockets,
self.log_level,
self.log_config,
self.ssl_ctx,
{
"url_path_prefix": self.url_path_prefix
Expand Down

0 comments on commit 6b3d076

Please sign in to comment.