NewsBlur-viq/utils/log.py

170 lines
4.5 KiB
Python
Raw Normal View History

import logging
import re
import string
import time
from django.core.handlers.wsgi import WSGIRequest
from django.conf import settings
class NullHandler(logging.Handler): #exists in python 3.1
def emit(self, record):
pass
def getlogger():
logger = logging.getLogger('newsblur')
return logger
def user(u, msg):
platform = '------'
time_elapsed = ""
if isinstance(u, WSGIRequest):
request = u
u = request.user
user_agent = request.environ.get('HTTP_USER_AGENT', '')
if 'iPad App' in user_agent:
platform = 'iPad'
elif 'iPhone App' in user_agent:
platform = 'iPhone'
elif 'Blar' in user_agent:
platform = 'Blar'
elif 'Android' in user_agent:
platform = 'Androd'
elif 'MSIE' in user_agent:
platform = 'IE'
if 'MSIE 9' in user_agent:
platform += '9'
elif 'MSIE 10' in user_agent:
platform += '10'
elif 'MSIE 8' in user_agent:
platform += '8'
elif 'Chrome' in user_agent:
platform = 'Chrome'
elif 'Safari' in user_agent:
platform = 'Safari'
elif 'MeeGo' in user_agent:
platform = 'MeeGo'
elif 'Firefox' in user_agent:
platform = 'FF'
elif 'Opera' in user_agent:
platform = 'Opera'
2011-12-22 15:16:45 -08:00
elif 'WP7' in user_agent:
platform = 'WP7'
if hasattr(request, 'start_time'):
2012-07-13 17:51:25 -07:00
seconds = time.time() - request.start_time
time_elapsed = "[%s%.4ss] " % (
'~FB' if seconds < .5 else '~FR',
seconds,
)
premium = '*' if u.is_authenticated() and u.profile.is_premium else ''
username = cipher(unicode(u)) if settings.CIPHER_USERNAMES else u
2012-07-13 15:32:27 -07:00
info(' ---> [~FB~SN%-6s~SB] %s[%s%s] %s' % (platform, time_elapsed, username, premium, msg))
def cipher(msg):
shift = len(msg)
in_alphabet = unicode(string.ascii_lowercase)
out_alphabet = in_alphabet[shift:] + in_alphabet[:shift]
translation_table = dict((ord(ic), oc) for ic, oc in zip(in_alphabet, out_alphabet))
return msg.translate(translation_table)
def debug(msg):
logger = getlogger()
logger.debug(colorize(msg))
def info(msg):
logger = getlogger()
logger.info(colorize(msg))
def error(msg):
logger = getlogger()
logger.error(msg)
def colorize(msg):
params = {
2010-12-05 16:59:48 -05:00
r'\-\-\->' : '~FB~SB--->~FW',
r'\*\*\*>' : '~FB~SB~BB--->~BT~FW',
r'\[' : '~SB~FB[~SN~FM',
2010-12-05 17:01:14 -05:00
r'AnonymousUser' : '~FBAnonymousUser',
r'\*\]' : '~SN~FR*]',
2010-12-05 16:59:48 -05:00
r'\]' : '~FB~SB]~FW~SN',
}
colors = {
'~SB' : Style.BRIGHT,
'~SN' : Style.NORMAL,
'~SK' : Style.BLINK,
'~SU' : Style.UNDERLINE,
'~ST' : Style.RESET_ALL,
'~FK': Fore.BLACK,
'~FR': Fore.RED,
'~FG': Fore.GREEN,
'~FY': Fore.YELLOW,
'~FB': Fore.BLUE,
'~FM': Fore.MAGENTA,
'~FC': Fore.CYAN,
'~FW': Fore.WHITE,
'~FT': Fore.RESET,
'~BK': Back.BLACK,
'~BR': Back.RED,
'~BG': Back.GREEN,
'~BY': Back.YELLOW,
'~BB': Back.BLUE,
'~BM': Back.MAGENTA,
'~BC': Back.CYAN,
'~BW': Back.WHITE,
'~BT': Back.RESET,
}
for k, v in params.items():
msg = re.sub(k, v, msg)
msg = msg + '~ST~FW~BT'
# msg = re.sub(r'(~[A-Z]{2})', r'%(\1)s', msg)
for k, v in colors.items():
msg = msg.replace(k, v)
return msg
'''
This module generates ANSI character codes to printing colors to terminals.
See: http://en.wikipedia.org/wiki/ANSI_escape_code
'''
COLOR_ESC = '\033['
class AnsiCodes(object):
def __init__(self, codes):
for name in dir(codes):
if not name.startswith('_'):
value = getattr(codes, name)
setattr(self, name, COLOR_ESC + str(value) + 'm')
class AnsiFore:
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
RESET = 39
class AnsiBack:
BLACK = 40
RED = 41
GREEN = 42
YELLOW = 43
BLUE = 44
MAGENTA = 45
CYAN = 46
WHITE = 47
RESET = 49
class AnsiStyle:
BRIGHT = 1
DIM = 2
UNDERLINE = 4
BLINK = 5
NORMAL = 22
RESET_ALL = 0
Fore = AnsiCodes(AnsiFore)
Back = AnsiCodes(AnsiBack)
Style = AnsiCodes(AnsiStyle)