コード例 #1
0
ファイル: log.py プロジェクト: sseshachala/shinken
    def _log(self, level, message, format=None, print_it=True, display_level=True):
        """We enter a log message, we format it, and we add the log brok"""
        global obj
        global name
        global local_log
        global human_timestamp_log

        # ignore messages when message level is lower than Log level
        if level < self._level:
            return

        # We format the log in UTF-8
        if isinstance(message, str):
            message = message.decode('UTF-8', 'replace')

        if format is None:
            lvlname = logging.getLevelName(level)

            if display_level:
                fmt = u'[%(date)s] %(level)-9s %(name)s%(msg)s\n'
            else:
                fmt = u'[%(date)s] %(name)s%(msg)s\n'

            args = {
                'date': (human_timestamp_log and time.asctime()
                         or int(time.time())),
                'level': lvlname.capitalize()+' :',
                'name': name and ('[%s] ' % name) or '',
                'msg': message
            }
            s = fmt % args
        else:
            s = format % message

        if print_it and len(s) > 1:            
            # Take a color so we can print if it's a TTY
            if is_tty():
                color = {Log.WARNING:'yellow', Log.CRITICAL:'magenta', Log.ERROR:'red'}.get(level, None)
            else:
                color = None
            
            # Print to standard output.
            # If the daemon is launched with a non UTF8 shell
            # we can have problems in printing, work around it.
            try:
                cprint(s[:-1], color)
            except UnicodeEncodeError:
                print s.encode('ascii', 'ignore')


        # We create the brok and load the log message
        # DEBUG level logs are logged by the daemon locally
        # and must not be forwarded to other satellites, or risk overloading them.
        if level != logging.DEBUG:
            b = Brok('log', {'log': s})
            obj.add(b)

        # If local logging is enabled, log to the defined handler, file.
        if local_log is not None:
            logging.log(level, s.strip())
コード例 #2
0
ファイル: log.py プロジェクト: cedef/shinken
 def emit(self, record):
     try:
         msg = self.format(record)
         colors = {'DEBUG': 'cyan', 'INFO': 'magenta', 'WARNING': 'yellow', 'CRITICAL': 'magenta', 'ERROR': 'red'}
         cprint(msg, colors[record.levelname])
     except UnicodeEncodeError:
         print msg.encode('ascii', 'ignore')
     except:
         self.handleError(record)
コード例 #3
0
 def emit(self, record):
     try:
         msg = self.format(record)
         colors = {'DEBUG': 'cyan', 'INFO': 'magenta',
                   'WARNING': 'yellow', 'CRITICAL': 'magenta', 'ERROR': 'red'}
         cprint(msg, colors[record.levelname])
     except UnicodeEncodeError:
         print msg.encode('ascii', 'ignore')
     except Exception:
         self.handleError(record)