Skip to content

Commit 698bfef

Browse files
committed
Libreria de logger
1 parent 163330e commit 698bfef

File tree

10 files changed

+54
-39
lines changed

10 files changed

+54
-39
lines changed

bin/pycamp_manager.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import argparse
22
import datetime
3-
import logging
43
from pycamp_bot.models import Pycamp
54
from pycamp_bot.models import Pycampista
65
from pycamp_bot.models import PycampistaAtPycamp
76
from pycamp_bot.models import models_db_connection
7+
from pycamp_bot.logger import logger
88

99

1010
def parse_args():
@@ -59,20 +59,20 @@ def parse_args():
5959

6060
if args.wich == 'pycampista':
6161
if args.add:
62-
logging.info('Adding pycampista')
62+
logger.info('Adding pycampista')
6363
pycampista = Pycampista.create(username=args.name)
6464

6565
if args.arrive:
6666
arrive_time = datetime.datetime.fromisoformat(args.arrive)
67-
logging.info('Changing arrive time to {}'.format(arrive_time))
67+
logger.info('Changing arrive time to {}'.format(arrive_time))
6868
pycampista = Pycampista.select().where(
6969
Pycampista.username == args.name)[0]
7070
pycampista.arrive = arrive_time
7171
pycampista.save()
7272

7373
if args.departure:
7474
departure_time = datetime.datetime.fromisoformat(args.departure)
75-
logging.info('Changing departure time to {}'.format(departure_time))
75+
logger.info('Changing departure time to {}'.format(departure_time))
7676
pycampista = Pycampista.select().where(
7777
Pycampista.username == args.name)[0]
7878
pycampista.leave = departure_time
@@ -83,20 +83,20 @@ def parse_args():
8383
print(pycampista)
8484
elif args.wich == 'pycamp':
8585
if args.add:
86-
logging.info('Adding pycamp')
86+
logger.info('Adding pycamp')
8787
pycamp = Pycamp.create(headquarters=args.name)
8888

8989
if args.init:
9090
init_time = datetime.datetime.fromisoformat(args.init)
91-
logging.info('Changing init time to {}'.format(init_time))
91+
logger.info('Changing init time to {}'.format(init_time))
9292
pycamp = Pycamp.select().where(
9393
Pycamp.headquarters == args.name)[0]
9494
pycamp.init = init_time
9595
pycamp.save()
9696

9797
if args.end:
9898
end_time = datetime.datetime.fromisoformat(args.end)
99-
logging.info('Changing end time to {}'.format(end_time))
99+
logger.info('Changing end time to {}'.format(end_time))
100100
pycamp = Pycamp.select().where(
101101
Pycamp.headquarters == args.name)[0]
102102
pycamp.end = end_time

bin/run_bot.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import os
32
from telegram.ext import Application
43
from pycamp_bot.commands import auth
@@ -11,12 +10,7 @@
1110
from pycamp_bot.commands import schedule
1211
from pycamp_bot.commands import announcements
1312
from pycamp_bot.models import models_db_connection
14-
15-
16-
logging.basicConfig(
17-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
18-
level=logging.INFO)
19-
logger = logging.getLogger(__name__)
13+
from pycamp_bot.logger import logger
2014

2115

2216
def set_handlers(application):

src/pycamp_bot/commands/announcements.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import logging
21
from telegram.ext import CommandHandler
32
from pycamp_bot.models import Project, Pycampista, Vote
43
from pycamp_bot.commands.auth import get_admins_username
54

65

7-
logger = logging.getLogger(__name__)
8-
9-
106
async def announce(update, context):
117
username = update.message.from_user.username
128
admins = get_admins_username()

src/pycamp_bot/commands/auth.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import logging
21
import os
32
from telegram.ext import CommandHandler
43
from pycamp_bot.models import Pycampista
5-
6-
7-
logger = logging.getLogger(__name__)
4+
from pycamp_bot.logger import logger
85

96

107
def get_admins_username():

src/pycamp_bot/commands/base.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import logging
21
from telegram.ext import CommandHandler
32
from pycamp_bot.commands.help_msg import get_help
4-
5-
6-
logger = logging.getLogger(__name__)
3+
from pycamp_bot.logger import logger
74

85

96
async def msg_to_active_pycamp_chat(bot, text):

src/pycamp_bot/commands/manage_pycamp.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import datetime
2-
import logging
32
from telegram.ext import CommandHandler
43
from pycamp_bot.models import Pycamp
54
from pycamp_bot.models import Pycampista
65
from pycamp_bot.models import PycampistaAtPycamp
76
from pycamp_bot.commands.auth import admin_needed
8-
9-
logger = logging.getLogger(__name__)
7+
from pycamp_bot.logger import logger
108

119

1210
def get_pycamp_by_name(name):

src/pycamp_bot/commands/projects.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import logging
21
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters
32
from pycamp_bot.models import Pycampista, Project, Vote
43
from pycamp_bot.commands.base import msg_to_active_pycamp_chat
54
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
65
from pycamp_bot.commands.auth import admin_needed
6+
from pycamp_bot.logger import logger
77

88

99
current_projects = {}
1010
NOMBRE, DIFICULTAD, TOPIC = ["nombre", "dificultad", "topic"]
1111

12-
logger = logging.getLogger(__name__)
13-
1412

1513
def load_authorized(f):
1614
async def wrap(*args, **kargs):

src/pycamp_bot/commands/schedule.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import string
32
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters
43
from pycamp_bot.models import Project, Slot, Pycampista, Vote
@@ -9,8 +8,6 @@
98

109
DAY_LETTERS = []
1110

12-
logger = logging.getLogger(__name__)
13-
1411

1512
def _dictToString(dicto):
1613
if dicto:

src/pycamp_bot/commands/voting.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import logging
21
import peewee
32
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
43
from telegram.ext import CommandHandler, CallbackQueryHandler
54
from pycamp_bot.commands.base import msg_to_active_pycamp_chat
65
from pycamp_bot.commands.auth import admin_needed
76
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
87
from pycamp_bot.models import Pycampista, Project, Vote
9-
10-
11-
logger = logging.getLogger(__name__)
8+
from pycamp_bot.logger import logger
129

1310

1411
def vote_authorized(f):

src/pycamp_bot/logger.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Módulo para hacer loggeos."""
2+
3+
import logging
4+
5+
6+
LOG_FORMAT = '[%(asctime)s] - [%(filename)s %(lineno)d] %(levelname)s:'
7+
LOG_DATE_FORMAT = '%Y:%m:%d %H:%M:%S'
8+
9+
10+
class LogFormatter(logging.Formatter):
11+
"""Formateador del log."""
12+
grey = "\x1b[38;20m"
13+
blue = "\x1b[34;20m"
14+
yellow = "\x1b[33;20m"
15+
red = "\x1b[31;20m"
16+
bold_red = "\x1b[31;1m"
17+
reset = "\x1b[0m"
18+
19+
FORMATS = {
20+
logging.DEBUG: LOG_FORMAT + ' ' + grey + '%(message)s' + reset,
21+
logging.INFO: LOG_FORMAT + ' ' + blue + '%(message)s' + reset,
22+
logging.WARNING: LOG_FORMAT + ' ' + yellow + '%(message)s' + reset,
23+
logging.ERROR: LOG_FORMAT + ' ' + red + '%(message)s' + reset,
24+
logging.CRITICAL: LOG_FORMAT + ' ' + bold_red + '%(message)s' + reset,
25+
}
26+
27+
def format(self, record):
28+
log_formatter = logging.Formatter(
29+
fmt=self.FORMATS.get(record.levelno),
30+
datefmt=LOG_DATE_FORMAT
31+
)
32+
return log_formatter.format(record)
33+
34+
35+
formatter = LogFormatter()
36+
ch = logging.StreamHandler()
37+
ch.setFormatter(formatter)
38+
logger = logging.getLogger(__name__)
39+
logger.addHandler(ch)
40+
logger.setLevel(logging.DEBUG)
41+
logger.propagate = False

0 commit comments

Comments
 (0)