Skip to content

Commit

Permalink
setup_logging before starting bot (#5355)
Browse files Browse the repository at this point in the history
* setup_logging before starting bot

* cleanup unused import
  • Loading branch information
rawgni authored and solderzzc committed Sep 10, 2016
1 parent 3bda6a9 commit f02ce43
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
36 changes: 36 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import string
import subprocess

from logging import Formatter

codecs.register(lambda name: codecs.lookup("utf-8") if name == "cp65001" else None)

from getpass import getpass
Expand Down Expand Up @@ -106,6 +108,38 @@ def initialize(config):

return bot

def setup_logging(config):
log_level = logging.ERROR

if config.debug:
log_level = logging.DEBUG

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 config.logging:
logging_format = '%(message)s'
logging_format_options = ''

if ('show_log_level' not in config.logging) or config.logging['show_log_level']:
logging_format = '[%(levelname)s] ' + logging_format
if ('show_process_name' not in config.logging) or config.logging['show_process_name']:
logging_format = '[%(name)10s] ' + logging_format
if ('show_thread_name' not in config.logging) or config.logging['show_thread_name']:
logging_format = '[%(threadName)s] ' + logging_format
if ('show_datetime' not in config.logging) or config.logging['show_datetime']:
logging_format = '[%(asctime)s] ' + logging_format
logging_format_options = '%Y-%m-%d %H:%M:%S'

formatter = Formatter(logging_format,logging_format_options)
for handler in logging.root.handlers[:]:
handler.setFormatter(formatter)

def start_bot(bot, config):
bot.start()
initialize_task(bot, config)
Expand Down Expand Up @@ -144,6 +178,8 @@ def get_commit_hash():
health_record = BotEvent(config)
health_record.login_success()

setup_logging(config)

finished = False

while not finished:
Expand Down
36 changes: 1 addition & 35 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import threading
import shelve
import uuid
from logging import Formatter

from geopy.geocoders import GoogleV3
from pgoapi import PGoApi
Expand Down Expand Up @@ -134,7 +133,6 @@ def __init__(self, db, config):

def start(self):
self._setup_event_system()
self._setup_logging()
self.sleep_schedule = SleepSchedule(self, self.config.sleep_schedule) if self.config.sleep_schedule else None
if self.sleep_schedule:
self.sleep_schedule.work()
Expand Down Expand Up @@ -815,38 +813,6 @@ def find_close_cells(self, lat, lng):
)
return map_cells

def _setup_logging(self):
log_level = logging.ERROR

if self.config.debug:
log_level = logging.DEBUG

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:
logging_format = '%(message)s'
logging_format_options = ''

if ('show_log_level' not in self.config.logging) or self.config.logging['show_log_level']:
logging_format = '[%(levelname)s] ' + logging_format
if ('show_process_name' not in self.config.logging) or self.config.logging['show_process_name']:
logging_format = '[%(name)10s] ' + logging_format
if ('show_thread_name' not in self.config.logging) or self.config.logging['show_thread_name']:
logging_format = '[%(threadName)s] ' + logging_format
if ('show_datetime' not in self.config.logging) or self.config.logging['show_datetime']:
logging_format = '[%(asctime)s] ' + logging_format
logging_format_options = '%Y-%m-%d %H:%M:%S'

formatter = Formatter(logging_format,logging_format_options)
for handler in logging.root.handlers[:]:
handler.setFormatter(formatter)

def check_session(self, position):

# Check session expiry
Expand Down Expand Up @@ -1459,4 +1425,4 @@ def _refresh_inventory(self):
inventory.refresh_inventory()
self.last_inventory_refresh = now
self.inventory_refresh_counter += 1


0 comments on commit f02ce43

Please sign in to comment.