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
Add support for colorized logging output #197
Comments
This might be helpful: |
An example (slightly hackish) alternative logger with color: import logging
from colorlog import ColoredFormatter
from cement.core import handler
from cement.utils.misc import is_true
from cement.ext.ext_logging import LoggingLogHandler, NullHandler
from cement.core.controller import expose, CementBaseController
class ColorLogController(CementBaseController):
class Meta:
label = 'color_log'
arguments = [
(['--no-color'],
dict(help="disable colorized output", action='store_true')),
]
class ColorLogHandler(LoggingLogHandler):
class Meta:
label = "color_log"
#: The logging format for the consoler logger.
console_format = "%(log_color)s%(levelname)s: %(message)s"
#: The logging format for both file and console if ``debug==True``.
debug_format = "%(log_color)s%(asctime)s (%(levelname)s) %(namespace)s : " + \
"%(message)s"
def _setup_console_log(self):
# Otherwise
to_console = self.app.config.get(self._meta.config_section,
'to_console')
if is_true(to_console):
console_handler = logging.StreamHandler()
if self.get_level() == logging.getLevelName(logging.DEBUG):
the_format = self._meta.debug_format
else:
the_format = self._meta.console_format
formatter = ColoredFormatter(the_format,
log_colors={
'DEBUG': 'white',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})
console_handler.setFormatter(formatter)
console_handler.setLevel(getattr(logging, self.get_level()))
else:
console_handler = NullHandler()
self.backend.addHandler(console_handler)
def load():
handler.register(ColorLogHandler)
handler.register(ColorLogController) |
|
@spinus are you suggested in And in Thanks. |
|
Yeah, that Rainbow Logging Handler looks pretty much perfect, and seems to have been around for a while. I'll look at doing both (the above doesn't require additional dependencies so it can go in core). |
This was committed to cement/master based on from cement.core.foundation import CementApp
class MyApp(CementApp):
class Meta:
label = 'myapp'
extensions = ['colorlog']
log_handler = 'colorlog'
with MyApp() as app:
app.run()
app.log.info('My Info Test')
app.log.error('My Error Test')
app.log.fatal('My Fatal Test')
app.log.warn('My Warn Test')
app.log.debug('My Debug Test') |
In apps that heavily rely on logging to the console, having colorized output is very useful. For example:
The text was updated successfully, but these errors were encountered: