Пример #1
0
Файл: log.py Проект: zasfe/pol
 def print_log(self, event):
     if 'isError' in event and event['isError']:
         sys.stdout.write(bcolors.FAIL + formatEventAsClassicLogText(event) + bcolors.ENDC)
         sys.stderr.write(formatEventAsClassicLogText(event))
         sys.stderr.flush()
     else:
         sys.stdout.write(formatEventAsClassicLogText(event))
     sys.stdout.flush()
Пример #2
0
    def test__logs_messages(self):
        events = []
        namespace = factory.make_name("namespace")
        legacy_logger = LegacyLogger(namespace, self, events.append)
        message = factory.make_name("message")
        keywords = {
            factory.make_name("key"): factory.make_name("value")
            for _ in range(3)
        }

        legacy_logger.msg(message, **keywords)

        expected = {
            'log_format': Equals('{_message_0}'),
            'log_level': Equals(logger.LogLevel.info),
            'log_logger': Is(legacy_logger),
            'log_namespace': Equals(namespace),
            'log_source': Is(self),
            'log_time': IsInstance(float),
            '_message_0': Equals(message),
        }
        expected.update(
            {key: Equals(value)
             for key, value in keywords.items()})
        self.assertThat(events, HasLength(1))
        self.assertThat(events[0], MatchesDict(expected))
        self.assertThat(
            logger.formatEventAsClassicLogText(events[0], formatTimeStatic),
            Equals("<when> [%s#info] %s\n" % (namespace, message)))
Пример #3
0
    def __call__(self, event):
        """
        Write event to file.

        @param event: An event.
        @type event: L{dict}
        """
        text = formatEventAsClassicLogText(
            event, formatTime=lambda e: formatTime(e, self.timeFormat))
        if text is None:
            text = u""

        if "log_failure" in event:
            try:
                traceback = event["log_failure"].getTraceback()
            except Exception:
                traceback = u"(UNABLE TO OBTAIN TRACEBACK FROM EVENT)\n"
            text = u"\n".join((text, traceback))

        if self._encoding is not None:
            text = text.encode(self._encoding)

        if text:
            if event['log_level'] > LogLevel.warn:
                self._err.write(text)
                self._err.flush()
            else:
                self._out.write(text)
                self._out.flush()
Пример #4
0
 def oneLogMessage(self, message):
     """
     
     """
     evt = eventFromJSON(message)
     text = formatEventAsClassicLogText(evt)
     messageBytes = (text).encode("utf-8")
     os.write(STDERR, messageBytes)
     return {}
Пример #5
0
    def formatEvent(event):
        log_level = event.get('log_level')

        if log_level is not None and log_level < config_level:
            # 比配置级别低的日志直接丢弃
            return None

        return formatEventAsClassicLogText(
            event, formatTime=lambda e: formatTime(e, timeFormat))
Пример #6
0
    def test__logs_errors(self):
        events = []
        namespace = factory.make_name("namespace")
        legacy_logger = LegacyLogger(namespace, self, events.append)
        message = factory.make_name("message")
        exception_type = factory.make_exception_type()
        keywords = {
            factory.make_name("key"): factory.make_name("value")
            for _ in range(3)
        }

        try:
            raise exception_type()
        except exception_type:
            legacy_logger.err(None, message, **keywords)

        expected = {
            'log_failure':
            MatchesAll(
                IsInstance(Failure),
                AfterPreprocessing(
                    (lambda failure: failure.value),
                    IsInstance(exception_type),
                ),
            ),
            'log_format':
            Equals('{_why}'),
            'log_level':
            Equals(logger.LogLevel.critical),
            'log_logger':
            Is(legacy_logger),
            'log_namespace':
            Equals(namespace),
            'log_source':
            Is(self),
            'log_time':
            IsInstance(float),
            '_why':
            Equals(message),
        }
        expected.update(
            {key: Equals(value)
             for key, value in keywords.items()})
        self.assertThat(events, HasLength(1))
        self.assertThat(events[0], MatchesDict(expected))
        # Twisted 16.6.0 (see issue #8858) now includes a traceback,
        # so we only match on the beginning of the string.
        self.assertThat(
            logger.formatEventAsClassicLogText(events[0], formatTimeStatic),
            StartsWith("<when> [%s#critical] %s\n" % (namespace, message)))
Пример #7
0
    def makeFilteredFileLogObserver(cls, stream, withTime=True):
        """
        For a child process that has its stdout captured by the master process to be logged by the master,
        we strip out the time from the log entry since the master process will always add one. Setting
        C{withTime} to L{False} will ensure no time is generated.
        """
        assert (cls.filterPublisher is None
                and cls.filterObserver is None), "Only call this once"

        timeFormat = formatTime if withTime else lambda _: u""
        cls.filterObserver = FileLogObserver(
            stream,
            lambda event: formatEventAsClassicLogText(event,
                                                      formatTime=timeFormat))
        cls.filterPublisher = LogPublisher(cls.filterObserver)
        return cls.filterPublisher
Пример #8
0
def _doSecondaryActions(action, tzpath, xmlfile, url):

    tzdb = SecondaryTimezoneDatabase(tzpath, xmlfile, url)
    try:
        tzdb.readDatabase()
    except:
        pass
    if action == "cache":
        print("Caching from secondary server: {}".format(url, ))

        observer = FileLogObserver(
            sys.stdout, lambda event: formatEventAsClassicLogText(event))
        Logger.beginLoggingTo([observer], redirectStandardIO=False)

        reactor.callLater(0, _runInReactor, tzdb)
        reactor.run()
    else:
        usage("Invalid action: {}".format(action, ))
Пример #9
0
    def test__logs_multiple_messages(self):
        events = []
        legacy_logger = LegacyLogger(observer=events.append)
        messages = [
            factory.make_name("message"),
            factory.make_name("message"),
            factory.make_name("message"),
        ]

        legacy_logger.msg(*messages)

        expected = {
            "_message_0": messages[0],
            "_message_1": messages[1],
            "_message_2": messages[2],
            "log_format": "{_message_0} {_message_1} {_message_2}",
        }
        self.assertThat(events, HasLength(1))
        self.assertThat(events[0], ContainsDictByEquality(expected))
        self.assertThat(
            logger.formatEventAsClassicLogText(events[0], formatTimeStatic),
            Equals("<when> [%s#info] %s\n" % (__name__, " ".join(messages))))
Пример #10
0
def printToConsole(event):
    log = formatEventAsClassicLogText(event)
    if log == None:
        print "NO LOG TRACEBACK", traceback.format_stack()
    print log,
Пример #11
0
 def fe(inp):
     return formatEventAsClassicLogText(inp) + "\n"
Пример #12
0
def utilityMain(
    configFileName, serviceClass, reactor=None, serviceMaker=None,
    patchConfig=None, onShutdown=None, verbose=False, loadTimezones=False
):
    """
    Shared main-point for utilities.

    This function will:

        - Load the configuration file named by C{configFileName},
        - launch a L{CalDAVServiceMaker}'s with the C{ProcessType} of
          C{"Utility"}
        - run the reactor, with start/stop events hooked up to the service's
          C{startService}/C{stopService} methods.

    It is C{serviceClass}'s responsibility to stop the reactor when it's
    complete.

    @param configFileName: the name of the configuration file to load.
    @type configuration: C{str}

    @param serviceClass: a 1-argument callable which takes an object that
        provides L{ICalendarStore} and/or L{IAddressbookStore} and returns an
        L{IService}.

    @param patchConfig: a 1-argument callable which takes a config object
        and makes and changes necessary for the tool.

    @param onShutdown: a 0-argument callable which will run on shutdown.

    @param reactor: if specified, the L{IReactorTime} / L{IReactorThreads} /
        L{IReactorTCP} (etc) provider to use.  If C{None}, the default reactor
        will be imported and used.
    """

    from calendarserver.tap.caldav import CalDAVServiceMaker, CalDAVOptions
    if serviceMaker is None:
        serviceMaker = CalDAVServiceMaker

    # We want to validate that the actual service is always an instance of WorkerService, so wrap the
    # service maker callback inside a function that does that check
    def _makeValidService(store):
        service = serviceClass(store)
        assert isinstance(service, WorkerService)
        return service

    # Install std i/o observer
    observers = []
    if verbose:
        observers.append(FileLogObserver(sys.stdout, lambda event: formatEventAsClassicLogText(event)))

    if reactor is None:
        from twisted.internet import reactor
    try:
        config = loadConfig(configFileName)
        if patchConfig is not None:
            patchConfig(config)

        checkDirectories(config)

        utilityLogFile = LogFile.fromFullPath(
            config.UtilityLogFile,
            rotateLength=config.ErrorLogRotateMB * 1024 * 1024,
            maxRotatedFiles=config.ErrorLogMaxRotatedFiles
        )
        utilityLogObserver = FileLogObserver(utilityLogFile, lambda event: formatEventAsClassicLogText(event))
        utilityLogObserver._encoding = "utf-8"
        observers.append(utilityLogObserver)
        Logger.beginLoggingTo(observers, redirectStandardIO=False)

        config.ProcessType = "Utility"
        config.UtilityServiceClass = _makeValidService

        autoDisableMemcached(config)

        maker = serviceMaker()

        # Only perform post-import duties if someone has explicitly said to
        maker.doPostImport = getattr(maker, "doPostImport", False)

        options = CalDAVOptions
        service = maker.makeService(options)

        reactor.addSystemEventTrigger("during", "startup", service.startService)
        reactor.addSystemEventTrigger("before", "shutdown", service.stopService)
        if onShutdown is not None:
            reactor.addSystemEventTrigger("before", "shutdown", onShutdown)

        if loadTimezones:
            TimezoneCache.create()

    except (ConfigurationError, OSError), e:
        sys.stderr.write("Error: %s\n" % (e,))
        return
Пример #13
0
 def oneLogMessage(self, message):
     evt = eventFromJSON(message)
     text = formatEventAsClassicLogText(evt)
     messageBytes = (text).encode("utf-8")
     os.write(STDERR, messageBytes)
     return {}
Пример #14
0
def printToConsole(event):
    log = formatEventAsClassicLogText(event)
    if log == None:
        print "NO LOG TRACEBACK", traceback.format_stack()
    print log,