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

Commit

Permalink
Merge 681910c into d257403
Browse files Browse the repository at this point in the history
  • Loading branch information
macartur committed May 2, 2017
2 parents d257403 + 681910c commit b92a5ad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
45 changes: 20 additions & 25 deletions etc/kytos/logging.ini
@@ -1,38 +1,33 @@
[formatters]
keys: precise,brief
keys: console,syslog

[handlers]
keys: console,syslog

[loggers]
keys: root,daemon,cli

[formatter_brief]
format: %(name)s:%(levelname)s: %(message)s

[formatter_precise]
keys: root,api_server

[formatter_syslog]
format: %(name)s:%(levelname)s %(module)s:%(lineno)d: %(message)s


[formatter_console]
format: %(asctime)s - %(levelname)s [%(name)s] (%(threadName)s) %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: brief
args:[sys.stdout]
formatter: console

[handler_syslog]
class: handlers.SysLogHandler
args: [('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER]
formatter: precise
args: ['/dev/log']
formatter: syslog

[logger_root]
level: INFO
handlers: syslog
handlers: syslog,console

[logger_daemon]
level: INFO
qualname: kytos.daemon
handlers: syslog

[logger_cli]
[logger_api_server]
level: INFO
qualname: kytos.cli
handlers: console
qualname: werkzeug
handlers:
4 changes: 1 addition & 3 deletions kytos/core/controller.py
Expand Up @@ -110,9 +110,7 @@ def register_websockets(self):

def enable_logs(self):
"""Method used to register kytos log and enable the logs."""
if self.options.debug:
LogManager.add_syslog()

LogManager.load_logging_file(self.options.logging)
self.log = logging.getLogger(__name__)

def start(self):
Expand Down
46 changes: 30 additions & 16 deletions kytos/core/logs.py
@@ -1,24 +1,27 @@
"""Handle logs displayed by Kytos SDN Platform."""
import inspect
import logging
import logging.handlers
import re
from configparser import RawConfigParser
from logging import Formatter, StreamHandler, config, getLogger

__all__ = ('LogManager', 'NAppLog')

FORMAT = '%(asctime)s - %(levelname)s [%(name)s] (%(threadName)s) %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)


class LogManager:
"""Manage handlers for all loggers."""

configuration = None

@classmethod
def add_syslog(cls):
"""Output all logs to syslog."""
handler = logging.handlers.SysLogHandler(address='/dev/log')
cls._add_handler(handler)
return handler
def load_logging_file(cls, logging_file):
"""Loadding logs configuration from a file.
Args:
logging_file(str): Address of logging configuration file.
"""
cls.configuration = RawConfigParser()
cls.configuration.read(logging_file)
config.fileConfig(logging_file)

@classmethod
def add_stream_handler(cls, stream):
Expand All @@ -27,14 +30,25 @@ def add_stream_handler(cls, stream):
Args:
stream: Object that supports ``write()`` and ``flush()``.
"""
handler = logging.StreamHandler(stream)
handler = StreamHandler(stream)
cls._add_handler(handler)
return handler

@staticmethod
def _add_handler(handler):
handler.setFormatter(logging.Formatter(FORMAT))
logging.getLogger().addHandler(handler)
@classmethod
def _add_handler(cls, handler):
"""Method used to add a new handler to loggers.
Args:
handler(Handler): Handle to be added.
"""
options = {}
if cls.configuration:
options = dict(cls.configuration.items('formatter_console'))

fmt = Formatter(options.get('format', None),
options.get('datefmt', None))
handler.setFormatter(fmt)
getLogger().addHandler(handler)


class NAppLog:
Expand All @@ -56,7 +70,7 @@ class NAppLog:
def __getattribute__(self, name):
"""Detect NApp ID and use its logger."""
napp_id = detect_napp_id()
logger = logging.getLogger(napp_id)
logger = getLogger(napp_id)
return logger.__getattribute__(name)


Expand Down

0 comments on commit b92a5ad

Please sign in to comment.