Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorized logging #2604

Closed
wants to merge 25 commits into from
Closed
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
beea535
Initial logging changes
gurupras Aug 4, 2016
8fc7dc6
Converted some more logger.log()s
gurupras Aug 4, 2016
f2b3aad
Merge branch 'logging' into colorized-logging
gurupras Aug 4, 2016
2a4c95b
Tiny spacing fix
gurupras Aug 4, 2016
53312b7
Fixed bug where old logging format was still in use
gurupras Aug 4, 2016
17d23a6
Merge branch 'dev' of github.com:PokemonGoF/PokemonGo-Bot into colori…
gurupras Aug 5, 2016
4dd21ba
Removed extraneous print as pointed out by @douglascamata
gurupras Aug 7, 2016
bdbabf2
Removed all lcd reference
gurupras Aug 7, 2016
4ba146d
Addded some comments
gurupras Aug 7, 2016
043801c
Removed logger.verbose()
gurupras Aug 7, 2016
79050bb
Removed additional prints from event_manager.py
gurupras Aug 7, 2016
9a550d3
Revert "Removed additional prints from event_manager.py"
gurupras Aug 7, 2016
17ae46b
Removed some more code related to LCD that somehow got through the fi…
gurupras Aug 7, 2016
79bf1b2
Renamed LogFormatter to ColorizedLogFormatter
gurupras Aug 7, 2016
64ef6c4
Merge branch 'dev' into colorized-logging
gurupras Aug 7, 2016
4176d7b
Merge branch 'dev' of github.com:PokemonGoF/PokemonGo-Bot into colori…
gurupras Aug 7, 2016
cbba16a
Merge branch 'dev' into colorized-logging
gurupras Aug 8, 2016
6cb0192
Fixed logging system initialization
gurupras Aug 8, 2016
babffeb
Updated EventManager() function so it more closely reflects previous …
gurupras Aug 8, 2016
681a7d1
Fixed whitespace that crept in while refactoring
gurupras Aug 8, 2016
6baa70d
Removed some of the commas-without-colors
gurupras Aug 8, 2016
ba5ab09
Removed additional initialize() method that was added to EventHandler…
gurupras Aug 8, 2016
e724c06
Refactored comments and order of initialization of LoggingHandler's _…
gurupras Aug 8, 2016
6c14602
Merge branch 'dev' of github.com:PokemonGoF/PokemonGo-Bot into colori…
gurupras Aug 9, 2016
3fa43b9
Added import of unicode_literals from future to logger.py
gurupras Aug 9, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(name)10s] [%(levelname)s] %(message)s')
from pokemongo_bot import logger
logger.init_logger()
logger = logging.getLogger('cli')
logger.setLevel(logging.INFO)

120 changes: 72 additions & 48 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -100,34 +100,35 @@ def _register_events(self):
'location_found',
parameters=('position', 'location')
)
self.event_manager.register_event('api_error')
self.event_manager.register_event('config_error')
self.event_manager.register_event('api_error', color='red')
self.event_manager.register_event('config_error', color='red')

self.event_manager.register_event('login_started')
self.event_manager.register_event('login_failed')
self.event_manager.register_event('login_successful')
self.event_manager.register_event('login_started', color='green')
self.event_manager.register_event('login_failed', color='red')
self.event_manager.register_event('login_successful', color='green')

self.event_manager.register_event('set_start_location')
self.event_manager.register_event('load_cached_location')
self.event_manager.register_event('location_cache_ignored')
self.event_manager.register_event('set_start_location', color='yellow')
self.event_manager.register_event('load_cached_location', color='yellow')
self.event_manager.register_event('location_cache_ignored', color='yellow')
self.event_manager.register_event(
'position_update',
parameters=(
'current_position',
'last_position',
'distance', # optional
'distance_unit' # optional
)
),
color='cyan'
)
self.event_manager.register_event('location_cache_error')
self.event_manager.register_event('location_cache_error', color='red')

self.event_manager.register_event('bot_start')
self.event_manager.register_event('bot_exit')
self.event_manager.register_event('bot_start', color='green')
self.event_manager.register_event('bot_exit', color='red')

# sleep stuff
self.event_manager.register_event(
'next_sleep',
parameters=('time',)
parameters=('time',),
)
self.event_manager.register_event(
'bot_sleep',
@@ -141,7 +142,8 @@ def _register_events(self):
'fort_id',
'latitude',
'longitude'
)
),
color='cyan'
)
self.event_manager.register_event(
'lured_pokemon_found',
@@ -151,14 +153,15 @@ def _register_events(self):
'encounter_id',
'latitude',
'longitude'
)
),
color='blue'
)
self.event_manager.register_event(
'moving_to_fort',
parameters=(
'fort_name',
'distance'
)
),
)
self.event_manager.register_event(
'moving_to_lured_fort',
@@ -172,26 +175,31 @@ def _register_events(self):
'spun_pokestop',
parameters=(
'pokestop', 'exp', 'items'
)
),
color='cyan'
)
self.event_manager.register_event(
'pokestop_empty',
parameters=('pokestop',)
parameters=('pokestop',),
color='yellow'
)
self.event_manager.register_event(
'pokestop_out_of_range',
parameters=('pokestop',)
parameters=('pokestop',),
color='yellow'
)
self.event_manager.register_event(
'pokestop_on_cooldown',
parameters=('pokestop', 'minutes_left')
parameters=('pokestop', 'minutes_left'),
color='red'
)
self.event_manager.register_event(
'unknown_spin_result',
parameters=('status_code',)
parameters=('status_code',),
color='red'
)
self.event_manager.register_event('pokestop_searching_too_often')
self.event_manager.register_event('arrived_at_fort')
self.event_manager.register_event('pokestop_searching_too_often', color='red')
self.event_manager.register_event('arrived_at_fort', color='green')

# pokemon stuff
self.event_manager.register_event(
@@ -203,7 +211,8 @@ def _register_events(self):
'latitude',
'longitude',
'expiration_timestamp_ms'
)
),
color='green'
)
self.event_manager.register_event(
'pokemon_appeared',
@@ -212,57 +221,65 @@ def _register_events(self):
'cp',
'iv',
'iv_display',
)
),
color='blue'
)
self.event_manager.register_event(
'pokemon_catch_rate',
parameters=(
'catch_rate',
'berry_name',
'berry_count'
)
),
)
self.event_manager.register_event(
'threw_berry',
parameters=(
'berry_name',
'new_catch_rate'
)
),
color='cyan'
)
self.event_manager.register_event(
'threw_pokeball',
parameters=(
'pokeball',
'success_percentage',
'count_left'
)
),
color='cyan'
)
self.event_manager.register_event(
'pokemon_fled',
parameters=('pokemon',)
parameters=('pokemon',),
color='red'
)
self.event_manager.register_event(
'pokemon_vanished',
parameters=('pokemon',)
parameters=('pokemon',),
color='red'
)
self.event_manager.register_event(
'pokemon_caught',
parameters=(
'pokemon',
'cp', 'iv', 'iv_display', 'exp'
)
),
color='green'
)
self.event_manager.register_event(
'pokemon_evolved',
parameters=('pokemon', 'iv', 'cp')
parameters=('pokemon', 'iv', 'cp'),
color='green'
)
self.event_manager.register_event(
'pokemon_evolve_fail',
parameters=('pokemon',)
parameters=('pokemon',),
color='red'
)
self.event_manager.register_event('skip_evolve')
self.event_manager.register_event('threw_berry_failed', parameters=('status_code',))
self.event_manager.register_event('vip_pokemon')
self.event_manager.register_event('threw_berry_failed', parameters=('status_code',), color='red')
self.event_manager.register_event('vip_pokemon', color='red')


# level up stuff
@@ -271,24 +288,27 @@ def _register_events(self):
parameters=(
'previous_level',
'current_level'
)
),
color='cyan'
)
self.event_manager.register_event(
'level_up_reward',
parameters=('items',)
parameters=('items',),
color='cyan'
)

# lucky egg
self.event_manager.register_event(
'used_lucky_egg',
parameters=('amount_left',)
parameters=('amount_left',),
color='cyan'
)
self.event_manager.register_event('lucky_egg_error')
self.event_manager.register_event('lucky_egg_error', color='red')

# softban
self.event_manager.register_event('softban')
self.event_manager.register_event('softban_fix')
self.event_manager.register_event('softban_fix_done')
self.event_manager.register_event('softban', color='red')
self.event_manager.register_event('softban_fix', color='yellow')
self.event_manager.register_event('softban_fix_done', color='green')

# egg incubating
self.event_manager.register_event(
@@ -306,30 +326,33 @@ def _register_events(self):
'next_egg_incubates',
parameters=('distance_in_km',)
)
self.event_manager.register_event('incubator_already_used')
self.event_manager.register_event('egg_already_incubating')
self.event_manager.register_event('incubator_already_used', color='yellow')
self.event_manager.register_event('egg_already_incubating', color='yellow')
self.event_manager.register_event(
'egg_hatched',
parameters=(
'pokemon',
'cp', 'iv', 'exp', 'stardust', 'candy'
)
),
color='blue'
)

# discard item
self.event_manager.register_event(
'item_discarded',
parameters=(
'amount', 'item', 'maximum'
)
),
color='yellow'
)
self.event_manager.register_event(
'item_discard_fail',
parameters=('item',)
parameters=('item',),
color='red'
)

# inventory
self.event_manager.register_event('inventory_full')
self.event_manager.register_event('inventory_full', color='yellow')

# release
self.event_manager.register_event(
@@ -531,6 +554,7 @@ def _setup_logging(self):
level=log_level,
format='%(asctime)s [%(name)10s] [%(levelname)s] %(message)s'
)

def check_session(self, position):
# Check session expiry
if self.api._auth_provider and self.api._auth_provider._ticket_expire:
2 changes: 1 addition & 1 deletion pokemongo_bot/cell_workers/sleep_schedule.py
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ class SleepSchedule(BaseTask):
SCHEDULING_MARGIN = timedelta(minutes=10) # Skip if next sleep is RESCHEDULING_MARGIN from now

def initialize(self):
# self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',))
# self.bot.event_manager.register_event('sleeper_scheduled', parameters=('datetime',), color='cyan')
self._process_config()
self._schedule_next_sleep()

5 changes: 3 additions & 2 deletions pokemongo_bot/event_handlers/logging_handler.py
Original file line number Diff line number Diff line change
@@ -9,10 +9,11 @@

class LoggingHandler(EventHandler):

def handle_event(self, event, sender, level, formatted_msg, data):
def handle_event(self, event, sender, level, formatted_msg, data, **kwargs):
logger = logging.getLogger(type(sender).__name__)
if formatted_msg:
message = "[{}] {}".format(event, formatted_msg)
else:
message = '{}: {}'.format(event, str(data))
getattr(logger, level)(message)
color = kwargs.get('color', 'white')
logger.colorized(level, message, color)
11 changes: 6 additions & 5 deletions pokemongo_bot/event_manager.py
Original file line number Diff line number Diff line change
@@ -26,9 +26,10 @@ def __init__(self, *handlers):
self._handlers = handlers or []

def event_report(self):
for event, parameters in self._registered_events.iteritems():
for event, (color, parameters) in self._registered_events.iteritems():
print '-'*80
print 'Event: {}'.format(event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still this print and the print above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think those were added by me. But sure, I'll remove them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh shit, my bad... don't remove those, here it's the event's report... they should be here

print 'Color: {}'.format(color)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, remove these prints.

if parameters:
print 'Parameters:'
for parameter in parameters:
@@ -37,8 +38,8 @@ def event_report(self):
def add_handler(self, event_handler):
self._handlers.append(event_handler)

def register_event(self, name, parameters=[]):
self._registered_events[name] = parameters
def register_event(self, name, color='white', parameters=[]):
self._registered_events[name] = (color, parameters)

def emit(self, event, sender=None, level='info', formatted='', data={}):
if not sender:
@@ -52,7 +53,7 @@ def emit(self, event, sender=None, level='info', formatted='', data={}):
raise EventNotRegisteredException("Event %s not registered..." % event)

# verify params match event
parameters = self._registered_events[event]
color, parameters = self._registered_events[event]
if parameters:
for k, v in data.iteritems():
if k not in parameters:
@@ -62,4 +63,4 @@ def emit(self, event, sender=None, level='info', formatted='', data={}):

# send off to the handlers
for handler in self._handlers:
handler.handle_event(event, sender, level, formatted_msg, data)
handler.handle_event(event, sender, level, formatted_msg, data, color=color)
Loading