annotate MoinMoin/logfile/eventlog.py @ 6111:1fdd537e9d83

SubProcess: reimplement exec_cmd subclassing Popen and overriding some methods isn't pretty. the code we have was written for py 2.4 or so and the py 2.7 Popen looked quite different. this way with the timer should be less problematic.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 06 Sep 2016 04:39:28 +0200
parents f662e5f7ca82
children
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 MoinMoin event log class
2286
01f05e74aa9c Big PEP8 and whitespace cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1962
diff changeset
3
1909
be140a589f11 improved some module docstrings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1791
diff changeset
4 The global event-log is mainly used for statistics (e.g. EventStats).
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5
1918
bb2e053067fb fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1909
diff changeset
6 @copyright: 2007 MoinMoin:ThomasWaldmann
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
7 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
9
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1000
diff changeset
10 import time
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1000
diff changeset
11
749
3dba26fcfde0 moved logfile/logfile.py to logfile/__init__.py
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 447
diff changeset
12 from MoinMoin.logfile import LogFile
1791
6dd2e29acffe Eclipse PyDev Check: fixed lots of its errors and warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1000
diff changeset
13 from MoinMoin import wikiutil
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
14
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
15 class EventLog(LogFile):
1962
40821d91633d logfile: improve docstrings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
16 """ The global event-log is mainly used for statistics (e.g. EventStats) """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
17 def __init__(self, request, filename=None, buffer_size=65536, **kw):
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
18 if filename is None:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
19 rootpagename = kw.get('rootpagename', None)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
20 if rootpagename:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
21 from MoinMoin.Page import Page
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
22 filename = Page(request, rootpagename).getPagePath('event-log', isfile=1)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
23 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
24 filename = request.rootpage.getPagePath('event-log', isfile=1)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
25 LogFile.__init__(self, filename, buffer_size)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
26
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
27 def add(self, request, eventtype, values=None, add_http_info=1,
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
28 mtime_usecs=None):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
29 """ Write an event of type `eventtype, with optional key/value
1962
40821d91633d logfile: improve docstrings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
30 pairs appended (i.e. you have to pass a dict).
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
31 """
5977
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
32 cfg = request.cfg
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
33 if cfg.log_events_format == 0 or request.isSpiderAgent:
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
34 # no event logging enabled or user agent is a bot / spider
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
35 return
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
36
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
37 if mtime_usecs is None:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
38 mtime_usecs = wikiutil.timestamp2version(time.time())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
39
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
40 if values is None:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
41 values = {}
5977
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
42 if cfg.log_remote_addr and add_http_info:
4470
ea2aa935fbde cfg.log_remote_addr (default: True) controls whether moin logs remote's IP/hostname to edit-log and event-log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
43 # if cfg.log_remote_addr is False (usually for privacy reasons),
ea2aa935fbde cfg.log_remote_addr (default: True) controls whether moin logs remote's IP/hostname to edit-log and event-log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2286
diff changeset
44 # we likely do not want to log user agent and http referer either.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
45 for key in ['remote_addr', 'http_user_agent', 'http_referer']:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46 value = getattr(request, key, '')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
47 if value:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
48 # Save those http headers in UPPERcase
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
49 values[key.upper()] = value
5977
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
50
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
51 if cfg.log_events_format == 2:
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
52 values['username'] = request.user.name
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
53 values['wikiname'] = cfg.interwikiname
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
54 values['url'] = request.url
f662e5f7ca82 cfg.log_events_format to configure event-log format, add a tool to export event-log to CSV
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4569
diff changeset
55
102
a54cebaab73e move make / parse query string functions to wikiutil, make unicode-safe
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
56 # Encode values in a query string TODO: use more readable format
4569
3caaa8c74c41 wikiutil: replace moin's cgi/urllib wrappers by calls to werkzeug.utils code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4470
diff changeset
57 values = wikiutil.makeQueryString(values)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
58 self._add(u"%d\t%s\t%s\n" % (mtime_usecs, eventtype, values))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
59
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
60 def parser(self, line):
1962
40821d91633d logfile: improve docstrings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
61 """ parse a event-log line into its components """
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
62 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63 time_usecs, eventtype, kvpairs = line.rstrip().split('\t')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
64 except ValueError:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
65 # badly formatted line in file, skip it
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
66 return None
102
a54cebaab73e move make / parse query string functions to wikiutil, make unicode-safe
Thomas Waldmann <tw@waldmann-edv.de>
parents: 0
diff changeset
67 return long(time_usecs), eventtype, wikiutil.parseQueryString(kvpairs)
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
68
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
69 def set_filter(self, event_types=None):
1962
40821d91633d logfile: improve docstrings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1918
diff changeset
70 """ optionally filter log for specific event types """
1000
6f6fcbe200b5 whitespace-only cleanup and minor style changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 749
diff changeset
71 if event_types is None:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
72 self.filter = None
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
73 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
74 self.filter = lambda line: (line[1] in event_types)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
75
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
76