Comments (7)
Ah, I think I've broken anything that uses multiple colors (e.g. red,bg_white
).
from python-colorlog.
Has any progress been made regarding that feature request?
from python-colorlog.
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.
@borntyping thanks for your quick reply, if doable, could you please add a working example into the README.
Many thanks,
Alex
from python-colorlog.
https://github.com/borntyping/python-colorlog#with-custom-log-levels 😄
from python-colorlog.
@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: ()
from python-colorlog.
This should be fixed by 02410e5 and 3967729.
from python-colorlog.
Related Issues (20)
- No module named 'colorlog' HOT 1
- v4.8.0 - ColoredRecord does not set asctime property. HOT 2
- 5.0.0: test suite warnings HOT 2
- 'bold' option is not working HOT 1
- Typing information is missing on import. HOT 2
- ModuleNotFoundError: No module named 'colorlog' HOT 1
- Purple - PowerShell HOT 1
- hope add predefined level such as colorlog.ERROR colorlog.DEBUG and so on HOT 1
- [Bug] colorlog.basicConfig() doesn't seems to work when upgrading from v5 to v6 HOT 3
- colorlog introduces "[1m[0m"-like entries in logs running on argo workflows HOT 2
- Logging is sometimes duplicated without color HOT 5
- How to make `print()` statement output colorful?
- Doesn't support defaults HOT 1
- Document `light_*` colors? HOT 4
- Update default_log_colors from log_colors instead of replace it HOT 3
- Failed to add color to log HOT 4
- Docu like ReadTheDocs / Offering a objects.inv HOT 1
- Duplicative stream definition required due to design choice made in #64 HOT 1
- support *stacklevel* argument HOT 3
- colorlog.basicConfig does not accept '{' style HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-colorlog.