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

Colorized logging #2604

wants to merge 25 commits into from

Conversation

gurupras
Copy link
Contributor

@gurupras gurupras commented Aug 4, 2016

Short Description: Updates to the (updated) logging system

Fixes:

  • Adds lost colour to the event system
  • Adds new methods to loggers

Some of the colours may not be the same as they were in the earlier logger.log system. I can change these if you're very particular. Some of the LCD code has also crept in. I can get rid of these if they're no longer required.

The commit in itself may have some redundant code in terms of functions that can be used like:
self.logger.red('message in red'), ...
I thought these might be useful for cases where a message doesn't necessarily fit into the usual logging levels like CRITICAL, ERROR, WARNING, INFO, DEBUG

Added new logging levels and functions
Conflicts:
	pokemongo_bot/api_wrapper.py
	pokemongo_bot/cell_workers/catch_lured_pokemon.py
	pokemongo_bot/cell_workers/catch_visible_pokemon.py
	pokemongo_bot/cell_workers/collect_level_up_reward.py
	pokemongo_bot/cell_workers/evolve_pokemon.py
	pokemongo_bot/cell_workers/handle_soft_ban.py
	pokemongo_bot/cell_workers/incubate_eggs.py
	pokemongo_bot/cell_workers/move_to_fort.py
	pokemongo_bot/cell_workers/nickname_pokemon.py
	pokemongo_bot/cell_workers/pokemon_catch_worker.py
	pokemongo_bot/cell_workers/recycle_items.py
	pokemongo_bot/cell_workers/sleep_schedule.py
	pokemongo_bot/cell_workers/spin_fort.py
	pokemongo_bot/cell_workers/transfer_pokemon.py
	pokemongo_bot/health_record/bot_event.py
	pokemongo_bot/logger.py

 Changes to be committed:
	modified:   pokecli.py
	modified:   pokemongo_bot/__init__.py
	modified:   pokemongo_bot/cell_workers/sleep_schedule.py
	modified:   pokemongo_bot/event_handlers/logging_handler.py
	modified:   pokemongo_bot/event_manager.py
	modified:   pokemongo_bot/logger.py
@douglascamata
Copy link
Member

@gurupras please, remember that this colors cannot be logged in files (and there will be options for the user to log every task, or a specific task, to a file), so we need to handle this.

@gurupras
Copy link
Contributor Author

gurupras commented Aug 5, 2016

I see. I may have missed this, but all the code I found was only invoking logging.basicConfig without passing in a file path. Regardless, if I understood the design correctly, this is trivial to support if it involves a new handler akin to socketio_handler.py and logging_handler.py

@gurupras
Copy link
Contributor Author

gurupras commented Aug 5, 2016

Oh, my bad. You're talking about getattr(logger, level)(message). We can still handle this while still providing colorized output on the console as long as the color formatter is not attached to the file handler.

@ghost
Copy link

ghost commented Aug 5, 2016

input option -V[erbose] -c[olor]

@raulgbcr
Copy link
Contributor

raulgbcr commented Aug 7, 2016

This NEEDS to be implemented, actual gui is shit.

@gurupras
Copy link
Contributor Author

gurupras commented Aug 7, 2016

Well, not necessarily.
Actual GUI might be nice to have..especially if we can incorporate a map
and stuff.

However, you're right. I would still like colorized logging available
regardless of GUI.
You just immediately know to focus on what's important and ignore what's
not.

Let me know if this requires any additional work, I'll be happy to get it
done.

On Sun, Aug 7, 2016 at 6:00 AM, raulgbcr notifications@github.com wrote:

This NEEDS to be implemented, actual gui is shit.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#2604 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACzB14kUjgz5x2ujSYgm2LibsP_9iEJKks5qdazVgaJpZM4JdIdY
.

@douglascamata
Copy link
Member

@raulgbcr if by actual GUI you meant the terminal, it's NOT supposed to be user friendly from the start. It's not a GUI, it's an application with logs. We have no GUI (:

@douglascamata
Copy link
Member

douglascamata commented Aug 7, 2016

👎

(see my comments)

@TheSavior please review this

print '-'*80
print 'Event: {}'.format(event)
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.

@@ -29,7 +29,6 @@ def event_report(self):
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

@douglascamata
Copy link
Member

@gurupras please put the event name print back, I forgot that code was inside the event report function. It should indeed be there. My bad.

@gurupras
Copy link
Contributor Author

gurupras commented Aug 7, 2016

But you don't want the port print? or you want both back

@douglascamata
Copy link
Member

douglascamata commented Aug 7, 2016

@gurupras port? which port? colour I meant? Color is not necessary. The event report is just to help web developers create new UIs

@gurupras
Copy link
Contributor Author

gurupras commented Aug 7, 2016

oops..sorry not port..it was:

        print '-'*80
        print 'Event: {}'.format(event)

I quickly saw that 80 and for some reason commented thinking it was port

@BartKoppelmans
Copy link
Contributor

How about using different types of logging functions (e.g. error, pokémon, item) to allow for more extensibility? If we would use one logging class, we could give it multiple functions to call. Then the function could use its own color and settings (possibly shared across the class). This would make it easier to add options in the future, for example to allow setting the color or to disable certain logging.

A con is that we would have to specify a type in the code.

@douglascamata: How much work would it be to have the events use different functions, according to a given event? And what do you thing about that kind of system?

@elicwhite
Copy link
Contributor

@BartKoppelmans When you receive an event, you know what type of event it is. Anything you could do with multiple functions you could do inside of the event handler.

@BartKoppelmans
Copy link
Contributor

@TheSavior: Nice, so it would be possible to implement this? What do you think about it then?

@douglascamata
Copy link
Member

@douglascamata: How much work would it be to have the events use different functions, according to a given event? And what do you thing about that kind of system?

We want to avoid duplicating code and be as simple as possible for the requirements we have. Why would we need multiple functions? What is so special that cannot be handled dynamically in just one function?

@douglascamata
Copy link
Member

@douglascamata What if the web ui wants to start doing special things for given events. Should those be passed in the initial data too and thus sent to all listeners? The idea is that you'd be keeping handler specific functionality inside the handler instead of sending unnecessary data around.

That is not in our requirements, at least yet. So don't need to bother thinking about it because we don't know if that will ever happen (and personally, I don't think it will).

@BartKoppelmans
Copy link
Contributor

@douglascamata

We want to avoid duplicating code and be as simple as possible for the requirements we have. Why would we need multiple functions? What is so special that cannot be handled dynamically in just one function?

This would enable us to have different functions for each type of log, and thus allow us to have separate settings for these functions. We could say no to logging the incubator and yes to logging the rest, while keeping the caught pokémon green and the transfers red. This would make it easier to extend, without having to change the old code. The common code can still be written in one function, which is called to reduce redundancy.

The biggest plus is the extensibility, as it would make it easier to add settings to the functions. Doing this dynamically would result in one method full of complex nesting and if-statements.

@gurupras
Copy link
Contributor Author

gurupras commented Aug 8, 2016

The biggest plus is the extensibility, as it would make it easier to add settings to the functions. Doing this dynamically would result in one method full of complex nesting and if-statements.

Or a json array of events that we want to log

@douglascamata
Copy link
Member

douglascamata commented Aug 8, 2016

@BartKoppelmans @gurupras I already have plans for this and it will keep everything as dynamic as it is right now. There will be configs in every task inheriting from BaseTask to customize log level, format and destination (file or terminal). No nasty code involved.

@BartKoppelmans
Copy link
Contributor

@douglascamata I'm curious, share it as soon as you start! 😄

@douglascamata
Copy link
Member

@BartKoppelmans I will when I do it, but i'm working on more important stuff and I'm a bit overloaded.

@douglascamata
Copy link
Member

@BartKoppelmans btw, can you solve merge conflicts, please?

@BartKoppelmans
Copy link
Contributor

@douglascamata Sure, let me know if I can help with some things! And I'm sorry, but this is not my PR, otherwise I would!😅

Pinging @gurupras to resolve the merge conflicts.

@BartKoppelmans
Copy link
Contributor

Great job, thank you very much! @gurupras

Conflicts:
	pokemongo_bot/__init__.py
    - Cleaned up code
    - No more initialization upon `import logging`
    - The event system's `LoggingHandler` class now initializes logging framework
    - Early console is handled by `logging.basicConfig(...)`
There were some events where the initialization ended with a
trailing comma. A color was _supposed_ to be added here but wasn't
due to not falling clearly under one of the color categories.
… class

In python __init__ __is__ the initialization method. So use that instead.
…zed-logging

Conflicts:
	pokemongo_bot/__init__.py
@DanielVolz
Copy link

can someone help me, getting this errors on OSX with iterm app.

Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit msg = self.format(record) File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format return fmt.format(record) File "/Users/danielvolz/MyPogoHeroku/PokemonGo-Bot/pokemongo_bot/logger.py", line 55, in format record.msg = '{color}{message}{reset}'.format(color=ColorizedLogFormatter.COLOR_SEQ % (color), message=record.msg, reset=ColorizedLogFormatter.RESET_SEQ) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 48: ordinal not in range(128) Logged from file logger.py, line 86 22:59:13 [RecycleItems] [Level info] [item_discarded] Discarded 1x Pokeball (maximum 100). 22:59:14 [RecycleItems] [Level info] [item_discarded] Discarded 1x Super Potion (maximum 0). Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit msg = self.format(record) File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format return fmt.format(record) File "/Users/danielvolz/MyPogoHeroku/PokemonGo-Bot/pokemongo_bot/logger.py", line 55, in format record.msg = '{color}{message}{reset}'.format(color=ColorizedLogFormatter.COLOR_SEQ % (color), message=record.msg, reset=ColorizedLogFormatter.RESET_SEQ) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 48: ordinal not in range(128) Logged from file logger.py, line 86 Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit msg = self.format(record) File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format return fmt.format(record) File "/Users/danielvolz/MyPogoHeroku/PokemonGo-Bot/pokemongo_bot/logger.py", line 55, in format record.msg = '{color}{message}{reset}'.format(color=ColorizedLogFormatter.COLOR_SEQ % (color), message=record.msg, reset=ColorizedLogFormatter.RESET_SEQ) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 48: ordinal not in range(128) Logged from file logger.py, line 86 Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit msg = self.format(record) File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format return fmt.format(record) File "/Users/danielvolz/MyPogoHeroku/PokemonGo-Bot/pokemongo_bot/logger.py", line 55, in format record.msg = '{color}{message}{reset}'.format(color=ColorizedLogFormatter.COLOR_SEQ % (color), message=record.msg, reset=ColorizedLogFormatter.RESET_SEQ) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 48: ordinal not in range(128) Logged from file logger.py, line 86 Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 861, in emit msg = self.format(record) File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 734, in format return fmt.format(record) File "/Users/danielvolz/MyPogoHeroku/PokemonGo-Bot/pokemongo_bot/logger.py", line 55, in format record.msg = '{color}{message}{reset}'.format(color=ColorizedLogFormatter.COLOR_SEQ % (color), message=record.msg, reset=ColorizedLogFormatter.RESET_SEQ) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 48: ordinal not in range(128) Logged from file logger.py, line 86

@gurupras
Copy link
Contributor Author

gurupras commented Aug 9, 2016

Oh boy.
I don't have access to a Mac. Could someone who uses OSX take a look at this and suggest a fixup?

@douglascamata
Copy link
Member

@gurupras logger.py needs to import unicode_literals from future, probably.

@DanielVolz
Copy link

DanielVolz commented Aug 9, 2016

trying now with the new commit... looks good right now
edit: it works without the previous error now! Thank! @douglascamata @gurupras

@douglascamata
Copy link
Member

douglascamata commented Aug 10, 2016

@gurupras I'm sorry, but #3198 had a much more modular structure for the same features and it was merged :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants