Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Modify loggers to begin the hierarchy with "kytos"
Browse files Browse the repository at this point in the history
  • Loading branch information
rmotitsuki authored and beraldoleal committed Oct 8, 2019
1 parent 4e395cc commit 9d4197b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
6 changes: 5 additions & 1 deletion etc/kytos/logging.ini.template
Expand Up @@ -5,7 +5,7 @@ keys: console,syslog
keys: console,syslog

[loggers]
keys: root,api_server,socket
keys: root,kytos,api_server,socket

[formatter_syslog]
format: %(name)s:%(levelname)s %(module)s:%(lineno)d: %(message)s
Expand All @@ -27,6 +27,10 @@ formatter: syslog
level: INFO
handlers: syslog,console

[logger_kytos]
level: INFO
handlers: syslog,console

[logger_api_server]
level: WARNING
qualname: werkzeug
Expand Down
2 changes: 1 addition & 1 deletion kytos/core/api_server.py
Expand Up @@ -43,7 +43,7 @@ def __init__(self, app_name, listen='0.0.0.0', port=8181,
self.napps_dir = napps_dir

self.flask_dir = os.path.join(dirname, '../web-ui')
self.log = logging.getLogger('api_server')
self.log = logging.getLogger(__name__)

self.listen = listen
self.port = port
Expand Down
2 changes: 1 addition & 1 deletion kytos/core/atcp_server.py
Expand Up @@ -7,7 +7,7 @@
from kytos.core.connection import Connection
from kytos.core.events import KytosEvent

LOG = logging.getLogger("atcp_server")
LOG = logging.getLogger(__name__)


def exception_handler(loop, context):
Expand Down
4 changes: 2 additions & 2 deletions kytos/core/controller.py
Expand Up @@ -130,7 +130,7 @@ def enable_logs(self):
"""Register kytos log and enable the logs."""
LogManager.load_config_file(self.options.logging, self.options.debug)
LogManager.enable_websocket(self.api_server.server)
self.log = logging.getLogger("controller")
self.log = logging.getLogger(__name__)

def start(self, restart=False):
"""Create pidfile and call start_controller method."""
Expand Down Expand Up @@ -213,7 +213,7 @@ def _stop_loop(_):
loop.stop()

async def _run_api_server_thread(executor):
log = logging.getLogger('controller.api_server_thread')
log = logging.getLogger('kytos.core.controller.api_server_thread')
log.debug('starting')
# log.debug('creating tasks')
loop = asyncio.get_event_loop()
Expand Down
8 changes: 7 additions & 1 deletion kytos/core/logs.py
Expand Up @@ -45,6 +45,7 @@ def load_config_file(cls, config_file, debug='False'):
def _set_debug_mode(cls, debug=False):
if debug is True:
cls._PARSER.set('logger_root', 'level', 'DEBUG')
cls._PARSER.set('logger_kytos', 'level', 'DEBUG')
cls._PARSER.set('logger_api_server', 'level', 'DEBUG')
LOG.info('Setting log configuration with debug mode.')

Expand Down Expand Up @@ -141,7 +142,12 @@ class NAppLog:
def __getattribute__(self, name):
"""Detect NApp ID and use its logger."""
napp_id = _detect_napp_id()
logger = getLogger(napp_id)
logger = getLogger(f"kytos.napps")

# if napp_id is detected, get the napp logger.
if napp_id:
logger = logger.getChild(napp_id)

return logger.__getattribute__(name)


Expand Down
8 changes: 6 additions & 2 deletions tests/test_core/test_atcp_server.py
Expand Up @@ -56,6 +56,10 @@ def test_exception_handler_oserror(self, caplog):
exception_handler(self.loop, context2)

assert caplog.record_tuples == [
("atcp_server", logging.INFO, "Socket timeout: 'unit_tests'"),
("atcp_server", logging.INFO, "Socket closed: 'unit_tests'"),
("kytos.core.atcp_server",
logging.INFO,
"Socket timeout: 'unit_tests'"),
("kytos.core.atcp_server",
logging.INFO,
"Socket closed: 'unit_tests'"),
]
4 changes: 2 additions & 2 deletions tests/test_core/test_logs.py
Expand Up @@ -223,13 +223,13 @@ def _set_filename(self, filename):
def test_napp_id_detection(self):
"""Test NApp ID detection based on filename."""
self._set_filename('/napps/username/name/main.py')
expected_logger_name = 'username/name'
expected_logger_name = 'kytos.napps.username/name'
napp_logger = NAppLog()
self.assertEqual(expected_logger_name, napp_logger.name)

def test_napp_id_not_found(self):
"""If NApp ID is not found, should use root logger."""
self._set_filename('not/an/expected/NApp/path.py')
root_logger = logging.getLogger()
root_logger = logging.getLogger("kytos.napps")
napp_logger = NAppLog()
self.assertEqual(root_logger.name, napp_logger.name)

0 comments on commit 9d4197b

Please sign in to comment.