Skip to content

Commit

Permalink
Clean logging option (#3706)
Browse files Browse the repository at this point in the history
* Add clean logging functionality

* Change example configs to include nested logging configurations
  • Loading branch information
extink authored and mjmadsen committed Aug 27, 2016
1 parent b7be689 commit 566a3f7
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 94 deletions.
5 changes: 4 additions & 1 deletion configs/config.json.cluster.example
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
"logging_color": true,
"logging": {
"color": true,
"clean": false
},
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or"},
"// Example of always catching Rattata:": {},
Expand Down
5 changes: 4 additions & 1 deletion configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
"logging_color": true,
"logging": {
"color": true,
"clean": false
},
"catch": {
"any": {"candy_threshold" : 400 ,"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or"},
"// Example of always catching Rattata:": {},
Expand Down
5 changes: 4 additions & 1 deletion configs/config.json.map.example
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
"logging_color": true,
"logging": {
"color": true,
"clean": false
},
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or"},
"// Example of always catching Rattata:": {},
Expand Down
5 changes: 4 additions & 1 deletion configs/config.json.optimizer.example
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
"logging_color": true,
"logging": {
"color": true,
"clean": false
},
"catch": {
"any": {
"always_catch": true
Expand Down
5 changes: 4 additions & 1 deletion configs/config.json.path.example
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
"logging_color": true,
"logging": {
"color": true,
"clean": false
},
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or"},
"// Example of always catching Rattata:": {},
Expand Down
5 changes: 4 additions & 1 deletion configs/config.json.pokemon.example
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
"logging_color": true,
"logging": {
"color": true,
"clean": false
},
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or" },

Expand Down
11 changes: 9 additions & 2 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,19 @@ def _json_loader(filename):
add_config(
parser,
load,
long_flag="--logging_color",
long_flag="--logging.color",
help="If logging_color is set to true, colorized logging handler will be used",
type=bool,
default=True
)
add_config(
parser,
load,
long_flag="--logging.clean",
help="If clean_logging is set to true, meta data will be stripped from the log messages",
type=bool,
default=False
)
add_config(
parser,
load,
Expand Down Expand Up @@ -605,7 +613,6 @@ def _json_loader(filename):
type=float,
default=8.0
)

add_config(
parser,
load,
Expand Down
46 changes: 21 additions & 25 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import threading
import shelve
import uuid
from logging import Formatter

from geopy.geocoders import GoogleV3
from pgoapi import PGoApi
Expand Down Expand Up @@ -130,12 +131,15 @@ def start(self):

def _setup_event_system(self):
handlers = []

if self.config.logging_color:
handlers.append(ColoredLoggingHandler())
handlers.append(ColoredLoggingHandler(self))
else:
handlers.append(LoggingHandler())
handlers.append(LoggingHandler(self))

if self.config.enable_social:
handlers.append(SocialHandler(self))

if self.config.websocket_server_url:
if self.config.websocket_start_embedded_server:
self.sio_runner = SocketIoRunner(self.config.websocket_server_url)
Expand Down Expand Up @@ -743,32 +747,24 @@ def find_close_cells(self, lat, lng):
return map_cells

def _setup_logging(self):
# log settings
# log format
log_level = logging.ERROR

if self.config.debug:
log_level = logging.DEBUG
logging.getLogger("requests").setLevel(logging.DEBUG)
logging.getLogger("websocket").setLevel(logging.DEBUG)
logging.getLogger("socketio").setLevel(logging.DEBUG)
logging.getLogger("engineio").setLevel(logging.DEBUG)
logging.getLogger("socketIO-client").setLevel(logging.DEBUG)
logging.getLogger("pgoapi").setLevel(logging.DEBUG)
logging.getLogger("rpc_api").setLevel(logging.DEBUG)
else:
log_level = logging.ERROR
logging.getLogger("requests").setLevel(logging.ERROR)
logging.getLogger("websocket").setLevel(logging.ERROR)
logging.getLogger("socketio").setLevel(logging.ERROR)
logging.getLogger("engineio").setLevel(logging.ERROR)
logging.getLogger("socketIO-client").setLevel(logging.ERROR)
logging.getLogger("pgoapi").setLevel(logging.ERROR)
logging.getLogger("rpc_api").setLevel(logging.ERROR)

logging.basicConfig(
level=log_level,
format='%(asctime)s [%(name)10s] [%(levelname)s] %(message)s'
)

logging.getLogger("requests").setLevel(log_level)
logging.getLogger("websocket").setLevel(log_level)
logging.getLogger("socketio").setLevel(log_level)
logging.getLogger("engineio").setLevel(log_level)
logging.getLogger("socketIO-client").setLevel(log_level)
logging.getLogger("pgoapi").setLevel(log_level)
logging.getLogger("rpc_api").setLevel(log_level)

if self.config.logging_clean and not self.config.debug:
formatter = Formatter(fmt='[%(asctime)s] %(message)s', datefmt='%H:%M:%S')
for handler in logging.root.handlers[:]:
handler.setFormatter(formatter)

def check_session(self, position):

# Check session expiry
Expand Down
108 changes: 52 additions & 56 deletions pokemongo_bot/event_handlers/colored_logging_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class ColoredLoggingHandler(EventHandler):
EVENT_COLOR_MAP = {
'api_error': 'red',
'badges': 'blue',
'bot_exit': 'red',
'bot_start': 'green',
'catch_limit': 'red',
Expand All @@ -33,6 +34,7 @@ class ColoredLoggingHandler(EventHandler):
'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',
Expand All @@ -41,6 +43,7 @@ class ColoredLoggingHandler(EventHandler):
'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',
Expand All @@ -52,63 +55,53 @@ class ColoredLoggingHandler(EventHandler):
'pokemon_release': 'green',
'pokemon_vanished': 'red',
'pokestop_empty': 'yellow',
'pokestop_log': 'magento',
'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',
'spun_pokestop': 'cyan',
'threw_berry_failed': 'red',
'transfer_log': 'magenta',
'unknown_spin_result': 'red',
'unset_pokemon_nickname': 'red',
'vip_pokemon': 'red',
'path_lap_end': 'green',
'log_stats': 'magenta',
'show_inventory': 'magenta',
'show_best_pokemon': 'magenta',
'badges': 'blue',

# event names for 'white' still here to remember that these events are already determined its color.
'arrived_at_cluster': 'white',
'arrived_at_fort': 'white',
'bot_sleep': 'white',
'bot_random_pause': 'white',
'bot_random_alive_pause': 'white',
'catchable_pokemon': 'white',
'found_cluster': 'white',
'incubate_try': 'white',
'load_cached_location': 'white',
'location_found': 'white',
'login_started': 'white',
'lured_pokemon_found': 'white',
'move_to_map_pokemon_move_towards': 'white',
'move_to_map_pokemon_teleport_back': 'white',
'move_to_map_pokemon_updated_map': 'white',
'moving_to_fort': 'white',
'moving_to_lured_fort': 'white',
'pokemon_catch_rate': 'white',
'pokemon_evolve_fail': 'white',
'pokestop_on_cooldown': 'white',
'pokestop_out_of_range': 'white',
'polyline_request': 'white',
'position_update': 'white',
'path_lap_update': 'white',
'set_start_location': 'white',
'softban_fix': 'white',
'softban_log': 'magenta',
'softban_fix_done': 'white',
'spun_fort': 'white',
'threw_berry': 'white',
'threw_pokeball': 'white',
'used_lucky_egg': 'white',
'player_data': 'white'
'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'
}
CONTINUOUS_EVENT_NAMES = [
'catchable_pokemon',
'moving_to_lured_fort',
'spun_fort'
]
COLOR_CODE = {
'gray': '\033[90m',
'red': '\033[91m',
Expand All @@ -118,21 +111,24 @@ class ColoredLoggingHandler(EventHandler):
'magenta': '\033[95m',
'cyan': '\033[96m',
'white': '\033[97m',
'reset': '\033[0m'
'none': '\033[0m'
}

def __init__(self, bot):
self.bot = bot

def handle_event(self, event, sender, level, formatted_msg, data):
logger = logging.getLogger(type(sender).__name__)
if not formatted_msg:
formatted_msg = str(data)

color = self.COLOR_CODE['white']
if event in self.EVENT_COLOR_MAP:
color = self.COLOR_CODE[self.EVENT_COLOR_MAP[event]]
if event == 'egg_hatched' and data.get('pokemon', 'error') == 'error':
color = self.COLOR_CODE['red']
formatted_msg = '{}{}{}'.format(color, formatted_msg, self.COLOR_CODE['reset'])
if event == 'egg_hatched' and data.get('pokemon', 'error') == 'error':
color = self.COLOR_CODE['red']
formatted_msg = '{}{}{}'.format(color, formatted_msg, self.COLOR_CODE['none'])

if self.bot.config.debug or not self.bot.config.logging_clean:
formatted_msg = '[{}] {}'.format(event, formatted_msg)

if formatted_msg:
message = "[{}] {}".format(event, formatted_msg)
else:
message = '{}: {}'.format(event, str(data))
getattr(logger, level)(message)
logger = logging.getLogger(type(sender).__name__)
getattr(logger, level)(formatted_msg)
14 changes: 9 additions & 5 deletions pokemongo_bot/event_handlers/logging_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

class LoggingHandler(EventHandler):

def __init__(self, bot):
self.bot = bot

def handle_event(self, event, sender, level, formatted_msg, data):
if not formatted_msg:
formatted_msg = str(data)
if self.bot.config.debug or not self.bot.config.logging_clean:
formatted_msg = '[{}] {}'.format(event, formatted_msg)

logger = logging.getLogger(type(sender).__name__)
if formatted_msg:
message = "[{}] {}".format(event, formatted_msg)
else:
message = '{}: {}'.format(event, str(data))
getattr(logger, level)(message)
getattr(logger, level)(formatted_msg)

0 comments on commit 566a3f7

Please sign in to comment.