Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
first attempt at logging
Browse files Browse the repository at this point in the history
  • Loading branch information
djetelina committed Jun 4, 2016
1 parent c6937e6 commit bb42d3d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
cogs/avatar/
cogs/db/
helpers/tokens.py
*.log
2 changes: 0 additions & 2 deletions cogs/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ async def github(self): # returns link to github for this bot

@commands.command(description=desc.twitch, brief=desc.twitchb)
async def twitch(self):
# finds status of Idiotech's twitch stream
# if live, it will return amount of viewers, current stream up-time and game being played
with aiohttp.ClientSession() as session:
async with session.get('https://api.twitch.tv/kraken/streams?channel=idiotechgaming')as resp:
data = await resp.json()
Expand Down
10 changes: 4 additions & 6 deletions cogs/giveaway.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import random
import time
import logging

from discord.ext import commands

Expand All @@ -10,7 +11,7 @@
# List with running giveaway instances
giveawayslist = []
loop = asyncio.get_event_loop()

log = logging.getLogger(__name__)

class Giveaway:
"""
Expand Down Expand Up @@ -41,6 +42,7 @@ def __init__(self, game, countdown, channel, owner, bot):
self.status = 1
self.bot = bot
giveawayslist.append(self)
log.info("Giveaway for {} is being started by {}".format(self.game, self.owner.name))

async def countdown(self):
"""
Expand Down Expand Up @@ -122,10 +124,6 @@ async def giveaway(self, ctx):
reply = "No giveaway open"

await self.bot.say(reply)
try:
await self.bot.delete_message(ctx.message)
except Exception as e:
print(e, ctx.message.channel.name)

@giveaway.command(name="open", pass_context=True, description=desc.openga, brief=desc.opengab)
async def _open(self, ctx, countdown: int, *, game: str):
Expand Down Expand Up @@ -238,7 +236,7 @@ async def enroll(self, ctx, *, game: str):
try:
await self.bot.delete_message(ctx.message)
except Exception as e:
print(e)
log.exception("Couldn't delete enroll message")


def parsesecs(sec: int) -> str:
Expand Down
4 changes: 3 additions & 1 deletion cogs/overwatch.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import asyncio
import logging

import bs4
import requests
from discord.ext import commands

from helpers import descriptions as desc

log = logging.getLogger('__main__').setLevel(logging.INFO)

class Overwatch:
def __init__(self, bot):
Expand Down Expand Up @@ -44,6 +46,7 @@ async def overwatch(self, region: str, battletag: str):
except Exception as e:
await self.bot.edit_message(msg, "**Error with request. Please check for mistakes before trying again.**"
".\nError: "+str(e))
log.exception("Error with request")
return

doc = bs4.BeautifulSoup(res.text, "html.parser")
Expand All @@ -67,4 +70,3 @@ async def overwatch(self, region: str, battletag: str):

def setup(bot):
bot.add_cog(Overwatch(bot))

7 changes: 4 additions & 3 deletions cogs/stats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sqlite3
import logging

from discord.ext import commands

Expand Down Expand Up @@ -78,7 +79,7 @@ def is_in_db(self, table, check):
column = "command"

else:
print("DB error is_in_db")
logging.warning("DB error is_in_db")
return

query = """
Expand All @@ -102,7 +103,7 @@ def new_entry(self, table, entry):
column = "command"

else:
print("DB error new_entry")
logging.warning("DB error new_entry")
return

query = """
Expand Down Expand Up @@ -146,7 +147,7 @@ def get_total(self, table):
column = "used"

else:
print("DB error get_total")
logging.warning("DB error get_total")
return

query = """
Expand Down
29 changes: 24 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
import logging

import discord
from discord.ext import commands
Expand All @@ -11,8 +11,7 @@

@bot.event
async def on_ready():
"""After logging in"""
print(bot.user.name + ' logged in at ' + str(datetime.now()))
log.info(bot.user.name + ' logged in')
await bot.change_status(game=discord.Game(name=settings.now_playing))


Expand All @@ -31,8 +30,12 @@ async def on_message(message):

@bot.event
async def on_command(command, ctx):
stats = bot.get_cog('Stats')
try:
log.info("#{2}:{1} called {0}".format(command.name, ctx.message.author.name, ctx.message.channel.name))
except AttributeError:
log.info("PM:{1} called {0}".format(command.name, ctx.message.author.name))

stats = bot.get_cog('Stats')
if stats is not None:
await stats.on_command_p(command.name)

Expand Down Expand Up @@ -98,11 +101,27 @@ async def unload(*, module: str):


if __name__ == '__main__':
log = logging.getLogger()
log.setLevel(logging.INFO)
console = logging.StreamHandler()
log_file = logging.FileHandler('log.log', 'w', 'utf-8')

console.setLevel(logging.INFO)
log_file.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', "%d.%m.%Y %H:%M:%S")
console.setFormatter(formatter)
log_file.setFormatter(formatter)

log.addHandler(log_file)
log.addHandler(console)

for extension in settings.extensions:
try:
bot.load_extension(extension)
log.info('{} loaded'.format(extension))

except Exception as e:
print('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))
log.warning('Failed to load extension {}\n{}: {}'.format(extension, type(e).__name__, e))

bot.run(t.token)

0 comments on commit bb42d3d

Please sign in to comment.