Custom Levels¶
The eight built-in levels cover most needs. But sometimes you want a level that means something specific to your app: a NOTICE between INFO and WARNING, an AUDIT level for security events, a METRIC level...
You can create your own levels with logger.level(), and use them like the built-in ones.
Define a level¶
Call logger.level() with a name and a severity number (no), then log with logger.log():
from logust import logger
logger.level("NOTICE", no=23, color="cyan", icon="đ")
logger.info("Deploying version 2.4.0")
logger.log("NOTICE", "Feature flag 'new-checkout' is now on")
logger.warning("The cache is cold")
On a terminal, the NOTICE line is printed in cyan. đ
The parameters are:
| Parameter | Type | Description |
|---|---|---|
name | str | The level name. Names are case-insensitive, uppercase is the convention. |
no | int | The severity. A higher number is more severe. |
color | str | The color of the level on the console (see Available colors). |
icon | str | An optional icon, shown by {level.icon} in a format. |
Choosing the number¶
The number decides where your level sits among the built-in ones:
| Level | no |
|---|---|
TRACE | 5 |
DEBUG | 10 |
INFO | 20 |
SUCCESS | 25 |
WARNING | 30 |
ERROR | 40 |
FAIL | 45 |
CRITICAL | 50 |
NOTICE with no=23 is a bit more important than INFO, and less than SUCCESS and WARNING. A handler that shows INFO and above also shows NOTICE, and a handler that only shows WARNING and above hides it.
Tip
Pick a number that no built-in level uses. logger.log(25, ...) always means SUCCESS, even if you registered a custom level with no=25, so a free number keeps "log by number" unambiguous.
Look up a level¶
Call logger.level() with only a name to get the level's information back. Every call to logger.level() returns a Level named tuple with name, no, color and icon:
from logust import logger
logger.level("NOTICE", no=23, color="cyan", icon="đ")
print(logger.level("NOTICE"))
print(logger.level("notice").no)
logger.level("NOTICE", icon="đŖ")
logger.level("INFO", color="blue")
print(logger.level("NOTICE"))
print(logger.level("INFO"))
Notice that logger.level("notice") finds NOTICE: level names are case-insensitive.
Looking up a level that doesn't exist raises a ValueError:
Update a level¶
Call logger.level() with a name and a color and/or icon, but without no, to change an existing level. The other values are kept.
That's what lines 8 and 9 of the example above do:
logger.level("NOTICE", icon="đŖ")givesNOTICEa new icon, and keeps its number and color.logger.level("INFO", color="blue")works on built-in levels too:INFOis now blue on the console.
You can also call logger.level() again with no to re-register a custom level with a new number:
Built-in levels keep their numbers, though. Changing one raises a TypeError:
logger.level("INFO", no=21)
# TypeError: Level 'INFO' already exists, you can't update its severity no
Technical Details
Level.color is a color name like "cyan". In loguru it's a markup string like "<cyan><bold>". And unlike loguru, passing no for an existing custom level re-registers it instead of raising.
Icons and numbers in a format¶
The level of each record has an icon, a name and a no. You can use all of them in a format:
import sys
from logust import logger
logger.remove()
logger.add(sys.stdout, format="{level.icon} {level.name:<8} ({level.no}) | {message}")
logger.level("NOTICE", no=23, color="cyan", icon="đ")
logger.info("Deploying version 2.4.0")
logger.log("NOTICE", "Feature flag 'new-checkout' is now on")
logger.success("Deployed")
Built-in levels use loguru's default icons:
| Level | Icon | Level | Icon | |
|---|---|---|---|---|
TRACE | âī¸ | WARNING | â ī¸ | |
DEBUG | đ | ERROR | â | |
INFO | âšī¸ | FAIL | âī¸ | |
SUCCESS | â | CRITICAL | â ī¸ |
FAIL is a Logust-only level, so its icon has no loguru counterpart. A custom level without an icon renders {level.icon} as an empty string.
Available colors¶
These names work for color=:
black,red,green,yellow,blue,magenta,cyan,whitebright_red,bright_green,bright_yellow,bright_blue,bright_magenta,bright_cyan,bright_white
Warning
An unknown color name is not an error: the level is shown in white. If your level doesn't get the color you expected, check the spelling. đ¤
The built-in levels use cyan (TRACE), blue (DEBUG), green (INFO), bright_green (SUCCESS), yellow (WARNING), red (ERROR), magenta (FAIL) and bright_red (CRITICAL).
Logging with a custom level¶
There is no logger.notice() method. Custom levels are logged with logger.log(), by name or by number:
logger.log("NOTICE", "By name")
logger.log("notice", "Also by name, names are case-insensitive")
logger.log(23, "By number")
logger.log() accepts the same message arguments as the other methods:
And it works with opt() too, for example logger.opt(lazy=True).log("NOTICE", ...).
A level name that was never registered raises a ValueError:
Filtering by a custom level¶
A custom level works as a threshold anywhere a level is: pass its name to level=, and the handler takes that level and everything above it:
import sys
from logust import logger
logger.remove()
logger.level("NOTICE", no=23, color="cyan")
logger.add(sys.stdout, level="NOTICE", format="{level:<8} | {message}")
logger.info("Deploying version 2.4.0")
logger.log("NOTICE", "Feature flag 'new-checkout' is now on")
logger.log(23, "The same level, by number")
logger.warning("The cache is cold")
A record passes when its level number is at least the threshold's: NOTICE is 23, so the handler keeps NOTICE (23) and WARNING (30), and drops INFO (20).
The same goes for every level= option: logger.add(level=...) for every kind of sink, logger.set_level(), logger.enable(level=...), logger.add_callback(level=...), logger.is_level_enabled() and the "level" key of logger.configure(handlers=[...]). Each one accepts:
- A level name, built-in or custom, in any case:
"WARNING","notice". - A number,
0or more:level=23. It doesn't have to be a registered level. - A
LogLevelmember:level=LogLevel.Warning.
logger.enable() takes a custom level as a keyword only: logger.enable(level="NOTICE"). A positional name that is not a built-in level is a module name, so logger.enable("NOTICE") re-enables a module called NOTICE.
The comparison is always by number, so built-in and custom levels mix freely. With level="SUCCESS" (25) a handler shows custom levels from 25 up, and with level="NOTICE" it shows SUCCESS too.
Register a level before you use it as a threshold. A name that doesn't exist raises a ValueError (Level 'NOPE' does not exist), and so does a negative number. Any other type raises a TypeError.
Levels in configure()¶
If you set up logging with logger.configure(), you can declare and update levels in the same place, with the same rules as logger.level():
logger.configure(
levels=[
{"name": "NOTICE", "no": 23, "color": "cyan", "icon": "đ"},
{"name": "INFO", "color": "blue"},
],
)
Recap¶
logger.level("NOTICE", no=23, color="cyan", icon="đ")creates a level.logger.level("NOTICE")looks it up, and returns aLevel(name, no, color, icon).logger.level("INFO", color="blue")updates a level, built-in ones included.- Log with
logger.log("NOTICE", ...)orlogger.log(23, ...). {level.icon},{level.name}and{level.no}show the level in a format.level="NOTICE"(orlevel=23) starts a handler at a custom level, andlogger.set_level("NOTICE")does the same for the console.