ashb commented on issue #5681: [AIRFLOW-5065] Add colors to console log
URL: https://github.com/apache/airflow/pull/5681#issuecomment-516370691
 
 
   `coloredlogs` doesn't look like it gives the flexability we want (the 
different colours for each of the parts is _really_ nice) but 
https://github.com/borntyping/python-colorlog (`pip install colorlog`) is close:
   
   ```python
   from colorlog import ColoredFormatter
   import logging
   
   logging.basicConfig(level=logging.INFO)
   
   log = logging.getLogger(__name__)
   
   log_format = '[%(blue)s%(asctime)s%(reset)s] 
{%(blue)s%(filename)s%(reset)s:%(lineno)d} %(log_color)s%(levelname)s%(reset)s 
- %(message_log_color)s%(message)s'
   formatter = ColoredFormatter(
       log_format,
       log_colors={
           'WARNING': 'yellow',
           'ERROR': 'red',
           'CRITICAL': 'red,bold',
       },
       secondary_log_colors={
           'message': {
               'INFO': 'bold',
           }
       },
   )
   
   logging.root.handlers[0].setFormatter(formatter)
   
   log.info("Usiung connection to: %s", {'id': 'proxy_postgres_tcp'})
   log.warning("Hi")
   log.error("Hi")
   
   try:
       raise RuntimeError("x")
   except Exception:
       log.critical("err")
   ```
   
   Looks like:
   
   ![Screenshot 2019-07-30 at 11 56 
19](https://user-images.githubusercontent.com/34150/62123993-1149eb00-b2c1-11e9-8ca2-0777ae63f58e.png)
   
   And it automatically detects when in a TTY. _But_ it may not be written in a 
way that lets us get the colours we want.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to