Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Colors for extra levels #12

Closed
jarogames opened this issue Jul 2, 2017 · 6 comments
Closed

Colors for extra levels #12

jarogames opened this issue Jul 2, 2017 · 6 comments

Comments

@jarogames
Copy link

jarogames commented Jul 2, 2017

Hi, it is nice. And yes, it is really important to say, that logging is behind. My questions:

1/ how is it possible to have green color for all INFO levels (11-20)?

2/ Colors are also in a logfile, would it be possible to have colors on the screen but not in the file (for later parsing)?

@metachris
Copy link
Owner

Hi,

Re 1) What do you mean with all INFO levels (11-20)? Can you provide some sample code? For me if I setup the logger with a loglevel of 15 it will print all INFO messages. And INFO messages are always green.

Re 2) This is an error on logzero side. It should not write the color codes to the logfile too. I'll fix it and report back to this thread.

@metachris
Copy link
Owner

Re 2 - Just released v1.1.1 which fixed this issue: 68323d9

@jarogames
Copy link
Author

jarogames commented Jul 4, 2017

I have cut out this - see that levl 19 is not green

logging.addLevelName(19, "+PLUS")
def infoP(self, message, *args, **kws):
    if self.isEnabledFor(19):self._log(19, message, args, **kws) 
logging.Logger.infoP = infoP
log_format = '%(color)s%(levelname)1.1s... %(end_color)s %(message)s'  # i...  format
formatter = LogFormatter(fmt=log_format)
logger = setup_logger( name="main",logfile=logfile, level=1,formatter=formatter )


logger.warn("warn")
logger.error("error")
logger.infoP("info plus")

@metachris
Copy link
Owner

Thanks for the example. I will look into this and report back here.

@metachris
Copy link
Owner

metachris commented Jul 10, 2017

logzero.LogFormatter has a DEFAULT_COLORS dict, which sets the colors for specific loglevels. You need to add a color for your exact loglevel to logzero.LogFormatter.DEFAULT_COLORS:

import logging
import logzero

# Add a custom logging level
logging.addLevelName(19, "+PLUS")
def infoP(self, message, *args, **kws):
    if self.isEnabledFor(19):self._log(19, message, args, **kws)
logging.Logger.infoP = infoP

# Set a color for the custom loglevel 19
logzero.LogFormatter.DEFAULT_COLORS[19] = logzero.colors.Fore.GREEN

# Create the logger
logger = logzero.setup_logger()

# Log messages
logger.debug("debug")
logger.infoP("info plus")
logger.info("info")

These are the available foreground colors: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE.

Does that work for your workflow, or would you rather have a more simple API to accomplish this?

@metachris
Copy link
Owner

logzero was updated to v1.3.0 today, and supports color output on Windows with colorama. For this update the color definitions changed a little, now you would specify the custom color like this:

logzero.LogFormatter.DEFAULT_COLORS[19] = logzero.colors.Fore.GREEN

Before it was

logzero.LogFormatter.DEFAULT_COLORS[19] = 2

I've updated the code in my last comment.

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

No branches or pull requests

2 participants