Giter Site home page Giter Site logo

Comments (7)

borntyping avatar borntyping commented on August 16, 2024 1

Ah, I think I've broken anything that uses multiple colors (e.g. red,bg_white).

from python-colorlog.

aress31 avatar aress31 commented on August 16, 2024

Has any progress been made regarding that feature request?

from python-colorlog.

borntyping avatar borntyping commented on August 16, 2024

I think this should be doable with the existing logging library (see addLevelName()), and then including that level in the log_colors argument to a ColoredFormatter.

logging.addLevelName(1, 'TRACE')

...

formatter = ColoredFormatter(
	"%(log_color)s%(levelname)-8s%(reset)s %(blue)s%(message)s",
	log_colors={
		'TRACE':    'magenta',
		'DEBUG':    'cyan',
		'INFO':     'green',
		'WARNING':  'yellow',
		'ERROR':    'red',
		'CRITICAL': 'red,bg_white',
	}
)

from python-colorlog.

aress31 avatar aress31 commented on August 16, 2024

@borntyping thanks for your quick reply, if doable, could you please add a working example into the README.

Many thanks,
Alex

from python-colorlog.

borntyping avatar borntyping commented on August 16, 2024

https://github.com/borntyping/python-colorlog#with-custom-log-levels 😄

from python-colorlog.

aress31 avatar aress31 commented on August 16, 2024

@borntyping I have been trying this way of achieving custom colored level, but I am encountering an issue. I believe that this issue is related to the background color which does not behave very well.

This is the code I am using:

import colorlog, logging

SUCCESS = 21

logging.addLevelName(SUCCESS, "SUCCESS")

def success(self, message, *args, **kws):
    if self.isEnabledFor(SUCCESS):
        self._log(SUCCESS, message, args, **kws) 

logging.Logger.success = success

FAILURE = 22

logging.addLevelName(FAILURE, "FAILURE")

def failure(self, message, *args, **kws):
    if self.isEnabledFor(FAILURE):
        self._log(FAILURE, message, args, **kws) 

logging.Logger.failure = failure

formatter = colorlog.ColoredFormatter(
    "%(log_color)s[%(asctime)s] [%(levelname)s] %(message)s%(reset)s",
    datefmt = "%H:%M:%S",
    log_colors = {
        'DEBUG':    'cyan',
        'INFO':     'green',
        'SUCCESS':  'red',
        'FAILURE':  'blue',
        'WARNING':  'yellow',
        'ERROR':    'red',
        'CRITICAL': 'red, bg_white'
    },
    reset = True
)

handler = logging.StreamHandler()
handler.setFormatter(formatter)

logger = logging.getLogger("Colorlog Example")
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

logger.debug("DEBUG")
logger.info("INFO")
logger.success("SUCCES")
logger.failure("FAILURE")
logger.warning("WARNING")
logger.error("ERROR")
logger.critical("CRITICAL")

This is the exception been thrown:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 987, in emit
    msg = self.format(record)
  File "C:\Program Files\Python36\lib\logging\__init__.py", line 833, in format
    return fmt.format(record)
  File "C:\Program Files\Python36\lib\site-packages\colorlog\colorlog.py", line 119, in format
    record.log_color = self.color(self.log_colors, record.levelname)
  File "C:\Program Files\Python36\lib\site-packages\colorlog\colorlog.py", line 114, in color
    return parse_colors(log_colors.get(level_name, ""))
  File "C:\Program Files\Python36\lib\site-packages\colorlog\escape_codes.py", line 60, in parse_colors
    return ''.join(escape_codes[n] for n in sequence.split(',') if n)
  File "C:\Program Files\Python36\lib\site-packages\colorlog\escape_codes.py", line 60, in <genexpr>
    return ''.join(escape_codes[n] for n in sequence.split(',') if n)
KeyError: ' bg_white'
Call stack:
  File ".\test.py", line 67, in <module>
    logger.critical("CRITICAL")
Message: 'CRITICAL'
Arguments: ()

The corresponding screenshot:
image

from python-colorlog.

borntyping avatar borntyping commented on August 16, 2024

This should be fixed by 02410e5 and 3967729.

from python-colorlog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.