Skip to content

Commit

Permalink
one log handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rawgni committed Sep 6, 2016
1 parent f0df954 commit 2e02cd7
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 149 deletions.
10 changes: 5 additions & 5 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from item_list import Item
from metrics import Metrics
from sleep_schedule import SleepSchedule
from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler, ColoredLoggingHandler, SocialHandler
from pokemongo_bot.event_handlers import SocketIoHandler, LoggingHandler, SocialHandler
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from pokemongo_bot.websocket_remote_control import WebsocketRemoteControl
from pokemongo_bot.base_dir import _base_dir
Expand Down Expand Up @@ -145,10 +145,10 @@ def start(self):
def _setup_event_system(self):
handlers = []

if self.config.logging and 'color' in self.config.logging and self.config.logging['color']:
handlers.append(ColoredLoggingHandler(self))
else:
handlers.append(LoggingHandler(self))
color = self.config.logging and 'color' in self.config.logging and self.config.logging['color']
debug = self.config.debug

handlers.append(LoggingHandler(color, debug))

if self.config.enable_social:
handlers.append(SocialHandler(self))
Expand Down
1 change: 0 additions & 1 deletion pokemongo_bot/event_handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from logging_handler import LoggingHandler
from socketio_handler import SocketIoHandler
from colored_logging_handler import ColoredLoggingHandler
from social_handler import SocialHandler
from telegram_handler import TelegramHandler
138 changes: 0 additions & 138 deletions pokemongo_bot/event_handlers/colored_logging_handler.py

This file was deleted.

127 changes: 122 additions & 5 deletions pokemongo_bot/event_handlers/logging_handler.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,139 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals


import logging

from pokemongo_bot.event_manager import EventHandler


class LoggingHandler(EventHandler):
EVENT_COLOR_MAP = {
'api_error': 'red',
'badges': 'blue',
'bot_exit': 'red',
'bot_start': 'green',
'catch_limit': 'red',
'catch_log': 'magenta',
'config_error': 'red',
'egg_already_incubating': 'yellow',
'egg_hatched': 'green',
'egg_hatched_fail': 'red',
'eggs_hatched_log': 'magenta',
'evolve_log': 'magenta',
'future_pokemon_release': 'yellow',
'incubate': 'green',
'incubator_already_used': 'yellow',
'inventory_full': 'yellow',
'item_discard_fail': 'red',
'item_discarded': 'green',
'next_force_recycle': 'green',
'force_recycle': 'green',
'keep_best_release': 'green',
'level_up': 'green',
'level_up_reward': 'green',
'location_cache_error': 'yellow',
'location_cache_ignored': 'yellow',
'login_failed': 'red',
'login_log': 'magenta',
'login_successful': 'green',
'log_stats': 'magenta',
'lucky_egg_error': 'red',
'move_to_map_pokemon_encounter': 'green',
'move_to_map_pokemon_fail': 'red',
'next_egg_incubates': 'yellow',
'next_sleep': 'green',
'next_random_pause': 'green',
'next_random_alive_pause': 'green',
'no_pokeballs': 'red',
'path_lap_end': 'green',
'pokemon_appeared': 'yellow',
'pokemon_capture_failed': 'red',
'pokemon_caught': 'blue',
'pokemon_evolved': 'green',
'pokemon_fled': 'red',
'pokemon_inventory_full': 'red',
'pokemon_nickname_invalid': 'red',
'pokemon_not_in_range': 'yellow',
'pokemon_release': 'green',
'pokemon_upgraded': 'green',
'pokemon_vanished': 'red',
'pokestop_empty': 'yellow',
'pokestop_log': 'magenta',
'pokestop_searching_too_often': 'yellow',
'rename_pokemon': 'green',
'show_best_pokemon': 'magenta',
'show_inventory': 'magenta',
'skip_evolve': 'yellow',
'softban': 'red',
'softban_log': 'magenta',
'spin_limit': 'red',
'spun_pokestop': 'cyan',
'threw_berry_failed': 'red',
'transfer_log': 'magenta',
'unknown_spin_result': 'red',
'unset_pokemon_nickname': 'red',
'vip_pokemon': 'red',
'use_incense': 'blue',
'vanish_limit_reached': 'red',

'arrived_at_cluster': 'none',
'arrived_at_fort': 'none',
'bot_sleep': 'none',
'bot_random_pause': 'none',
'bot_random_alive_pause': 'none',
'catchable_pokemon': 'none',
'found_cluster': 'none',
'incubate_try': 'none',
'load_cached_location': 'none',
'location_found': 'none',
'login_started': 'none',
'lured_pokemon_found': 'none',
'move_to_map_pokemon_move_towards': 'none',
'move_to_map_pokemon_teleport_back': 'none',
'move_to_map_pokemon_updated_map': 'none',
'moving_to_fort': 'none',
'moving_to_lured_fort': 'none',
'pokemon_catch_rate': 'none',
'pokemon_evolve_fail': 'none',
'pokestop_on_cooldown': 'none',
'pokestop_out_of_range': 'none',
'polyline_request': 'none',
'position_update': 'none',
'path_lap_update': 'none',
'set_start_location': 'none',
'softban_fix': 'none',
'softban_fix_done': 'none',
'spun_fort': 'none',
'threw_berry': 'none',
'threw_pokeball': 'none',
'used_lucky_egg': 'none'
}
COLOR_CODE = {
'gray': '\033[90m',
'red': '\033[91m',
'green': '\033[92m',
'yellow': '\033[93m',
'blue': '\033[94m',
'magenta': '\033[95m',
'cyan': '\033[96m',
'white': '\033[97m',
'none': '\033[0m'
}

def __init__(self, bot):
self.bot = bot
def __init__(self, color=True, debug=False):
self.color = color
self.debug = debug

def handle_event(self, event, sender, level, formatted_msg, data):
if not formatted_msg:
formatted_msg = str(data)
if self.bot.config.debug:

if self.color and event in self.EVENT_COLOR_MAP:
color = self.COLOR_CODE[self.EVENT_COLOR_MAP[event]]
formatted_msg = '{}{}{}'.format(color, formatted_msg, self.COLOR_CODE['none'])

if self.debug:
formatted_msg = '[{}] {}'.format(event, formatted_msg)

logger = logging.getLogger(type(sender).__name__)
getattr(logger, level)(formatted_msg)
getattr(logger, level)(formatted_msg.encode(sys.stdout.encoding, "replace").decode("utf-8"))

0 comments on commit 2e02cd7

Please sign in to comment.