captive.whump.shanti-portal/plugins/sample_log.py

49 lines
1.4 KiB
Python
Raw Normal View History

# Demonstration plugin, only logs a message.
# Sets up logging by importing from the bottle app in the parent dir.
from logging import getLogger, DEBUG, WARN, INFO
2016-04-17 09:21:37 +00:00
try:
2016-04-18 19:33:35 +00:00
from configparser import RawConfigParser
2016-04-17 09:21:37 +00:00
except ImportError:
from ConfigParser import RawConfigParser
from portal import logHandler, logFormatter
def run(arg):
# The WSGI environ dict should always be there, sans any special objects
# like io streams.
environ = arg['environ']
2016-04-17 09:21:37 +00:00
plugin_config = arg['config']
config = RawConfigParser(defaults=plugin_config)
config.add_section('sample_log')
config._sections['sample_log'] = plugin_config
2016-04-16 19:30:17 +00:00
l = getLogger('plugin_log')
l.addHandler(logHandler)
2016-04-20 16:26:25 +00:00
if config.getboolean('sample_log', 'debug'):
2016-04-17 08:21:22 +00:00
l.setLevel(DEBUG)
l.debug('debug logging enabled')
log_url = '{proto}://{server}:{port}{request}'.format(
proto=environ.get('wsgi.url_scheme'),
server=environ.get('SERVER_NAME'),
port=environ.get('SERVER_PORT'),
request=environ.get('PATH_INFO')
)
log_client = '{client_ip}'.format(
2016-04-16 22:24:34 +00:00
client_ip=environ.get(
'HTTP_X_FORWARDED_FOR',
environ.get('REMOTE_ADDR')
2016-04-16 22:25:37 +00:00
)
)
# Log a msg
l.info('{log_client} - {method} - {log_url}'.format(
log_client=log_client,
log_url=log_url,
method=environ.get('REQUEST_METHOD')
))