reduce logging

This commit is contained in:
Luna Mendes 2017-12-03 22:30:23 -03:00
parent 07842e356e
commit e974dee9b7
2 changed files with 27 additions and 8 deletions

View file

@ -12,3 +12,5 @@ command_channel = 386989864014446612
sexr_chan = 386990164896907264 sexr_chan = 386990164896907264
admin_role = 386566510803681280 admin_role = 386566510803681280
bot_token = None

View file

@ -15,7 +15,7 @@ import asyncpg
import config import config
from bot import schedule_bot from bot import schedule_bot
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
db = None db = None
bot = None bot = None
@ -29,6 +29,15 @@ bot = None
# - op 2 : rsudo # - op 2 : rsudo
async def wrap(coro):
try:
await coro()
except ConnectionError as e:
log.warning('connection err: %r', e)
except:
log.exception('error inside wrapped')
def parse_logstr(string): def parse_logstr(string):
# '2015-02-11T19:05:10+00:00 labrat-1 snoopy[896]: [uid:0 sid:11679 # '2015-02-11T19:05:10+00:00 labrat-1 snoopy[896]: [uid:0 sid:11679
# tty:/dev/pts/2 cwd:/root filename:/usr/bin/cat]: cat /etc/fstab.BAK' # tty:/dev/pts/2 cwd:/root filename:/usr/bin/cat]: cat /etc/fstab.BAK'
@ -55,11 +64,12 @@ def parse_logstr(string):
async def read_msg(reader): async def read_msg(reader):
header = await reader.read(8) header = await reader.read(8)
log.debug('[recv] %r', header)
length, op = struct.unpack('Ii', header) length, op = struct.unpack('Ii', header)
data = await reader.read(length) data = await reader.read(length)
data = data.decode() data = data.decode()
log.info('[recv] %d %d %s', length, op, data) log.debug('[recv] %d %d %s', length, op, data)
return op, data return op, data
@ -72,12 +82,12 @@ async def read_payload(reader):
async def send_msg(writer, op: int, data: str): async def send_msg(writer, op: int, data: str):
header = struct.pack('Ii', len(data), op) header = struct.pack('Ii', len(data), op).decode()
msg = f'{header.decode()}{data}'.encode() msg = f'{header}{data}'.encode()
log.info('[send] %d, %s -> %r', op, data, msg) log.debug('[send] %d, %s -> %r', op, data, msg)
writer.write(msg) writer.write(msg)
asyncio.get_event_loop().create_task(writer.drain()) asyncio.get_event_loop().create_task(wrap(writer.drain))
async def process(reader, writer, op: int, message: str): async def process(reader, writer, op: int, message: str):
@ -85,15 +95,21 @@ async def process(reader, writer, op: int, message: str):
if op == 1: if op == 1:
uid, cwd, command = parse_logstr(message) uid, cwd, command = parse_logstr(message)
log.info('[process] Logging command uid=%d cwd=%r cmd=%r',
uid, cwd, command)
await db.execute(""" await db.execute("""
INSERT INTO logs (uid, cwd, cmd) VALUES ($1, $2, $3) INSERT INTO logs (uid, cwd, cmd) VALUES ($1, $2, $3)
""", uid, cwd, command) """, uid, cwd, command)
elif op == 2: elif op == 2:
log.debug(bot.cogs) if not bot:
return await send_msg(writer, 1, 'no bot up')
rsudo = bot.get_cog('Rsudo') rsudo = bot.get_cog('Rsudo')
if not rsudo: if not rsudo:
return await send_msg(writer, 1, 'no rsudo cog') return await send_msg(writer, 1, 'no rsudo cog')
log.info('[process] got rsudo! %r', message)
await rsudo.create_request(message) await rsudo.create_request(message)
return await send_msg(writer, 1, 'ok') return await send_msg(writer, 1, 'ok')
@ -112,9 +128,10 @@ async def handle_client(reader, writer):
await process(reader, writer, op, message) await process(reader, writer, op, message)
writer.close() writer.close()
except ConnectionError as e:
log.warning('connection err: %r', e)
except Exception as e: except Exception as e:
log.exception('error at handler coro') log.exception('error at handler coro')
await send_msg(writer, -1, repr(e))
writer.close() writer.close()