Beispiel #1
0
    def test_log_output(self):
        """
        When a structured logging config is given, Synapse will use it.
        """
        beginner = FakeBeginner()
        publisher = setup_logging(self.hs,
                                  self.hs.config,
                                  logBeginner=beginner)

        # Make a logger and send an event
        logger = Logger(namespace="tests.logging.test_structured",
                        observer=publisher)

        with LoggingContext("testcontext", request="somereq"):
            logger.info("Hello there, {name}!", name="steve")

        with open(self.homeserver_log, "r") as f:
            logged_events = [
                eventAsText(x, includeTimestamp=False)
                for x in eventsFromJSONLogFile(f)
            ]

        logs = "\n".join(logged_events)
        self.assertTrue("***** STARTING SERVER *****" in logs)
        self.assertTrue("Hello there, steve!" in logs)
Beispiel #2
0
def print_log(path=None, output_stream=None):
    if path is None:
        from pychron.paths import paths
        path = os.path.join(paths.log_dir, 'pps.log.json')

    if output_stream is None:
        output_stream = sys.stdout
    elif isinstance(output_stream, (str, unicode)):
        output_stream = io.open(output_stream, 'w')

    output = textFileLogObserver(output_stream)
    for event in eventsFromJSONLogFile(io.open(path)):
        output(event)
Beispiel #3
0
def print_log(path=None, output_stream=None):
    if path is None:
        from pychron.paths import paths
        path = os.path.join(paths.log_dir, 'pps.log.json')

    if output_stream is None:
        output_stream = sys.stdout
    elif isinstance(output_stream, (str, six.text_type)):
        output_stream = io.open(output_stream, 'w')

    output = textFileLogObserver(output_stream)
    for event in eventsFromJSONLogFile(io.open(path)):
        output(event)
Beispiel #4
0
    def test_output_to_json_round_trip(self):
        """
        Synapse logs can be outputted to JSON and then read back again.
        """
        temp_dir = self.mktemp()
        os.mkdir(temp_dir)
        self.addCleanup(shutil.rmtree, temp_dir)

        json_log_file = os.path.abspath(os.path.join(temp_dir, "out.json"))

        log_config = {
            "drains": {
                "jsonfile": {
                    "type": "file_json",
                    "location": json_log_file
                }
            }
        }

        # Begin the logger with our config
        beginner = FakeBeginner()
        setup_structured_logging(self.hs,
                                 self.hs.config,
                                 log_config,
                                 logBeginner=beginner)

        # Make a logger and send an event
        logger = Logger(namespace="tests.logging.test_structured",
                        observer=beginner.observers[0])
        logger.info("Hello there, {name}!", name="wally")

        # Read the log file and check it has the event we sent
        with open(json_log_file, "r") as f:
            logged_events = list(eventsFromJSONLogFile(f))
        self.assertEqual(len(logged_events), 1)

        # The event pulled from the file should render fine
        self.assertEqual(
            eventAsText(logged_events[0], includeTimestamp=False),
            "[tests.logging.test_structured#info] Hello there, wally!",
        )
Beispiel #5
0
def print_log(fp, user=None, n=1, dest=sys.stdout):
    output = textFileLogObserver(dest)
    events, startups = [], []

    for event in eventsFromJSONLogFile(io.open(fp)):
        events.append(event)
        if event.get('log_format') == 'Start client':
            startups.append(event.get('log_time'))

    last_start_time = 0
    if len(startups) >= n:
        last_start_time = startups[-n]
    elif startups:
        last_start_time = startups[0]

    for event in events:
        fitting_timestamp = (last_start_time <= event.get('log_time'))
        fitting_name = (not user or user == event.get('player_name'))

        if fitting_timestamp and fitting_name:
            output(event)
Beispiel #6
0
def print_log(fp, user=None, n=1, dest=sys.stdout):
    output = textFileLogObserver(dest)
    events, startups = [], []

    for event in eventsFromJSONLogFile(io.open(fp)):
        events.append(event)
        if event.get('log_format') == 'Start client':
            startups.append(event.get('log_time'))

    last_start_time = 0
    if len(startups) >= n:
        last_start_time = startups[-n]
    elif startups:
        last_start_time = startups[0]

    for event in events:
        fitting_timestamp = (last_start_time <= event.get('log_time'))
        fitting_name = (not user or user == event.get('player_name'))

        if fitting_timestamp and fitting_name:
            output(event)
Beispiel #7
0
def iter_test_logs():
    log_file = io.open(DEFAULT_LOG_FILE)
    for event in eventsFromJSONLogFile(log_file):
        if event.get('log_namespace').startswith('test'):
            yield event
Beispiel #8
0
import sys
import io
from twisted.logger import eventsFromJSONLogFile, textFileLogObserver

output = textFileLogObserver(sys.stdout)

for event in eventsFromJSONLogFile(open("log.json")):
    output(event)
Beispiel #9
0
 def _file(self, r):
     from twisted.logger import eventsFromJSONLogFile
     return eventsFromJSONLogFile(r)
Beispiel #10
0
def iter_test_logs():
    log_file = io.open(DEFAULT_LOG_FILE)
    for event in eventsFromJSONLogFile(log_file):
        if event.get('log_namespace').startswith('test'):
            yield event
Beispiel #11
0
 def _file(self, r):
     from twisted.logger import eventsFromJSONLogFile
     return eventsFromJSONLogFile(r)
Beispiel #12
0
import io
from twisted.logger import eventsFromJSONLogFile
from analyze import analyze

for event in eventsFromJSONLogFile(io.open("log.json")):
    analyze(event)
 def process_json_stream(self, processor, stream):
     for msg in eventsFromJSONLogFile(stream):
         processor.process_message(Message(json=msg))
Beispiel #14
0
#!/usr/bin/env python3
import sys
import io
from twisted.logger import (eventsFromJSONLogFile, textFileLogObserver)

output = textFileLogObserver(sys.stdout)

if __name__ == "__main__":
    try:
        f = sys.argv[1]
        for event in eventsFromJSONLogFile(io.open(f)):
            output(event)
    except IndexError as e:
        print("EXAMPLE: view_log.py ./log.json")
    except Exception as e:
        raise e