#1161 Categorize log messages
Merged 4 months ago by vmaljulin. Opened 4 months ago by vmaljulin.
vmaljulin/fm-orchestrator FACTORY-3469  into  master

@@ -50,7 +50,7 @@ 

  from gi.repository import Modulemd  # noqa

  

  from module_build_service.logger import (

-     init_logging, ModuleBuildLogs, level_flags)

+     init_logging, ModuleBuildLogs, level_flags, MBSLogger)

  

  from module_build_service.errors import (

      ValidationError, Unauthorized, UnprocessableEntity, Conflict, NotFound,

@@ -154,7 +154,7 @@ 

  

  

  init_logging(conf)

- log = getLogger(__name__)

+ log = MBSLogger()

  build_logs = ModuleBuildLogs(

      conf.build_logs_dir,

      conf.build_logs_name_format,

@@ -44,6 +44,7 @@ 

  

  import os

  import logging

+ import inspect

  

  levels = {}

  levels["debug"] = logging.DEBUG

@@ -149,6 +150,59 @@ 

          del self.handlers[build.id]

  

  

+ class MBSLogger:

+     def __init__(self):

+         self._logger = logging.getLogger('MBS')

+         self._level = logging.NOTSET

+         self._current_path = os.path.dirname(os.path.realpath(__file__))

+ 

+     @property

+     def logger(self):

+         return self._logger

+ 

+     @property

+     def level(self):

+         return self._level

+ 

+     @level.setter

+     def level(self, level):

+         self._level = level

+         self.logger.setLevel(self._level)

+ 

+     def setLevel(self, level):

+         self.level = level

+ 

+     def debug(self, *args, **kwargs):

+         return self._log_call('debug', args, kwargs)

+ 

+     def info(self, *args, **kwargs):

+         return self._log_call('info', args, kwargs)

+ 

+     def warning(self, *args, **kwargs):

+         return self._log_call('warning', args, kwargs)

+ 

+     def error(self, *args, **kwargs):

+         return self._log_call('error', args, kwargs)

+ 

+     def critical(self, *args, **kwargs):

+         return self._log_call('critical', args, kwargs)

+ 

+     def exception(self, *args, **kwargs):

+         return self._log_call('exception', args, kwargs)

+ 

+     def log(self, *args, **kwargs):

+         return self._log_call('log', args, kwargs)

+ 

+     def _log_call(self, level_name, args, kwargs):

+         caller_filename = inspect.stack()[2][1]

+         caller_filename = os.path.normpath(caller_filename)

+         if not caller_filename.startswith(self._current_path):

+             log_name = 'MBS'

+         else:

+             log_name = 'MBS' + caller_filename[len(self._current_path):-3].replace('/', '.')

+         return getattr(logging.getLogger(log_name), level_name)(*args, **kwargs)

+ 

+ 

  def str_to_log_level(level):

      """

      Returns internal representation of logging level defined

@@ -174,9 +228,9 @@ 

  

      if not log_backend or len(log_backend) == 0 or log_backend == "console":

          logging.basicConfig(level=conf.log_level, format=log_format)

-         log = logging.getLogger()

-         log.setLevel(conf.log_level)

+         log = MBSLogger()

+         log.level = conf.log_level

      else:

          logging.basicConfig(filename=conf.log_file, level=conf.log_level,

                              format=log_format)

-         log = logging.getLogger()

+         log = MBSLogger()

file modified
+1 -1

@@ -104,7 +104,7 @@ 

              data = f.read()

              # Note that DEBUG is not present unless configured server-wide.

              for level in ["INFO", "WARNING", "ERROR"]:

-                 assert data.find("%s - ignore this test msg2" % level) != -1

+                 assert data.find("MBS - {0} - ignore this test msg2".format(level)) != -1

Hint: Just {} would be enough here.

@fivaldi Do we still have a need to be compatible with Python 2?

  

          # Try to log more messages when build_log for module 1 is stopped.

          # New messages should not appear in a log.

This fixes #1064

Signed-off-by: Valerij Maljulin vmaljuli@redhat.com

Log messages now looks like this:
2019-03-08 14:50:33,944 - MainThread - MBS.builder.KojiModuleBuilder - DEBUG - Using koji_config: ./conf/koji.conf
2019-03-08 14:50:33,951 - MainThread - MBS.utils.general - WARNING - Module build 2 does not have buildrequires in its xmd

It adds some overhead to logging. The tests without this PR runs in 44 seconds for me, with this PR it's 56 seconds. I still think it is worth it and should not be an issue, because MBS is not real time application :).

So, +1. Will wait for one more :).

pretty please pagure-ci rebuild

4 months ago

rebased onto 46736ea2302501f830ac33b66aae97330a8cc468

4 months ago

Hint: Just {} would be enough here.

Seems tricky. :-) I wonder if it would break on some circumstances, but with the path normalization above, it should probably work anytime/anywhere. :-)

It'd be nice having this as a property...

@vmaljulin I left couple of comments. Furthermore, I think init_logging should initialize the MBSLogger with the corresponding log level (set via config variable in the config file).

What do you think?

@fivaldi Do we still have a need to be compatible with Python 2?

@fivaldi Do we still have a need to be compatible with Python 2?

My preference is close to "NO" :), but what's the actual project's preference, I dunno.

rebased onto f83ad998d2a6d1f5ebef160c5144a8e3479a7fa4

4 months ago

rebased onto e9d2928d2c2f193c3b56ba044543d2e8ad06054d

4 months ago

Yes, we are still running MBS using python2 at least internally, so we need to be compatible with python2 right now.

The changes look good to me. Not sure about Mr. Jenkins...?

rebased onto b39f92b

4 months ago

The changes look good to me. Not sure about Mr. Jenkins...?

Rebased. It should work now.

Pull-Request has been merged by vmaljulin

4 months ago