コード例 #1
0
ファイル: test_log.py プロジェクト: shawn0915/pgtoolkit
def test_parse():
    from pgtoolkit.log import parse, UnknownData

    lines = """\
\tResult  (cost=0.00..0.01 rows=1 width=4) (actual time=1001.117..1001.118 rows=1 loops=1)
\t  Output: pg_sleep('1'::double precision)
2018-06-15 10:03:53.488 UTC [7931]: [2-1] app=[unknown],db=[unknown],client=[local],user=[unknown] LOG:  incomplete startup packet
2018-06-15 10:44:42.923 UTC [8280]: [2-1] app=,db=,client=,user= LOG:  checkpoint starting: shutdown immediate
2018-06-15 10:44:58.206 UTC [8357]: [4-1] app=psql,db=postgres,client=[local],user=postgres HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
2018-06-15 10:45:03.175 UTC [8357]: [7-1] app=psql,db=postgres,client=[local],user=postgres LOG:  duration: 1002.209 ms  statement: select pg_sleep(1);
2018-06-15 10:49:11.512 UTC [8357]: [8-1] app=psql,db=postgres,client=[local],user=postgres LOG:  duration: 0.223 ms  statement: show log_timezone;
2018-06-15 10:49:26.084 UTC [8420]: [2-1] app=[unknown],db=postgres,client=[local],user=postgres LOG:  connection authorized: user=postgres database=postgres
2018-06-15 10:49:26.088 UTC [8420]: [3-1] app=psql,db=postgres,client=[local],user=postgres LOG:  duration: 1.449 ms  statement: SELECT d.datname as "Name",
\t       pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
\t       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
\t       d.datcollate as "Collate",
\t       d.datctype as "Ctype",
\t       pg_catalog.array_to_string(d.datacl, E'\\n') AS "Access privileges"
\tFROM pg_catalog.pg_database d
\tORDER BY 1;
2018-06-15 10:49:26.088 UTC [8420]: [4-1] app=psql,db=postgres,client=[local],user=postgres LOG:  disconnection: session time: 0:00:00.006 user=postgres database=postgres host=[local]
BAD PREFIX 10:49:31.140 UTC [8423]: [1-1] app=[unknown],db=[unknown],client=[local],user=[unknown] LOG:  connection received: host=[local]
""".splitlines(True)  # noqa

    log_line_prefix = '%m [%p]: [%l-1] app=%a,db=%d,client=%h,user=%u '
    records = list(parse(lines, prefix_fmt=log_line_prefix))

    assert isinstance(records[0], UnknownData)
    assert '\n' not in repr(records[0])
    record = records[1]
    assert 'LOG' == record.severity
コード例 #2
0
ファイル: test_log.py プロジェクト: shawn0915/pgtoolkit
def test_filters():
    from pgtoolkit.log import parse, NoopFilters

    lines = """\
stage1 LOG:  duration: 1002.209 ms  statement: select pg_sleep(1);
stage2 LOG:  duration: 0.223 ms  statement: show log_timezone;
stage3 LOG:  connection authorized: user=postgres database=postgres
""".splitlines(True)  # noqa

    class MyFilters(NoopFilters):
        def stage1(self, record):
            return record.prefix.startswith('stage1')

        def stage2(self, record):
            return record.prefix.startswith('stage2')

        def stage3(self, record):
            return record.prefix.startswith('stage3')

    log_line_prefix = 'stage%p '
    filters = MyFilters()
    records = list(parse(lines, prefix_fmt=log_line_prefix, filters=filters))
    assert 0 == len(records)